This exercise has many potential solutions and many paths you can take along the way. No path is manifestly "better" than another, although a particular path may be more interesting or better suited to what you want to learn or explore right now. Some paths may trade speed for clarity, others might take up more memory but be more scalable or maintainable. We encourage you to try out more than one strategy to see what happens.
- Python has a robust set of tools to work with strings.
str.split
str.replace
str.lower
andstr.strip
can be particularly helpful with this challenge. - String methods can be chained together (as long as the method returns a
str
)) - While
str.split()
is very specific,str.strip()
behaves differently, and allows multiple combinations. - The
string
module (as opposed tostr
) has some constants that can be useful for filtering and comparison when processing strings.
- Dictionaries can be helpful for tabulating when items (keys) appear more than once in a string.
dict.setdefault()
can help in processing when a key might be missing from a dictionary.- The Collections module implements some really useful subtypes to the core
dict
(dictionary), purpose-built to do things like tally.
- Exploring the
re
module and regular expressions can be fun, but is by no means necessary to solve this challenge. - Regex101 is very helpful for experimenting with regular expression logic.
- Both
re.sub
andre.findall
can be interesting strategies to employ.
- Comprehensions can often "flatten" loops where items are being appended to a list or inserted into a dictionary.
- Generator expressions can often "stand in" for a list comprehension when an iterable is needed. Generator expressions are evaluated in a "lazy" fashion, and take up less space in memory than a corresponding list comprehension.