python workout: exercise 8
sorting a string
problem
In this exercise, you’ll explore this idea by writing a function,
strsort, that takes a single string as its input and returns a string. The returned string should contain the same characters as the input, except that its characters should be sorted in order, from the lowest Unicode value to the highest Unicode value. For example, the result of invokingstrsort('cba')will be the stringabc.
attempts
The first thing that comes to mind is exploiting the hint/attribute that we are
sorting by Unicode value. Because this tells use that we can reuse ord to get
the integer value of the Unicode of a character in our string.
So, my solution is to just:
- map
ordover a string - sort the resultant list
- map over the sorted list with
chr - join the resultant characters
It would look something like:
def strsort(string: str) -> str:
return ''.join(map(chr, sorted(map(ord, string))))
print(strsort('cba'))
abc
It works!
Now…
Is it too functional? Not pythonic enough?
Maybe. But everyone has their preferences. And I think this is mine – it’s a short, sweet series of transformations.
solution
The book’s implementation:
def strsort(a_string):
return ''.join(sorted(a_string))
print(strsort('cbjeaf'))
abcefj
🤦
A classic example of overthinking…
The sorted function could obviously just as on the string directly, bypassing
the need for the explicit use of ord and chr.
We got fixated on Unicode to the extent of forgetting sorted did just that :(
beyond the exercise
sorting words
-
problem
Given the string “Tom Dick Harry,” break it into individual words, and then sort those words alphabetically. Once they’re sorted, print them with commas ( ,) between the names.
-
attempts
This should be straightforward.
The
sortedfunction can sort strings and not just characters. So it’s just a question of splitting the string into a list, sorting, then rejoining them:def strsort(string: str) -> str: return ','.join(sorted(string.split())) print(strsort('Tom Dick Harry'))Dick,Harry,Tom
the last word in a text file
-
problem
Which is the last word, alphabetically, in a text file?
-
attempts
I’m pretty sure we can use
minandmaxhere – we don’t need to sort all the words in a file.And for simplicity’s sake, let’s just assume our function takes a the text in a file as a single string. So we don’t have to deal with opeining a file at a path:
def last_word(text: str) -> str: return max(text.split()) text = """Voluptate aut reprehenderit sint. Saepe aut dolores qui dolor quibusdam iusto. Sequi sunt aliquid quos ex id veritatis nesciunt modi. Facilis numquam omnis nesciunt non aut sit non est. Aspernatur et excepturi enim. Ad doloremque aliquid rem voluptatem.""" print(last_word(text))voluptatem.(Note that we use
maxbecause strings are sorted in alphabetical order by default.)
longest word
-
problem
Which is the longest word in a text file?
-
attempts
The first thing option that comes to mind is to iterate through each word and maintain a record of the maximum word length seen so far and its associated word.
This approach also begs the question: what should we do in the case of multiple longest words?
Another option is to use
maxagain but passlenas thekeyargument:def longest_word(text: str) -> str: return max(text.split(), key=len) text = """Voluptate aut reprehenderit sint. Saepe aut dolores qui dolor quibusdam iusto. Sequi sunt aliquid quos ex id veritatis nesciunt modi. Facilis numquam omnis nesciunt non aut sit non est. Aspernatur et excepturi enim. Ad doloremque aliquid rem voluptatem.""" print(longest_word(text))reprehenderitThis doesn’t solve our “multiple longest words” problem. But let’s just assume it’s sufficient to return a longest word and not all the longest words.