Answer to Question #252368 in Python for Raymond Ekwubiri

Question #252368
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.

Implement has_duplicates by creating a histogram using the histogram function above. Do not use any of the implementations of has_duplicates that are given in your textbook. Instead, your implementation should use the counts in the histogram to decide if there are any duplicates.

Write a loop over the strings in the provided test_dups list. Print each string in the list and whether or not it has any duplicates based on the return value of has_duplicates for that string. For example, the output for "aaa" and "abc" would be the following.

aaa has duplicates
abc has no duplicates

Print a line like one of the above for each of the strings in test_dups.
1
Expert's answer
2021-10-19T02:05:53-0400
alphabet = 'abcdefghijklmnopqrstuvwxyz'


test_miss = ["aaa", "abcdddefrr"]
test_dups = ["aabbcc", "abcdd"]


def histogram(s):
    hist = []
    for i in alphabet:
        if not((i in s) or (i.upper() in s)):
            hist.append(i)
    return hist


def has_dups(ss):
    t = set()
    test = True
    for i in ss: t.add(i)
    for i in t:
        if ss.count(i)>1:
            test = False
            print(f"There are (is) {ss.count(i)-1} duplicate(s) of {i}")
    if test:
        print("There are not duplicates in this string")
            


def missing_letters(foo):
    ans = histogram(foo)
    ans.sort()
    ans = ''.join(ans)
    if len(ans)==0:
        print("It uses all the letters")
    else:
        print(ans)


for i in test_dups:
    print("###Calling the function test_dups###")
    has_dups(i)
    print()


for i in test_miss:
    print("###Calling the function test_miss###")
    missing_letters(i)
    print()

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