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.

Respuesta :

Answer:

Explanation:

The python code for the question is attached in the image below.

The objective here is to write a code in python with 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.

SEE BELOW FOR THE 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

def has_duplicates():

   for string in test_dups:

       dictionary=histogram(string)

       duplicates=False

       for ch in dictionary.keys():

           if dictionary[ch] > 1:

               duplicates=True

               break

       if duplicates==True:

           print(string,"has duplicates")

       else:

           print(string,"has no duplicates")

def missing_letters(string):

   answer=""

   for ch in alphabet:

       if ch not in string:

           answer=answer+ch

   return answer

print("__________________________________________")

print(" Calling has_duplicates() function")

print("__________________________________________")

has_duplicates()

print("\n______________________________________________")

print("Calling missing_letters() function in for loop")

print("______________________________________________")

for i in test_miss:

   answer=missing_letters(i)

   if len(answer)>=1:

       print(i,"is missing letters",answer)

   else:

       print(i,"uses all the letters")

Ver imagen ajeigbeibraheem

The program illustrates the use of lists and iterations.

Lists are used to hold multiple values in one variable, while iterations are used for repetitive operations

The has_duplicates function written in Python, where comments are used to explain each line is as follows:

#This initializes the test_dups list

test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]

   

#This defines the histogram function

def histogram(myStr):

   #This creates an empty dictionary

   myDict = dict()

   #This iterates through the string

   for c in myStr:

       #This counts the number of unique strings in the string

       if c not in myStr:

           myDict[c]=1

       else:

           myDict[c]+=1

   #This returns the dictionary that contains the strings and their frequencies

   return myDict

#This defines the has_duplicates function

def has_duplicates():

   #This iterates through the strings in test_dups

   for myStr in test_dups:

       #This calls the histogram function

       dictionary=histogram(myStr)

       #This initializes chk to 0

       chk = 0

       #This iterates through the dictionary keys

       for ch in dictionary.keys():

           #For keys greater than 1,

           if dictionary[ch] > 1:

               #chk is set to 1 and the loop is exited

               chk = 1

               break

           #This prints if the string has duplicates or not

           if chk ==1:

               print(myStr,"has duplicates")

           else:

               print(myStr,"has no duplicates")

   

has_duplicates()

Read more about lists and iterations at:

https://brainly.com/question/18917298

ACCESS MORE
EDU ACCESS