Skip to content

Commit 9deedc7

Browse files
authored
[Unpacking, Generators, Bin/Ox/Hex Concepts]: Typo & Formatting Fixes (exercism#4186)
* Typo and formatting fixes in concept docs. * Took care of the generator-iterator thingy.
1 parent ed70b14 commit 9deedc7

6 files changed

Lines changed: 47 additions & 19 deletions

File tree

concepts/binary-octal-hexadecimal/about.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ A snippet from the base 2 system looks like this, although it continues infinite
1818
| -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- |
1919
| 2 \*\* 7 | 2 \*\* 6 | 2 \*\* 5 | 2 \*\* 4 | 2 \*\* 3 | 2 \*\* 2 | 2 \*\* 1 | 2 \*\* 0 |
2020

21-
So if we want to represent the number 6, it would in binary be: 110
21+
So if we want to represent the number 6 in binary, it would be 110.
2222

2323
| Place value | 4 | 2 | 1 |
2424
| ------------- | --- | --- | --- |
@@ -41,7 +41,6 @@ In Python, we can represent binary literals using the `0b` prefix.
4141
If we write `0b10011`, Python will interpret it as a binary number and convert it to base 10.
4242

4343
```python
44-
# 0b10011
4544
>>> 0b10011
4645
19
4746

@@ -86,6 +85,8 @@ However, the usual mathematical operator rules apply: dividing two binary numbe
8685

8786
>>> 0b10011/3
8887
6.333333333333333
88+
```
89+
8990

9091
### Converting to and from Binary Representation
9192

@@ -133,6 +134,9 @@ For example, `bit_count()` on '0b11011' will return 4:
133134
```python
134135
>>> 0b11011.bit_count()
135136
4
137+
```
138+
139+
136140
~~~~exercism/note
137141
If you are working locally, `bit_count()` requires at least Python 3.10.
138142
The Exercism online editor currently supports all features through Python 3.11.
@@ -148,7 +152,6 @@ In Python, we can represent octal numbers using the `0o` prefix.
148152
As with binary, Python automatically converts an octal representation to an `int`.
149153

150154
```python
151-
# 0o123
152155
>>> 0o123
153156
83
154157
```
@@ -157,14 +160,15 @@ As with binary, octal numbers **are ints** and support all integer operations.
157160
Prefixing a number with `0o` that is not in the octal system will raise a `SyntaxError`.
158161

159162
### Converting to and from Octal Representation
160-
161163

162164
To convert an `int` into an octal representation, you can use the built-in [`oct()`][oct] function.
163165
This acts similarly to the `bin()` function, returning a string:
164166

165167
```python
166168
>>> oct(83)
167169
'0o123'
170+
```
171+
168172

169173
To convert an octal number to an integer, we can use the `int()` function, passing an octal string representation and the base (8) as arguments:
170174

@@ -175,22 +179,21 @@ To convert an octal number to an integer, we can use the `int()` function, passi
175179

176180
As with binary, giving the wrong base will raise a `ValueError`.
177181

178-
### Hexadecimal
182+
## Hexadecimal
179183

180184
[Hexadecimal][hexadecimal] is a base 16 numeral system.
181185
It uses the digits 0 - 9 and the letters A, B, C, D, E, and F.
182186
A is 10, B is 11, C is 12, D is 13, E is 14, and F is 15.
183187

184188
We can represent hexadecimal numbers in Python using the `0x` prefix.
185-
As with binary and octal, Python will automatically convert hexadecimal literals to `int`.
189+
As with binary and octal, Python will automatically convert hexadecimal literals to `int`s.
186190

187191
```python
188-
# 0x123
189192
>>> 0x123
190193
291
191194
```
192195

193-
As with binary and octal - hexadecimal literals **are ints**, and you can perform all integer operations.
196+
As with binary and octal hexadecimal literals **are ints**, and you can perform all integer operations with them.
194197
Prefixing a non-hexadecimal number with `0x` will raise a `SyntaxError`.
195198

196199

@@ -202,6 +205,8 @@ This acts similarly to the `bin()` function, returning a string:
202205
```python
203206
>>> hex(291)
204207
'0x123'
208+
```
209+
205210

206211
To convert a hexadecimal representation to an integer, we can use the `int()` function, passing a hexadecimal string with the base (16) as arguments:
207212

concepts/binary-octal-hexadecimal/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# binary, octal, hexadecimal
1+
# Binary, Octal, Hexadecimal
22

33
Binary, octal, and hexadecimal (_also known as hex_) are different [numeral systems][numeral-systems] with different bases.
44
Binary is base 2, octal is base 8, and hexadecimal is base 16.

concepts/generators/about.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,33 @@ The rationale behind this is that you use a generator when you do not need all t
3535

3636
This saves memory and processing power, since only the value you are _currently working on_ is calculated.
3737

38+
3839
## Using a generator
3940

40-
Generators may be used in place of most `iterables` in Python. This includes _functions_ or _objects_ that require an `iterable`/`iterator` as an argument.
41+
Generators (_technically [`generator-iterator`s][generator-iterator] — see the note below._) are a type of `iterator` and can be used anywhere in Python where an `iterator` or `iterable` is expected.
42+
This includes _functions_ or _objects_ that require an `iterable`/`iterator` as an argument.
43+
For a deeper dive, see [How to Make an Iterator in Python][how-to-iterator].
44+
45+
46+
~~~~exercism/note
47+
48+
Generator-iterators are a special sub-set of [iterators][iterator].
49+
`Iterators` are the mechanism/protocol that enables looping over _iterables_.
50+
Generator-iterators and the iterators returned by common Python [`iterables`][iterables] act very similarly, but there are some important differences to note:
51+
52+
- They are _[lazily evaluated][lazy evaluation]_; iteration is _one-way_ and there is no "backing up" to a previous value.
53+
- They are _consumed_ by iterating over the returned values; there is no resetting or saving in memory.
54+
- They are not sortable and cannot be reversed.
55+
- They are not sequence types, and _do not_ have `indexes`.
56+
You cannot reference a previous or future value using addition or subtraction and you cannot use bracket (`[]`) notation or slicing.
57+
- They cannot be used with the `len()` function, as they have no length.
58+
- They can be _finite_ or _infinite_ - be careful when collecting all values from an _infinite_ `generator-iterator`!
59+
60+
[iterator]: https://docs.python.org/3.11/glossary.html#term-iterator
61+
[iterables]: https://wiki.python.org/moin/Iterator
62+
[lazy evaluation]: https://en.wikipedia.org/wiki/Lazy_evaluation
63+
~~~~
64+
4165

4266
To use the `squares_generator()` generator:
4367

@@ -140,7 +164,8 @@ Generators are also very helpful when a process or calculation is _complex_, _ex
140164
Now whenever `__next__()` is called on the `infinite_sequence` object, it will return the _previous number_ + 1.
141165

142166

143-
[generator-iterator]: https://docs.python.org/3.11/glossary.html#term-generator-iterator
167+
[generator-iterator]: https://docs.python.org/3/glossary.html#term-generator-iterator
168+
[how-to-iterator]: https://treyhunner.com/2018/06/how-to-make-an-iterator-in-python/#Generators:_the_easy_way_to_make_an_iterator
144169
[iterables]: https://wiki.python.org/moin/Iterator
145170
[iterator]: https://docs.python.org/3.11/glossary.html#term-iterator
146171
[lazy iterator]: https://en.wikipedia.org/wiki/Lazy_evaluation

concepts/unpacking-and-multiple-assignment/about.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A very common example of this behavior is `for item in list`, where `item` takes
88
This allows for code to be more concise and readable, and is done by separating the variables to be assigned with a comma such as `first, second, third = (1,2,3)` or `for index, item in enumerate(iterable)`.
99

1010
The special operators `*` and `**` are often used in unpacking contexts.
11-
`*` can be used to combine multiple `lists`/`tuples` into one `list`/`tuple` by _unpacking_ each into a new common `list`/`tuple`.
11+
`*` can be used to combine multiple `list`s/`tuple`s into one `list`/`tuple` by _unpacking_ each into a new common `list`/`tuple`.
1212
`**` can be used to combine multiple dictionaries into one dictionary by _unpacking_ each into a new common `dict`.
1313

1414
When the `*` operator is used without a collection, it _packs_ a number of values into a `list`.
@@ -73,7 +73,7 @@ Since `tuples` are immutable, you can't swap elements in a `tuple`.
7373
The examples below use `lists` but the same concepts apply to `tuples`.
7474
~~~~
7575

76-
In Python, it is possible to [unpack the elements of `list`/`tuple`/`dictionary`][unpacking] into distinct variables.
76+
In Python, it is possible to [unpack the elements of a `list`/`tuple`/`dict`][unpacking] into distinct variables.
7777
Since values appear within `lists`/`tuples` in a specific order, they are unpacked into variables in the same order:
7878

7979
```python
@@ -94,7 +94,7 @@ If there are values that are not needed then you can use `_` to flag them:
9494

9595
### Deep unpacking
9696

97-
Unpacking and assigning values from a `list`/`tuple` inside of a `list` or `tuple` (_also known as nested lists/tuples_), works in the same way a shallow unpacking does, but often needs qualifiers to clarify the values context or position:
97+
Unpacking and assigning values from a `list`/`tuple` inside of a `list` or `tuple` (_also known as nested lists/tuples_), works in the same way a shallow unpacking does, but often needs qualifiers to clarify the value's context or position:
9898

9999
```python
100100
>>> fruits_vegetables = [["apple", "banana"], ["carrot", "potato"]]

concepts/unpacking-and-multiple-assignment/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A very common example of this behavior is `for item in list`, where `item` takes
88
This allows for code to be more concise and readable, and is done by separating the variables to be assigned with a comma such as `first, second, third = (1,2,3)` or `for index, item in enumerate(iterable)`.
99

1010
The special operators `*` and `**` are often used in unpacking contexts.
11-
`*` can be used to combine multiple `lists`/`tuples` into one `list`/`tuple` by _unpacking_ each into a new common `list`/`tuple`.
11+
`*` can be used to combine multiple `list`s/`tuple`s into one `list`/`tuple` by _unpacking_ each into a new common `list`/`tuple`.
1212
`**` can be used to combine multiple dictionaries into one dictionary by _unpacking_ each into a new common `dict`.
1313

1414
When the `*` operator is used without a collection, it _packs_ a number of values into a `list`.

exercises/concept/plane-tickets/.docs/introduction.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Generators
22

3-
A `generator` is a function or expression that returns a special type of [iterator][iterator] called [generator iterator][generator-iterator].
4-
`Generator-iterators` are [lazy][lazy iterator]: they do not store their `values` in memory, but _generate_ their values when needed.
3+
A `generator` is a function or expression that returns a special type of [iterator][iterator] called a [`generator iterator`][generator-iterator].
4+
`Generator-iterator`s are [lazy][lazy iterator]: they do not store their `values` in memory, but _generate_ their values when needed.
55

66
A generator function looks like any other function, but contains one or more [yield expressions][yield expression].
77
Each `yield` will suspend code execution, saving the current execution state (_including all local variables and try-statements_).
@@ -144,8 +144,6 @@ Now whenever `__next__()` is called on the `infinite_sequence` object, it will r
144144

145145

146146
[generator-iterator]: https://docs.python.org/3.11/glossary.html#term-generator-iterator
147-
[iterables]: https://wiki.python.org/moin/Iterator
148147
[iterator]: https://docs.python.org/3.11/glossary.html#term-iterator
149-
[lazy evaluation]: https://en.wikipedia.org/wiki/Lazy_evaluation
150148
[lazy iterator]: https://en.wikipedia.org/wiki/Lazy_evaluation
151149
[yield expression]: https://docs.python.org/3.11/reference/expressions.html#yield-expressions

0 commit comments

Comments
 (0)