Skip to content

Commit eddb892

Browse files
committed
add fourth approach
1 parent 12c7b6e commit eddb892

5 files changed

Lines changed: 87 additions & 6 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Use the Built-In Bit-Count Functionality
2+
3+
~~~~exercism/caution
4+
This approach does _not_ follow the instructions, as it uses the bit-count functionality from the standard library.
5+
It is only described here to show what an idiomatic way of counting bits in a _different context_ would be.
6+
~~~~
7+
8+
```python
9+
def egg_count(display_value):
10+
return display_value.bit_count()
11+
```
12+
13+
This approach uses [`int.bit_count()`][int-bit_count] from the Python standard library to count the number of ones in the binary representation of `display_value`.
14+
15+
This works because Python does _not_ have separate types for binary, octal, or hexadecimal numbers.
16+
Even if a binary literal is declared with the `0b` prefix, Python still stores it as an `int`.
17+
Due to this, all of the methods that work with binary numbers in the standard library just operate on `int`s.
18+
19+
20+
## Variation #1: Using `str.count()`
21+
22+
```python
23+
def egg_count(display_value):
24+
return bin(display_value).count("1")
25+
```
26+
27+
This variant uses [`bin()`][bin-built-in] (or any other method discussed in the [convert to a binary string][approach-convert-to-binary-string] approach) to convert `display_value` to a binary string.
28+
Then, [`str.count()`][sequence-count] is used to count how many times "1" appears in the string.
29+
30+
Though one could argue that this _technically_ doesn't use the built-in bit-count functionality, the solution still defeats the purpose of the exercise.
31+
32+
33+
## Variation #2: Using Function Aliasing
34+
35+
```python
36+
egg_count = int.bit_count
37+
```
38+
39+
This solution is the shortest of them all, but it can also be rather confusing (and it still does not follow the instructions).
40+
41+
This variant makes clever use of [function aliasing][function-aliasing], which creates a new name that refers to the same exact function.
42+
However, you should be careful when using function aliasing, as it often makes code _less_ readable, and it doesn't have many practical applications outside of backward compatibility.
43+
44+
This solution works because `int.bit_count(<integer_variable>)` is just another way of saying `<integer_variable>.bit_count()`, as instance methods of a class have `self` implicitly passed as the first parameter when calling them on an instance (but not when calling them directly on the class).
45+
See the [Classes Concept in the Syllabus][concept-classes: methods] or the [Official Python Classes Tutorial][class-method-objects-tutorial] for more detail on how this works.
46+
47+
48+
[approach-convert-to-binary-string]: https://exercism.org/tracks/python/exercises/eliuds-eggs/approaches/convert-to-binary-string
49+
[bin-built-in]: https://docs.python.org/3/library/functions.html#bin
50+
[class-method-objects-tutorial]: https://docs.python.org/3/tutorial/classes.html#method-objects
51+
[concept-classes: methods]: https://exercism.org/tracks/python/concepts/classes#h-methods
52+
[function-aliasing]: https://tutorialreference.com/python/examples/faq/python-how-to-use-function-aliasing
53+
[int-bit_count]: https://docs.python.org/3/library/stdtypes.html#int.bit_count
54+
[sequence-count]: https://docs.python.org/3/library/stdtypes.html#sequence.count
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def egg_count(display_value):
2+
return display_value.bit_count()

