Skip to content

Commit 69db976

Browse files
committed
add first approach
1 parent 530d9a8 commit 69db976

4 files changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"introduction": {
3+
"authors": ["yrahcaz7"],
4+
"contributors": []
5+
},
6+
"approaches": [
7+
{
8+
"uuid": "27fb20ed-a4c2-4f73-a8fc-86ba384c7b35",
9+
"slug": "parameter-modification",
10+
"title": "Modify the Parameter in a Loop",
11+
"blurb": "Modify the Parameter in a while-loop to Calculate the Number of Eggs.",
12+
"authors": ["yrahcaz7"]
13+
}
14+
]
15+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Introduction
2+
3+
There are many Pythonic approaches to solving the Eliud's Eggs exercise:
4+
5+
- Using a `while-loop`, modifying the parameter on each iteration
6+
- Looping over every binary digit _without_ modifying the parameter
7+
- Converting the `int` to a binary string and counting the ones
8+
9+
There are also some approaches that aren't recommended:
10+
11+
- Using the bit-count functionality from the Python standard library, as the instructions forbid it
12+
- Breaking up the proccess into many functions, overcomplicating the solution
13+
14+
15+
## General guidance
16+
17+
The goal of the Eliud's Eggs exercise is to count the number of ones in the binary representation of a number.
18+
In essence, this requires you to loop over each bit (binary digit) of the number in some way.
19+
20+
The approaches below represent categories of the most common ways of accomplishing this.
21+
22+
23+
## Approach: Modifying the Parameter in a `while-loop`
24+
25+
```python
26+
def egg_count(display_value):
27+
eggs = 0
28+
while display_value:
29+
eggs += display_value % 2
30+
display_value //= 2
31+
return eggs
32+
```
33+
34+
This approach uses a `while-loop` to count up all of the ones.
35+
In the loop, we increment `eggs` by `display_value % 2`.
36+
This adds the least significant bit (the rightmost digit in the binary representation) of `display_value` to `eggs`.
37+
38+
Next, we divide `display_value` by `2`, discarding any remainder.
39+
This essentially removes the least significant bit of `display_value`, setting up `display_value` for processing the next bit.
40+
41+
The loop repeats until `display_value` reaches `0` (which indicates that we have no more bits to check), and then we return `eggs`.
42+
43+
To see more variants of this solution, see the [modify the parameter in a loop][parameter-modification] approach.
44+
45+
46+
[parameter-modification]: https://exercism.org/tracks/python/exercises/eliuds-eggs/approaches/parameter-modification
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def egg_count(display_value):
2+
eggs = 0
3+
while display_value:
4+
eggs += display_value % 2
5+
display_value //= 2
6+
return eggs

0 commit comments

Comments
 (0)