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
{{ message }}
Repository navigation
Commit afbd8ad
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: exercises/practice/acronym/.approaches/functools-reduce/content.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,10 +38,10 @@ The application of the function travels from left to right, so that the iterable
38
38
39
39
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`.
40
40
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.
42
42
43
43
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.
45
45
46
46
47
47
In benchmarks, this solution performed about as well as both the `loops` and the `list-comprehension` solutions.
Copy file name to clipboardExpand all lines: exercises/practice/acronym/.approaches/generator-expression/content.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,7 @@ A [`generator-expression`][generator-expression] is then used to iterate through
32
32
Generator expressions are short-form [generators][generators] — lazy iterators that produce their values _on demand_, instead of saving them to memory.
33
33
This generator expression is consumed by [`str.join()`][str-join], which joins the generated letters together using an empty string.
34
34
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.
36
36
37
37
38
38
In benchmarks, this solution was surprisingly slower than the `list comprehension` version.
Copy file name to clipboardExpand all lines: exercises/practice/acronym/.approaches/list-comprehension/content.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ As of this writing, both of these methods benchmark slower than using `str.repla
27
27
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].
28
28
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.
29
29
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.
31
31
32
32
33
33
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.
Copy file name to clipboardExpand all lines: exercises/practice/acronym/.approaches/map-function/content.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,15 +30,15 @@ Once the phrase is cleaned and turned into a word `list`, the acronym is created
30
30
The application of the function travels from left to right, and function results are produced as needed.
31
31
32
32
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.
34
34
`word[0]` is the function, which extracts the letter at index zero for every word in the phrase list.
35
35
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.
37
37
38
38
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.
40
40
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.
Copy file name to clipboardExpand all lines: exercises/practice/acronym/.approaches/regex-join/content.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,7 @@ This means that `re.finditer()` produces matches _on demand_ instead of saving t
54
54
55
55
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.
56
56
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.
58
58
59
59
For example, in `Complementary metal-oxide semiconductor`, the regex will match `Complementary`, `metal`, `oxide`, and `semiconductor`.
60
60
The regex will not match on `` or `-`.
@@ -78,9 +78,9 @@ This generator expression is consumed by [`str.join()`][str-join], which joins t
78
78
Other "separator" strings can be used with `str.join()` — see [concept:python/string-methods]() for some additional examples.
79
79
80
80
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.
84
84
85
85
86
86
This approach was less performant in benchmarks than those using `loop`, `map`, `list-comprehension`, and `reduce`.
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`.
20
20
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.
21
21
22
22
@@ -28,7 +28,7 @@ While it is a fun experiment to see if the entire problem can be more or less so
28
28
This approach was one of the slowest solutions in benchmarking, taking 652 steps in the regex engine to find and replace 82 matches.
29
29
30
30
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.
32
32
`to_abbreviate.replace("-", " ").replace("_", " ").upper().split()` can also be used, and is even more performant here for cleaning test inputs.
33
33
34
34
@@ -46,21 +46,21 @@ For convenience and reuse, the regex is compiled using [`re.compile()`][re-compi
46
46
Alternatives are seperated with the pipe (`|`) symbol:
47
47
48
48
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.
54
54
55
55
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.
57
57
58
58
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"`.
61
61
62
62
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.
64
64
65
65
To play with this regex and see a more in-depth explanation, you can use it on [regex101][regex101].
0 commit comments