exercises/practice/eliuds-eggs/.approaches/config.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
"uuid": "27fb20ed-a4c2-4f73-a8fc-86ba384c7b35",
99
"slug": "parameter-modification",
1010
"title": "Modify the Parameter in a Loop",
11-
"blurb": "Modify the parameter in a while-loop to calculate the number of eggs.",
11+
"blurb": "Modify the parameter in a while-loop to determine the number of eggs.",
1212
"authors": ["yrahcaz7"]
1313
},
1414
{
1515
"uuid": "65fd717c-0e50-444b-a4b2-b51862f1f810",
1616
"slug": "no-parameter-modification",
1717
"title": "Loop Without Modifying the Parameter",
18-
"blurb": "Loop over the bits without modifying the parameter to determine the number of eggs.",
18+
"blurb": "Loop over the bits without modifying the parameter to calculate the number of eggs.",
1919
"authors": ["yrahcaz7"]
2020
},
2121
{
@@ -24,6 +24,13 @@
2424
"title": "Convert to a Binary String",
2525
"blurb": "Convert the parameter to a binary string and count its ones to determine the number of eggs.",
2626
"authors": ["yrahcaz7"]
27+
},
28+
{
29+
"uuid": "97b06094-7ce8-4874-b66a-af7391adc853",
30+
"slug": "built-in-bit-count",
31+
"title": "Use the Built-In Bit-Count Functionality",
32+
"blurb": "Use Python's Built-In Bit-Count Functionality to calculate the number of eggs.",
33+
"authors": ["yrahcaz7"]
2734
}
2835
]
2936
}

exercises/practice/eliuds-eggs/.approaches/convert-to-binary-string/content.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ However, the creation and repeated calling of the `lambda` adds unnecessary over
105105

106106

107107
[bin-built-in]: https://docs.python.org/3/library/functions.html#bin
108-
[f-string]: https://docs.python.org/3/reference/lexical_analysis.html#f-strings
109108
[generator-expression]: https://dbader.org/blog/python-generator-expressions
110109
[lambda-expression]: https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions
111110
[list-comprehension]: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

exercises/practice/eliuds-eggs/.approaches/introduction.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,29 @@ Many variations of this approach use a built-in function like `sum()` to make th
8888
For more details, check out the [convert to a binary string][approach-convert-to-binary-string] approach.
8989

9090

91-
[approach-parameter-modification]: https://exercism.org/tracks/python/exercises/eliuds-eggs/approaches/parameter-modification
92-
[approach-no-parameter-modification]: https://exercism.org/tracks/python/exercises/eliuds-eggs/approaches/no-parameter-modification
91+
## Approach: Using the Built-In Bit-Count Functionality
92+
93+
~~~~exercism/caution
94+
This approach does _not_ follow the instructions, as it uses the bit-count functionality from the standard library.
95+
It is only described here to show what an idiomatic way of counting bits in a _different context_ would be.
96+
~~~~
97+
98+
```python
99+
def egg_count(display_value):
100+
return display_value.bit_count()
101+
```
102+
103+
This approach uses [`int.bit_count()`][int-bit_count] from the Python standard library to count the number of ones in the binary representation of `display_value`.
104+
105+
For more details and variations, read the [built-in bit-count][approach-built-in-bit-count] approach.
106+
107+
108+
[approach-built-in-bit-count]: https://exercism.org/tracks/python/exercises/eliuds-eggs/approaches/built-in-bit-count
93109
[approach-convert-to-binary-string]: https://exercism.org/tracks/python/exercises/eliuds-eggs/approaches/convert-to-binary-string
110+
[approach-no-parameter-modification]: https://exercism.org/tracks/python/exercises/eliuds-eggs/approaches/no-parameter-modification
111+
[approach-parameter-modification]: https://exercism.org/tracks/python/exercises/eliuds-eggs/approaches/parameter-modification
94112
[bin-built-in]: https://docs.python.org/3/library/functions.html#bin
95113
[concept-numbers]: https://exercism.org/tracks/python/concepts/numbers
96-
[right-shift-operator]: https://www.geeksforgeeks.org/software-engineering/right-shift-operator-in-programming/
97114
[f-string]: https://docs.python.org/3/reference/lexical_analysis.html#f-strings
115+
[int-bit_count]: https://docs.python.org/3/library/stdtypes.html#int.bit_count
116+
[right-shift-operator]: https://www.geeksforgeeks.org/software-engineering/right-shift-operator-in-programming/

0 commit comments

Comments
 (0)