Answer to Question #253877 in Python for Samad

Question #253877

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 



1
Expert's answer
2021-10-20T07:39:23-0400
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")

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS