Start with the following Python code.
alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]
# From Section 11.2 of:
# Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree Press.
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
def has_duplicates(s):
d=histogram(s)
for i in d.values():
if i!=1:
return True
return False
def missing_letters(s):
missing_letters_string=""
d=histogram(s)
for ch in alphabet:
if ch not in d.keys():
missing_letters_strings+=ch
return missing_letters_string
for s in test_dups:
if has_duplicates(s):
print(s+"has duplicates")
else:
print(s+"has no duplicates")
print("\n")
for s in test_miss:
missing_letters_string=missing_letters(s)
if missing_letters_string!="":
pring(s+" is missing letters "+missing_letters_string)
else:
print(s+" uses all the letters")
print("\n")
Comments
Leave a comment