Answer to Question #100873 in Python for Olayemi Adigun

Question #100873
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"]


def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d

The function has_duplicates should take a string parameter and return True if the string has repeated characters. Otherwise, it should return False.
Use has_duplicates by creating a histogram using the histogram function above.
Write a loop over the strings in test_dups list.
write a function missing_letters that takes a string parameter and returns a new string with all the letters of the alphabet that aren't in the argument string. The letters in the returned string should be in alphabetical order
1
Expert's answer
2020-01-07T12:29:35-0500
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

def has_duplicates(s):
    for v in histogram(s).values():
        if v > 1:
            return True
    return False

def test_dups_loop():
    for s in test_dups:
        print(s + ':', has_duplicates(s))

def missing_letters(s):
    r = list('abcdefghijklmnopqrstuvwxyz')
    s = s.lower()
    for c in s.lower():
        if c in r:
            r.remove(c)  # the first matching instance
    return ''.join(r)

def test_miss_loop():
    for s in test_miss:
        print(s + ':', missing_letters(s))

def main():
    test_dups_loop()
    test_miss_loop()

if __name__ == '__main__':
    main()

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