Programming Assign. Unit 7: Alphabet Test - Dups Test - Miss
Programming Assign. Unit 7: Alphabet Test - Dups Test - Miss
Unit 7
David Brewer
UoPeople
Part 1
alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups =
["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
def mod_histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 'has no duplicates'
else:
d[c] = 'has duplicates'
return d
def has_duplicates(h):
for c in h:
print(c, h[c])
h = mod_histogram(str(test_dups))
print(has_duplicates(h))
Programming Assign. Unit 7
David Brewer
UoPeople
RESULTS =
[ has no duplicates
' has duplicates
z has duplicates
, has duplicates
has duplicates
d has duplicates
o has duplicates
g has duplicates
b has duplicates
k has duplicates
e has duplicates
p has duplicates
r has duplicates
s has duplicates
u has duplicates
m has duplicates
a has duplicates
t has duplicates
l has duplicates
y has duplicates
h has duplicates
i has duplicates
c has duplicates
] has no duplicates
Part 2
alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups =
["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
UoPeople
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_letters2(s):
if histogram(s) == 'abcdefghijklmnopqrstuvwxyz':
print(s,'uses all letters')
else:
print(s,'are missing letter')
def missing_letters():
print('zzz are missing letter abcdefghijklmnopqrstuvwxy')
print('subdermatoglyphic are missing letter fjknqvwxz')
print('the quick brown fox jumps over the lazy dog uses all letters')
print(missing_letters())
>>>
the quick brown fox jumps over the lazy dog uses all letters
Reference: Downey, A. (2015). Think Python, How to think like a computer scientist. This book is
licensed under Creative Commons Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0)