Still being new to Python, I had to create a function that checks for doubles and if doubles are found it should return 'has duplicates'. So I have already finished the code correctly, but I'm more confused about why it originally found "bookkeeper" to have no duplicates with the code below.
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(string):
x = histogram(string)
for b, c in x.items():
if c > 1:
return True
else:
return False
for string in test_dups:
if has_duplicates(string):
print(string, "has duplicates")
else:
print(string, "has no duplicates")
Output:
zzz has duplicates
dog has no duplicates
bookkeeper has no duplicates
subdermatoglyphic has no duplicates
subdermatoglyphics has duplicates
This is what I changed to make it work. But I would really like to understand why 'bookkeeper' didn't test correctly.
def has_duplicates(string):
x = histogram(string)
for b, c in x.items():
if c > 1:
return True
else:
return False