# A Common-Sense Guide to Data Structures and Algorithms, Second Edition: suggestion for sum\_swap example (page 413)

**URL:** <https://forum.devtalk.com/t/a-common-sense-guide-to-data-structures-and-algorithms-second-edition-suggestion-for-sum-swap-example-page-413/38830>\
**Category:** PragProg Customers\
**Tags:** book-a-common-sense-guide-to-data-structures-and-algorithms-second-edition, suggestion\
**Created:** [2 February 2022 04:14 UTC](https://forum.devtalk.com/t/a-common-sense-guide-to-data-structures-and-algorithms-second-edition-suggestion-for-sum-swap-example-page-413/38830 "2022-02-02T04:14:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjd](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@pjd](https://forum.devtalk.com/u/pjd)\
**Post date:** [2 February 2022 04:14 UTC](https://forum.devtalk.com/t/a-common-sense-guide-to-data-structures-and-algorithms-second-edition-suggestion-for-sum-swap-example-page-413/38830/1 "2022-02-02T04:14:54Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jaywengrow](https://forum.devtalk.com/user_avatar/forum.devtalk.com/jaywengrow/32/527_2.png) [@jaywengrow](https://forum.devtalk.com/u/jaywengrow)\
**Post date:** [12 March 2023 22:33 UTC](https://forum.devtalk.com/t/a-common-sense-guide-to-data-structures-and-algorithms-second-edition-suggestion-for-sum-swap-example-page-413/38830/2 "2023-03-12T22:33:14Z")

</div>

Thank you! Currently, the method does handle cases where swapping the sums isn’t possible, as it returns nil at the end. However, you are correct about the odd number issue. I will handle this in a future version. Good catch!
