alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
Part 1
Write a function called has_duplicates that takes a string parameter and returns True if the string has any repeated characters. Otherwise, it should return False.
aaa has duplicates
abc has no duplicates
Part 2
Write a loop over the strings in list test_miss and call missing_letters with each string. Print a line for each string listing the missing letters. For example, for the string "aaa", the output should be the following.
aaa is missing letters bcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz uses all these letters
# Part 1:
def has_duplicates (str):
count = histogram(tuple(str))
for key in count:
check = count[key]
if check > 1:
return True
return False
for e in test_dups:
if has_duplicates(e):
print(str(e)+' has duplicates')
else:
print(str(e)+' has no duplicates')
# Part 2:
def missing_letters (str):
dic_str = histogram(tuple(str))
arr = list(alphabet)
for letter in dic_str:
if letter in alphabet:
arr.remove(letter)
new_str = "".join(arr)
return new_str
for e in test_miss:
if missing_letters(e):
print(str(e)+' is missing letters '+str(missing_letters(e)))
else:
print(str(e)+ ' uses all the letters' )
Comments
Leave a comment