Let’s say we’re pioneering a language and don’t have the range of built-ins such as str.isalpha()
or str.count()
. That changes the landscape.
We could write our own isalpha()
as a helper function.
def isalpha(s):
return s in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”
and,
def addunique(c, s):
return s + c if c not in s else s
then implement them,
def unique_english_letters(word):
uniques = ''
for char in word:
if not isalpha(char): raise ValueError
uniques = addunique(char, uniques)
return uniques, len(uniques), len(word)
of which we only need the middle value to satisfy the lesson, but, hey?
w = 'supercalifragilisticexpialidocious'
print (unique_english_letters(w))
('supercalifgtxdo', 15, 34)
Were there a thousand letters the first element above would be the labels of the histogram, or frequency chart. The second value is the length of that string, and the final the length of the string we handed in.
w = 'quickbrownfoxjumpsoverthelazydog'
print (unique_english_letters(w))
('quickbrownfxjmpsvethlazydg', 26, 32)
if not isalpha(char): raise ValueError
can be set to less lenient and just ignore the character.
if not isalpha(char): continue
which will allow,
w = 'quick brown fox jumps over the lazy dog'
print (unique_english_letters(w))
('quickbrownfxjmpsvethlazydg', 26, 39)
This brings us to helper functions that are dedicated to one function. In that case we may as well roll them into the function and not clutter up the global namespace.
def unique_english_letters(word):
def isalpha(s):
return s in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
def addunique(c, s):
return s + c if c not in s else s
uniques = ''
for char in word:
if not isalpha(char): continue
uniques = addunique(char, uniques)
return uniques, len(uniques), len(w)