Programming Assignment Unit 7
Programming Assignment Unit 7
alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]
#The below code turns the lists into strings that can go through the histogram program
test_dups = ''.join(test_dups)
test_miss = ''.join(test_miss)
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
#takes the histogram from above and returns true if any of the values are duplicated
def has_duplicates(s):
histo=histogram(s)
if v > 1:
return True
else:
return False
#prints has duplicates if there are any duplicates and prints does not have duplicates if there are no
#duplicates
def test_duplicates(s):
if has_duplicates(s)==True:
OUTPUT
>>> test_duplicates(alphabet)
>>> test_duplicates(test_dups)
>>> test_duplicates(test_miss)
zzzsubdermatoglyphicthe quick brown fox jumps over the lazy dog has duplicates
Part 2
alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]
#The below code turns the lists into strings that can go through the histogram program
test_dups = ''.join(test_dups)
test_miss = ''.join(test_miss)
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
def missing_letters(s):
histo=histogram(s)
absent_let=[]
for l in alphabet:
if l not in histo:
absent_let.append(l)
return ''.join(absent_let)
def has_letters(s):
if len(missing_letters(s))<1:
elif len(missing_letters(s))>=1:
OUTPUT
>>> has_letters(test_dups)
>>> has_letters(test_miss)
zzzsubdermatoglyphicthe quick brown fox jumps over the lazy dog has all the letters of the alphabet
>>> has_letters(alphabet)
References
Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree
Press.
Tyler David(1966, September 1). Checking for duplicate letters within lists by using a histogram function. Stack
Overflow. Retrieved March 15, 2023, from
GeeksforGeeks. (2023, February 27). Python - accessing items in lists within dictionary. GeeksforGeeks.
Retrieved March 15, 2023, from https://www.geeksforgeeks.org/python-accessing-items-in-lists-within-
dictionary/