Answers for "Write a function definition of occurrences(text1, text2) that takes two string arguments. The function returns the number of times a character from the first argument occurs in the second argument."

0

Write a function definition of occurrences(text1, text2) that takes two string arguments. The function returns the number of times a character from the first argument occurs in the second argument.

def occurrences(text1, text2):
"""Return the number of times characters from text1 occur in text2

occurrences(string, string) -> int
"""
text1_dict = {char:0 for char in text1}

for char in text2:
    if char in text1_dict:
        text1_dict[char] += 1

return sum(list(text1_dict.values()))
Posted by: Guest on March-23-2021

Code answers related to "Write a function definition of occurrences(text1, text2) that takes two string arguments. The function returns the number of times a character from the first argument occurs in the second argument."

Browse Popular Code Answers by Language