0% found this document useful (0 votes)
27 views

Programming Assignment Unit 7

The document defines functions to analyze strings: has_duplicates checks for repeated characters, returning True if any are repeated; missing_letters finds characters in the alphabet that do not appear in the string. These functions are used to analyze sample strings, printing whether strings have duplicates, or are missing letters, and if so which letters.

Uploaded by

kingluke1990
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Programming Assignment Unit 7

The document defines functions to analyze strings: has_duplicates checks for repeated characters, returning True if any are repeated; missing_letters finds characters in the alphabet that do not appear in the string. These functions are used to analyze sample strings, printing whether strings have duplicates, or are missing letters, and if so which letters.

Uploaded by

kingluke1990
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

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
def has_duplicates(s):
h=histogram(s)
for c in h.values():
if c>1:
return True
else:
return False
for s in test_dups:
if has_duplicates(s):
print(s,'has duplicates')
else:
print(s,'has no duplicates')

Output:
zzz has duplicates
dog has no duplicates
bookkeeper has no duplicates
subdermatoglyphic has no duplicates
subdermatoglyphics has duplicates

#Part 2
def missing_letters(s):
h=histogram(s)
my_list=[]
for x in alphabet:
if x not in h:
my_list.append(x)
return ''.join(my_list)

for b in test_miss:
if len(missing_letters(b)):
print(b,'is missing letters',missing_letters(b))
else:
print(b,'uses all the letters')

Output:
zzz is missing letters abcdefghijklmnopqrstuvwxy
subdermatoglyphic is missing letters fjknqvwxz
the quick brown fox jumps over the lazy dog uses all the letters

You might also like