You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/practice/eliuds-eggs/.approaches/parameter-modification/content.md
+41Lines changed: 41 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -115,9 +115,50 @@ As `display_value` is updated in the multiple assignment expression, we don't ne
115
115
Just like the previous variations, the loop will continue until `display_value` reaches 0, and then we return `eggs`.
116
116
117
117
118
+
## Variation #5: Overcomplicated One-Liner
119
+
120
+
~~~~exercism/caution
121
+
This approach is not idiomatic and can be quite confusing.
122
+
It is only provided here to show how one could apply various advanced techniques to turn this approach into a one-liner.
123
+
~~~~
124
+
125
+
```python
126
+
defegg_count(display_value):
127
+
returnsum(
128
+
(value %2, display_value := value //2)[0]
129
+
for value initer(lambda: display_value, 0)
130
+
)
131
+
```
132
+
133
+
This variation uses [the `sum()` built-in][sum-built-in], a [generator expression][generator-expression], a [`lambda` expression][lambda-expression], and a [walrus operator (`:=`)][walrus-operator] to reduce the solution to a one-liner.
134
+
The line is only broken up here for readability.
135
+
136
+
Here, the `while-loop` is converted into a generator expression, with `sum()` adding up the result of each iteration.
137
+
As the `while` keyword is not allowed in generator expressions, instead we iterate over an iterable with `for`.
138
+
This iterable is constructed from a `lambda` that returns `display_value`, with the [sentinel value][sentinel-value] set to `0`.
139
+
This means that [`iter()`][iter-built-in] returns an iterable that calls the `lambda` until the returned `display_value` equals `0`.
140
+
141
+
For each iteration of the generator expression, we assign `value` to the return value of the `lambda`.
142
+
Then we construct a `tuple` with two elements, using `[0]` to get its first element and feed it to `sum()`.
143
+
That element is the least significant bit of `value`, which can be calculated via `value % 2` or `value & 1`, as shown in the previous variations.
144
+
145
+
The second element is more complicated.
146
+
Here, we update `display_value`, cutting off the least significant bit (via `// 2` or `>> 1`) by using the walrus operator (`:=`).
147
+
The walrus operator acts like a simple assignment statement, except that it returns the right-hand value and it can be used anywhere that an expression can be used.
148
+
(See the [Python docs][assignment-expression-docs] for more details.)
149
+
Thus we can use walrus operator here to update `display_value` in the generator expression, then simply ignore the return value by only feeding the first element of the `tuple` to `sum()`.
0 commit comments