
pjd
A Common-Sense Guide to Data Structures and Algorithms, Second Edition: suggestion for sum_swap example (page 413)
Maybe you were just trying to keep the sum_swap code clean and streamlined for ease of reading, but this method didn’t seem as air-tight as other example methods in the book. You may want to at least comment that not all cases are handled by the method.
Two ways I think this method might fail are 1) the method does not handle the case where swapping to even out the sums is not possible, and 2) since shift_amount is calculated with integer division and thus will always be an integer, I suspect that sum_swap could give an incorrect answer if sum_1 - sum_2 is an odd number.
Then again, maybe those types of caveats are understood and maybe I missed other such scenarios elsewhere in the book.
I’m getting close to the end…finally! Great book, very well-written and helpful for me!
Popular Prag Prog topics










Other popular topics









Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /django
- /security
- /nodejs
- /centos
- /rails
- /haskell
- /fable
- /gleam
- /js
- /swift
- /deno
- /tailwind
- /assemblyscript
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /c-plus-plus
- /preact
- /flutter
- /actix
- /java
- /angular
- /ocaml
- /zig
- /scala
- /kubuntu
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /emacs
- /vuejs
- /nim
- /nerves
- /elm