Concept explainers
Could you solve this Question please?
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.
Obs:Copy the code below into your program
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
Step by stepSolved in 4 steps with 2 images
- lllustrating your answer using the buggy code below, explain in your own words what it means for variables to be defined “as locally as possible”, and why it is important. This code is intended to print one of two messages depending on whether an integer input is less than a secret number or not. The function, inputInt, correctly prints the given String and then returns a number input by the user.arrow_forwardComplete the function ratio_similarity, which computes the ratio of "similarity" between two non-empty strings. The ratio is computed as follows: 2 (the number of matches of characters) / (total number of characters in both strings) The matches are determined in pairwise fashion. For example, consider the strings abcdef and abdce. The three characters at indexes 0, 1 and 4 match. Therefore, the similarity ratio is 2 * 3/(6 + 5) = 0.5454 .... Examples: Function Call string_similarity('abcdef', 'abdce') string_similarity('dragon', 'flagon') string_similarity('stony', 'stony') string_similarity('Stony Brook University', 'WolfieNet Secure') string_similarity('yabba dabba doo', 'bippity boppity boo') string_similarity('Wolfie the Seawolf', "What's...a Seawolf?") [ ] Return Value 3 4 5 # Test cases 6 print (string_similarity('abcdef', 'abdce')) 7 print (string_similarity('dragon' 1 'flagon')) 'stony')) 0.5454545454545454 0.6666666666666666 1.0 0.0 0.058823529411764705 0.4864864864864865 1…arrow_forward2. Write the definition of a bool function named BooleanMode () whose header is bool BooleanMode (bool data[), int n) Given that n represents the size of data, the function returns the truth value that appears the most in data. If there is an equal number of both truth values in data, the function returns the truth value that appeared first. For instance, if data = [true, true,false, false,false, true,falsel, then the function will return false. Implarrow_forward
- write a function get_intersecting_station(line_one, line_two) where line_one and line_two are the colors of the lines. This function should return the name of the station where the two lines intersect. Recall from our simplification, there will be only one. If there is no intersecting station, return None use pythonarrow_forwardWrite the convert_word code. convert_word is known as a function, which we will cover more in depth later. You are automatically given a parameter called word, which is a string that contains a single word. The return conv is how you will send data out of your function. The conv is a variable that hasn't been created yet, but it will be a string containing the converted hey. For example, if word="hey", then conv will be "heey". You are responsible for creating the variable conv, as the template has NOT done that for you. You need to use an if statement to see if the word starts with "he" and ends with "y". Since you don't know how many e's might be between the he and y, you have to do some math here. Recall that you can get the length of a string by using len(word) in the code above. You also know that h and y will be included. so, len(word) - 2 will be the number of e's in between the h and y. Create a string that contains the number of e's. This will be your conv variable. I would…arrow_forwardWrite a program called p3.py that contains a function called reveal_recursive() that takes a word (string) and the length of the word (int) as input and has the following functionality: 1. prints the word where all characters are replaced by underscores 2. continue to print the word revealing one character at a time. i.e., the second line printed should print the first character followed by “_”’s representing the rest of the word. (see example below) 3. the function should end after printing the entire word once. 4. This function should be recursive Example: #the word is kangaroo ________ k_______ ka______ kan_____ kang____ kanga___ kangar__ kangaro_ kangarooarrow_forward
- In pythonarrow_forwardImplement a function, countVowels, which takes in a string and counts the number of vowels that are contained within it. Make sure that both capital vowels and lowercase vowels are counted. Your starter code contains a main function, which asks for user input and outputs the number of vowels. You will need to implement the user input for the string. Sample Runs (user input is italicized and underlined): Enter a phrase: Hello World!Number of vowels: 3 Enter a phrase: PROGRAMMING IS EXCELLENT.Number of vowels: 7 #include <string>#include <iostream>using namespace std; int countVowels(string word); int main(){ string phrase; cout << "Enter a phrase: "; // To do: implement user input cout << "Number of vowels: " << countVowels(phrase) << endl;} // To do: Implement countVowels int countVowels(string word){}arrow_forwardIn Kotlin, Write the function divIntSafe(a: Int, b:Int). This function should take two Ints, convert them to doubles, divide the first by the second, and return the result. Write code that creates a list of the inverses of the Ints from 1 to 10, Use a lambda expression as the initializer expression. Then do it again with divIntSafe function as the initializer expression.arrow_forward
- Write a python function called replacer() that takes two strings as arguments. Argument1: A sentence where you have to make the changes. Argument2: A word with the values either “vowel” or “consonant”.Your function should replace the characters of the sentence with “V” or “C” depending on the 2nd argument. If the word is “vowel”, then replace all the vowels with “V”. If the word is “consonant”, then replace all the consonants with “C”.[You are not allowed to use the replace() function]Function call1:replacer("It is MAT110 final", "vowels")Sample Output1:Vt Vs MVT110 fVnVlFunction call2:replacer("It is CSE110 final", "consonants")Sample Output 02:IC iC CCE110 CiCaCarrow_forwardWrite a function called monkey_experiment that takes in a goal string (e.g., "methinks it is like a weasel") and a number of iterations as an argument. Run the experiment for that many iterations, and return a tuple with the highest-scoring text that was generated along with the score that it got. Test results: >>> monkey_experiment("methinks it is like a weasel",100000) ('m ojindoebjrerloiuc m oaanvl', 8) >>> monkey_experiment("methinks it is like a weasel",100) ('yeanpqj twosisjcvqrrfu eagbi', 5) >>> monkey_experiment("brevity is the soul of wit",100000) ('nnebptorcrytbe wogl uygdsn', 8) >>> monkey_experiment("to be",100000) ('tofbe', 4) >>> monkey_experiment("to be",200000) ('tbhbe', 3) >>> monkey_experiment("to be",500000) ('eo be', 4)arrow_forwardcode should be python: write a function that allows the input of a list of strings as its only parameter. Inside the body of the function find the first palindromic word and return it. If there is no such word, it should return an empty string. A string is palindromic if it reads the same forward and backward so for example If we call the function with [“horse”, “rat”, “roar”, “dad”, “mom”], the returned value should be “dad”. Note that “mom” is also a palindrome, but it is not the first one.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education