Answer to Question #171434 in Python for Paul Carpenter

Question #171434


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




1
Expert's answer
2021-03-13T08:15:25-0500
# 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' )

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