Skip to content

Commit afbd8ad

Browse files
committed
improve clarity and fix rendering issues
1 parent b50b857 commit afbd8ad

6 files changed

Lines changed: 23 additions & 23 deletions

File tree

‎exercises/practice/acronym/.approaches/functools-reduce/content.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ The application of the function travels from left to right, so that the iterable
3838

3939
Using code from the example above, `reduce(lambda start, word: start + word[0], ["GNU", "IMAGE", "MANIPULATION", "PROGRAM"])` would calculate `((("GNU"[0] + "IMAGE"[0]) + "MANIPULATION"[0]) + "PROGRAM"[0])`, or `GIMP`.
4040
The left argument, `start`, is the _accumulated value_ and the right argument, `word`, is the value from the iterable that is used to update the accumulated 'total'.
41-
The optional 'initializer' value `""` is used here, and is placed ahead/before the items of the iterable in the calculation, and serves as a default if the iterable that is passed is empty.
41+
The optional 'initializer' value `""` is used here, and is placed before the items of the iterable in the calculation, and serves as a default if the iterable that is passed is empty.
4242

4343

44-
Since using `reduce()` is fairly succinct, it is put directly on the `return` line to produce the acronym rather than assigning and returning an intermediate variable.
44+
Since using `reduce()` is fairly succinct, it is put directly on the `return` line to produce the acronym, rather than assigning and returning an intermediate variable.
4545

4646

4747
In benchmarks, this solution performed about as well as both the `loops` and the `list-comprehension` solutions.

‎exercises/practice/acronym/.approaches/generator-expression/content.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ A [`generator-expression`][generator-expression] is then used to iterate through
3232
Generator expressions are short-form [generators][generators] — lazy iterators that produce their values _on demand_, instead of saving them to memory.
3333
This generator expression is consumed by [`str.join()`][str-join], which joins the generated letters together using an empty string.
3434
Other "separator" strings can be used with `str.join()` — see [concept:python/string-methods]() for some additional examples.
35-
Since the generator expression and `join()` are fairly succinct, they are put directly on the `return` line rather than assigning and returning an intermediate variable for the acronym.
35+
Since the generator expression and `join()` are fairly succinct, they are put directly on the `return` line, rather than assigning and returning an intermediate variable for the acronym.
3636

3737

3838
In benchmarks, this solution was surprisingly slower than the `list comprehension` version.

