|
| 1 | +# Modify the Parameter in a Loop |
| 2 | + |
| 3 | +```python |
| 4 | +def egg_count(display_value): |
| 5 | + eggs = 0 |
| 6 | + while display_value: |
| 7 | + eggs += display_value % 2 |
| 8 | + display_value //= 2 |
| 9 | + return eggs |
| 10 | +``` |
| 11 | + |
| 12 | +This approach uses a `while-loop` to count up all of the ones. |
| 13 | +In the loop, we increment `eggs` by `display_value % 2`. |
| 14 | +This adds the least significant bit (the rightmost digit in the binary representation) of `display_value` to `eggs`. |
| 15 | + |
| 16 | +Next, we divide `display_value` by `2`, discarding any remainder. |
| 17 | +This essentially removes the least significant bit of `display_value`, setting up `display_value` for processing the next bit. |
| 18 | + |
| 19 | +The loop repeats until `display_value` reaches `0` (which indicates that we have no more bits to check), and then we return `eggs`. |
| 20 | + |
| 21 | + |
| 22 | +## Variation #1: Using Boolean Operators |
| 23 | + |
| 24 | +```python |
| 25 | +def egg_count(display_value): |
| 26 | + eggs = 0 |
| 27 | + while display_value > 0: |
| 28 | + if display_value % 2 == 1: |
| 29 | + eggs += 1 |
| 30 | + display_value //= 2 |
| 31 | + return eggs |
| 32 | +``` |
| 33 | + |
| 34 | +This is essentially just a more verbose formulation of the previous version. |
| 35 | +Instead of relying on Python converting `int`s to `bool`s, this solution manually compares `display_value` to `0`. |
| 36 | +It also uses an `if` statement to check if `eggs` should be incremented by `1`, instead of directly using the result of `display_value % 2`. |
| 37 | + |
| 38 | +Even though this variant is more verbose, some may consider it to be more readable. |
| 39 | + |
| 40 | + |
| 41 | +## Variation #2: Using Bitwise Operators |
| 42 | + |
| 43 | +```python |
| 44 | +def egg_count(display_value): |
| 45 | + eggs = 0 |
| 46 | + while display_value > 0: |
| 47 | + eggs += display_value & 1 |
| 48 | + display_value >>= 1 |
| 49 | + return eggs |
| 50 | +``` |
| 51 | + |
| 52 | +This variant replaces the modulo (`%`) and floor division (`//`) with [bitwise operators][bitwise-operators]. |
| 53 | +`&` is the bitwise AND operator, which results in a number whose binary representation only has ones where _both_ of its arguments has ones. |
| 54 | +(All other bits are zeros.) |
| 55 | + |
| 56 | +For example, if we used the numbers `3` (`11` in binary) and `1` (`1` in binary), we get `1`: |
| 57 | + |
| 58 | +```python |
| 59 | +0b011 & 0b001 |
| 60 | +#=> 0b001 |
| 61 | +``` |
| 62 | + |
| 63 | +This is because the only bit in both numbers that is `1` is least significant bit. |
| 64 | +This property lets us extract the least significant bit of `display_value` by using `display_value & 1`. |
| 65 | + |
| 66 | +For the next step, we use `>>`, the [right-shift operator][right-shift-operator]. |
| 67 | +The expression `a >> b` shifts all of `a`'s bits to the right by `b` places, and returns the resulting number. |
| 68 | + |
| 69 | +For example, if we used the numbers `5` (`101` in binary) and `1`, we get `2` (`10` in binary): |
| 70 | + |
| 71 | +```python |
| 72 | +0b101 >> 1 |
| 73 | +#=> 0b010 |
| 74 | +``` |
| 75 | + |
| 76 | +You can see how `& 1` and `>>= 1` perform the same function as the `% 2` and `//= 2` used in earier variants. |
| 77 | + |
| 78 | + |
| 79 | +[bitwise-operators]: https://www.w3schools.com/programming/prog_operators_bitwise.php |
| 80 | +[right-shift-operator]: https://www.geeksforgeeks.org/software-engineering/right-shift-operator-in-programming/ |
0 commit comments