|
| 1 | +# Introduction |
| 2 | + |
| 3 | +There are multiple Pythonic ways to solve the Acronym exercise. |
| 4 | +Among them are: |
| 5 | + |
| 6 | +- Using `str.replace()` to scrub the input, and: |
| 7 | + - joining with a `for loop` with string concatenation via the `+` operator. |
| 8 | + - joining via `str.join()`, passing a `list-comprehension` or `generator-expression`. |
| 9 | + - joining via `str.join()`, passing `map()`. |
| 10 | + - joining via `functools.reduce()`. |
| 11 | + |
| 12 | +- Using `re.findall()`/`re.finditer()` to scrub the input, and: |
| 13 | + - joining via `str.join()`, passing a `generator-expression`. |
| 14 | + |
| 15 | + - Using `re.sub()` for both cleaning and joining (_using "only" regex for almost everything_)` |
| 16 | + |
| 17 | + |
| 18 | +## General Guidance |
| 19 | + |
| 20 | +The goal of the Acronym exercise is to collect the first letters of each word in the input phrase and return them as a single capitalized string (_the acronym_). |
| 21 | +The challenge is to efficiently identify and capitalize the first letters while removing or ignoring non-letter characters such as `'`,`-`,`_`, and white space. |
| 22 | + |
| 23 | + |
| 24 | +There are two idiomatic strategies for non-letter character removal: |
| 25 | +- Python's built-in [`str.replace()`][str-replace]. |
| 26 | +- The [`re`][re] module, (_regular expressions_). |
| 27 | + |
| 28 | +For all but the most complex scenarios, using `str.replace()` is generally more efficient than using a regular expression. |
| 29 | + |
| 30 | + |
| 31 | +Forming the final acronym is most easily done with a direct or indirect `loop`, after splitting the input into a word list via [`str.split()`][str-split]. |
| 32 | +The majority of these approaches demonstrate alternatives to the "classic" looping structure using various other iteration techniques. |
| 33 | +Some `regex` methods can avoid looping altogether, although they can become very non-performant due to excessive backtracking. |
| 34 | + |
| 35 | +Strings are _immutable_, so any method to produce an acronym will be creating and returning a new `str`. |
| 36 | + |
| 37 | + |
| 38 | +## Approach: scrub with `replace()` and join via `for` loop |
| 39 | + |
| 40 | +```python |
| 41 | +def abbreviate(to_abbreviate): |
| 42 | + phrase = to_abbreviate.replace('-', ' ').replace('_', ' ').upper().split() |
| 43 | + acronym = '' |
| 44 | + |
| 45 | + for word in phrase: |
| 46 | + acronym += word[0] |
| 47 | + |
| 48 | + return acronym |
| 49 | +``` |
| 50 | + |
| 51 | +For more information, take a look at the [loop approach][approach-loop]. |
| 52 | + |
| 53 | + |
| 54 | +## Approach: scrub with `replace()` and join via `list comprehension` or `Generator expression` |
| 55 | + |
| 56 | + |
| 57 | +```python |
| 58 | +def abbreviate(to_abbreviate): |
| 59 | + phrase = to_abbreviate.replace('-', ' ').replace('_', ' ').upper().split() |
| 60 | + |
| 61 | + return ''.join([word[0] for word in phrase]) |
| 62 | + |
| 63 | +###OR### |
| 64 | + |
| 65 | +def abbreviate(to_abbreviate): |
| 66 | + phrase = to_abbreviate.replace('-', ' ').replace('_', ' ').upper().split() |
| 67 | + |
| 68 | + # note the parenthesis instead of square brackets. |
| 69 | + return ''.join((word[0] for word in phrase)) |
| 70 | +``` |
| 71 | + |
| 72 | +For more information, check out the [list-comprehension][approach-list-comprehension] approach or the [generator-expression][approach-generator-expression] approach. |
| 73 | + |
| 74 | + |
| 75 | +## Approach: scrub with `replace()` and join via `map()` |
| 76 | + |
| 77 | +```python |
| 78 | +def abbreviate(to_abbreviate): |
| 79 | + phrase = to_abbreviate.replace("_", " ").replace("-", " ").upper().split() |
| 80 | + |
| 81 | + return ''.join(map(lambda word: word[0], phrase)) |
| 82 | +``` |
| 83 | + |
| 84 | +For more information, read the [map][approach-map-function] approach. |
| 85 | + |
| 86 | + |
| 87 | +## Approach: scrub with `replace()` and join via `functools.reduce()` |
| 88 | + |
| 89 | +```python |
| 90 | +from functools import reduce |
| 91 | + |
| 92 | + |
| 93 | +def abbreviate(to_abbreviate): |
| 94 | + phrase = to_abbreviate.replace("_", " ").replace("-", " ").upper().split() |
| 95 | + |
| 96 | + return reduce(lambda start, word: start + word[0], phrase, "") |
| 97 | +``` |
| 98 | + |
| 99 | +For more information, take a look at the [functools.reduce()][approach-functools-reduce] approach. |
| 100 | + |
| 101 | + |
| 102 | +## Approach: filter with `re.findall()` and join via `str.join()` |
| 103 | + |
| 104 | +```python |
| 105 | +import re |
| 106 | + |
| 107 | + |
| 108 | +def abbreviate(phrase): |
| 109 | + removed = re.findall(r"[a-zA-Z']+", phrase) |
| 110 | + |
| 111 | + return ''.join(word[0] for word in removed).upper() |
| 112 | +``` |
| 113 | + |
| 114 | +For more information, take a look at the [regex-join][approach-regex-join] approach. |
| 115 | + |
| 116 | + |
| 117 | +## Approach: use `re.sub()` |
| 118 | + |
| 119 | +```python |
| 120 | +import re |
| 121 | + |
| 122 | + |
| 123 | +def abbreviate_regex_sub(to_abbreviate): |
| 124 | + pattern = re.compile(r"(?<!_)\B[\w']+|[ ,\-_]") |
| 125 | + |
| 126 | + return re.sub(pattern, "", to_abbreviate.upper()) |
| 127 | +``` |
| 128 | + |
| 129 | +For more information, read the [regex-sub][approach-regex-sub] approach. |
| 130 | + |
| 131 | + |
| 132 | +## Other approaches |
| 133 | + |
| 134 | +Besides these seven idiomatic approaches, there are a multitude of possible variations using different string cleaning and joining methods. |
| 135 | + |
| 136 | +However, these listed approaches cover the majority of 'mainstream' strategies. |
| 137 | + |
| 138 | + |
| 139 | +## Which approach to use? |
| 140 | + |
| 141 | +All seven approaches are idiomatic, and show multiple paradigms and possibilities. |
| 142 | +All approaches are also `O(n)`, with `n` being the length of the input string. |
| 143 | +No matter the removal method, the entire input string must be iterated through to be cleaned and the first letters extracted. |
| 144 | + |
| 145 | +Of these strategies, the `loop` approach is the fastest, although `list-comprehension`, `map`, and `reduce` have near-identical performance for the test data. |
| 146 | +All approaches are fairly succinct and readable, although the 'classic' loop is probably the easiest understood by those coming to Python from other programming languages. |
| 147 | + |
| 148 | + |
| 149 | +The least performant for the test data was using a `generator-expression`, `re.findall` and `re.sub` (_least performant_). |
| 150 | + |
| 151 | +To compare performance of the approaches, take a look at the [Performance article][article-performance]. |
| 152 | + |
| 153 | +[approach-functools-reduce]: https://exercism.org/tracks/python/exercises/acronym/approaches/functools-reduce |
| 154 | +[approach-generator-expression]: https://exercism.org/tracks/python/exercises/acronym/approaches/generator-expression |
| 155 | +[approach-list-comprehension]: https://exercism.org/tracks/python/exercises/acronym/approaches/list-comprehension |
| 156 | +[approach-loop]: https://exercism.org/tracks/python/exercises/acronym/approaches/loop |
| 157 | +[approach-map-function]: https://exercism.org/tracks/python/exercises/acronym/approaches/map-function |
| 158 | +[approach-regex-join]: https://exercism.org/tracks/python/exercises/acronym/approaches/regex-join |
| 159 | +[approach-regex-sub]: https://exercism.org/tracks/python/exercises/acronym/approaches/regex-sub |
| 160 | +[article-performance]: https://exercism.org/tracks/python/exercises/isogram/articles/performance |
0 commit comments