CONTINUATION......
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
Implement has_duplicates by creating a histogram using the histogram function above. 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.
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
has_duplicates(d)
return d
def has_duplicates(d):
Flag=0
k = list(d.keys())
for r in range(0,len(k)):
if(d[k[r]]>1): Flag=1
return(Flag)
alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog."]
test_new = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog.","zzz"]
x = histogram(alphabet)
y = has_duplicates(x)
if(y==1): print("\nThe string < %s > has duplicates"%alphabet)
if(y==0): print("\nThe string < %s > has no duplicates"%alphabet)
x = histogram(test_dups)
y = has_duplicates(x)
if(y==1): print("\nThe string < %s > has duplicates"%test_dups)
if(y==0): print("\nThe string < %s > has no duplicates"%test_dups)
x = histogram(test_miss)
y = has_duplicates(x)
if(y==1): print("\nThe string < %s > has duplicates"%test_miss)
if(y==0): print("\nThe string < %s > has no duplicates"%test_miss)
x = histogram(test_new)
y = has_duplicates(x)
if(y==1): print("\nThe string < %s > has duplicates"%test_new)
if(y==0): print("\nThe string < %s > has no duplicates"%test_new)
Comments
Leave a comment