‎exercises/practice/acronym/.approaches/list-comprehension/content.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ As of this writing, both of these methods benchmark slower than using `str.repla
2727
A [`list comprehension`][list comprehension] is then used to iterate through the phrase and select the first letters of each word via [`bracket notation`][subscript notation].
2828
This comprehension is passed into [`str.join()`][str-join], which unpacks the `list` of first letters and joins them together using an empty string — the acronym.
2929
Other "separator" strings besides an empty string can be used with `str.join()` — see [concept:python/string-methods]() for some additional examples.
30-
Since the comprehension and `join()` are fairly succinct, they are put directly on the `return` line rather than assigning and returning an intermediate variable for the acronym.
30+
Since the comprehension and `join()` are fairly succinct, they are put directly on the `return` line, rather than assigning and returning an intermediate variable for the acronym.
3131

3232

3333
The weakness of this solution is that it is taking up extra space with the `list comprehension`, which is creating and saving a `list` in memory — only to have that list immediately unpacked by the `str.join()` method.

‎exercises/practice/acronym/.approaches/map-function/content.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ Once the phrase is cleaned and turned into a word `list`, the acronym is created
3030
The application of the function travels from left to right, and function results are produced as needed.
3131

3232

33-
Using code from the example above, `map(lambda word: word[0], ["GNU", "IMAGE", "MANIPULATION", "PROGRAM"])` would calculate `"GNU"[0], "IMAGE"[0], "MANIPULATION"[0]), "PROGRAM"[0]` in order as a stream of data.
33+
Using code from the example above, `map(lambda word: word[0], ["GNU", "IMAGE", "MANIPULATION", "PROGRAM"])` would calculate `"GNU"[0], "IMAGE"[0], "MANIPULATION"[0], "PROGRAM"[0]` in order as a stream of data.
3434
`word[0]` is the function, which extracts the letter at index zero for every word in the phrase list.
3535
This stream of data can then be 'consumed' — either in a `loop`, or by being 'unpacked' by another function or process.
36-
Here, the `iterator` from `map()` is immediately consumed/unpacked by [`join()`][str-join], which glues the results together with an empty string to produce the acronym.
36+
Here, the `iterator` from `map()` is immediately consumed/unpacked by [`str.join()`][str-join], which glues the results together with an empty string to produce the acronym.
3737

3838

39-
Since using `join()` with `map()` is fairly succinct, the combination is put directly on the `return` line to produce the acronym rather than assigning and returning an intermediate variable.
39+
Since using `join()` with `map()` is fairly succinct, the combination is put directly on the `return` line to produce the acronym, rather than assigning and returning an intermediate variable.
4040

41-
In benchmarks, this solution performed about as well as the `loops`, `reduce` and `list-comprehension` solutions.
41+
In benchmarks, this solution performed about as well as the `loops`, `reduce` and `list-comprehension` approaches.
4242

4343
[chaining]: https://pyneng.readthedocs.io/en/latest/book/04_data_structures/method_chaining.html
4444
[lazy iterator]: https://www.pythonmorsels.com/what-is-an-iterator/

‎exercises/practice/acronym/.approaches/regex-join/content.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ This means that `re.finditer()` produces matches _on demand_ instead of saving t
5454

5555
The regular expression `r[a-zA-Z']+` in the code example looks for any single character in the range `a-z` lowercase and `A-Z` uppercase, plus the `'` (_apostrophe_) character.
5656
The `+` operator is a 'greedy' modifier that matches the previous range one to unlimited times.
57-
This means that the expression will match any collection or repeat of letters (_word_), but will omit matching on any sort of space or 'non-letter' character, such as `\t`, `\n`, ` `, `_`, or `-`.
57+
This means that the expression will match any collection or repeat of letters (_a word_), but will not match any sort of space or 'non-letter' character, such as a tab, space, hyphen, or underscore.
5858

5959
For example, in `Complementary metal-oxide semiconductor`, the regex will match `Complementary`, `metal`, `oxide`, and `semiconductor`.
6060
The regex will not match on ` ` or `-`.
@@ -78,9 +78,9 @@ This generator expression is consumed by [`str.join()`][str-join], which joins t
7878
Other "separator" strings can be used with `str.join()` — see [concept:python/string-methods]() for some additional examples.
7979

8080

81-
Finally, the result of `.join()` is capitalized using the [chained][chaining] [`.upper()`][str-upper].
82-
Alternatively, `.upper()` can be used on `to_abbreviate` within `findall()`/`finditer()`, to uppercase the input before cleaning.
83-
Since the generator expression + join + upper is fairly succinct, they can be placed directly on the `return` line rather than assigning and returning an intermediate variable for the acronym.
81+
Finally, the result of `.join()` is capitalized using the [chained][chaining] [`str.upper()`][str-upper].
82+
Alternatively, `str.upper()` can be used on `to_abbreviate` within `findall()`/`finditer()`, to uppercase the input before cleaning.
83+
Since the solution is fairly succinct, it can be condensed onto the `return` line, rather than assigning and returning an intermediate variable for the acronym.
8484

8585

8686
This approach was less performant in benchmarks than those using `loop`, `map`, `list-comprehension`, and `reduce`.

‎exercises/practice/acronym/.approaches/regex-sub/content.md‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def abbreviate_regex_sub(to_abbreviate):
1616
return re.sub(r"(?<!_)\B[\w']+|[ ,\-_]", "", to_abbreviate.upper())
1717
```
1818

19-
This approach begins by using the [`re.sub()`][re-sub] method from the [`re` module][re-module] to remove unwanted characters such as `,`, `-`, `_`, whitespace, and all but the first letters of each word from `to_abbreviate`.
19+
This approach begins by using the [`re.sub()`][re-sub] method from the [`re` module][re-module] to remove unwanted characters such as spaces, commas, hyphens, underscores, and all but the first letters of each word from `to_abbreviate`.
2020
Python's `re` module provides support for [regular expressions][regular expressions] within the language, and has many useful methods for searching, parsing, and modifying text.
2121

2222

@@ -28,7 +28,7 @@ While it is a fun experiment to see if the entire problem can be more or less so
2828
This approach was one of the slowest solutions in benchmarking, taking 652 steps in the regex engine to find and replace 82 matches.
2929
3030
31-
A more performant method of cleaning would be to use [`re.findall()`][re-findall] or [`re.finditer()`][re-finditer] to clean the phrase of unwanted characters, and then process the results with a `list-comprehension` or `loop` to extract the first letters of words.
31+
A more performant method of cleaning would be to use [`re.findall()`][re-findall] or [`re.finditer()`][re-finditer] to clean `to_abbreviate` of unwanted characters, and then process the results with a `list-comprehension` or `loop` to extract the first letters of words.
3232
`to_abbreviate.replace("-", " ").replace("_", " ").upper().split()` can also be used, and is even more performant here for cleaning test inputs.
3333
3434
@@ -46,21 +46,21 @@ For convenience and reuse, the regex is compiled using [`re.compile()`][re-compi
4646
Alternatives are seperated with the pipe (`|`) symbol:
4747

4848

49-
1. `(?<!_)` is a [negative lookbehind][negative lookbehind], which ensures that `_` followed by letter characters (_see the pattern explanation below_) is **not** matched (_for example, `_none` is **not** matched, but ` _` with a preceding space **is** matched_).
50-
2. `\B[\w']+`, which starts searching at a [non-word boundary][re-non-word boundary], looks for any character in the group `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_'`.
51-
The `+` operator is a 'greedy' modifier that matches a character in the previous group one to unlimited times.
52-
This means that this expression will match any collection or repeat of the letters (_plus `'`_), but will not match on anything else.
53-
3. `[ ,\-_]` matches any of the characters ` -_,` (_space, hyphen, underscore, comma_) once.
49+
1. `(?<!_)` is a [negative lookbehind][negative lookbehind], which ensures that `_` followed by letter characters (_see the pattern explanation below_) is **not** matched (_for example, `"_none"` is **not** matched, but `" _"` with a preceding space **is** matched_).
50+
2. `\B[\w']+`, which starts searching at a [non-word boundary][re-non-word boundary], looks for any character that is a letter, number, underscore, or apostrophe.
51+
The `+` operator is a 'greedy' modifier that matches a character in the previous group one or more (unlimited) times.
52+
This means that this expression will match any collection or repeat of alphanumeric characters (_plus `_` and `'`_), but will not match anything else.
53+
3. `[ ,\-_]` matches any exactly one space, comma, hyphen, or underscore.
5454

5555

56-
Because these matches are used in the `re.sub()` method, an empty string is _substituted_ — so the matches are _removed_ from the result.
56+
Because these matches are used in the `re.sub()` method, each match is _substituted_ with an empty string — so the matches are _removed_ from the result.
5757

5858

59-
As an example, for the input phrase `The Road _Not_ Taken`, the regex will match `he`, ` `, `oad`, ` `, `-`, `ot`, `-`, ` `, and `aken`, replacing each match with "".
60-
The result is the string `TRNT`.
59+
As an example, for the input phrase `"The Road _Not_ Taken"`, the regex will match `"he"`, `" "`, `"oad"`, `" "`, `"_"`, `"ot_"`, `" "`, and `"aken"`, replacing each match with `""`.
60+
The result is the string `"TRNT"`.
6161

6262

63-
To ensure that all results are capitalized for any input, the approach then [chains][chaining] [`.upper()`][str-upper] to `re.sub()` on the `return` line to produce the final acronym.
63+
To ensure that all results are capitalized for any input, the approach then [chains][chaining] [`str.upper()`][str-upper] to `re.sub()` on the `return` line to produce the final acronym.
6464

6565
To play with this regex and see a more in-depth explanation, you can use it on [regex101][regex101].
6666

0 commit comments

Comments
 (0)