from functools import reduce
def abbreviate(to_abbreviate):
phrase = to_abbreviate.replace("-", " ").replace("_", " ").upper().split()
return reduce(lambda start, word: start + word[0], phrase, "")
- This approach begins by using
str.replace()onto_abbreviateto convert non-letter characters such as-and_into spaces. - The phrase is then upper-cased by calling
str.upper(). - Finally, the phrase is turned into a
listof words by callingstr.split().
The three methods above are all chained together, with each method operating on the output of the method before it in the "chain".
This works because both replace() and upper() operate on strings (as they are str methods) and return strings.
If split() was called first, replace() and upper() would fail, since they cannot operate on the list returned by split().
re.findall() or re.finditer() can also be used to clean to_abbreviate.
These two methods from the re module will return a list or a lazy iterator of results, respectively.
As of this writing, both of these methods benchmark slower than using str.replace() for cleaning.
Once the phrase is cleaned and turned into a word list, the acronym is created via reduce().
reduce() is a method from the functools module, which provides support for higher-order functions and functional programming in Python.
functools.reduce() applies an anonymous two-argument function (the lambda in the code example) to the items of an iterable.
The application of the function travels from left to right, so that the iterable becomes a single value (it is "reduced" to a single value).
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.
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'.
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.
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.
In benchmarks, this solution performed about as well as both the loops and the list-comprehension solutions.