python - Finding the number of times characters from text1 occur in text2 -


write function definition of occurrences takes 2 string arguments. function returns number of times character first argument occurs in second argument.

example:

occurrences('fooled', 'hello world')

should evaluate to:

7 (1 'e', 3 'l', 2 'o', 1 'd')

my code here:

def occurrences(text1, text2): """return number of times characters text1 occur in text2  occurrences(string, string) -> int """ # add code here ss=0 c in set(text2):     if c in text1:         return text1.count(text2)  return ss 

it says that: loop should iterate on text2 wrong: strings 'tc1h' , 'return number of times characters text1 occur in text2' got 0. correct answer 15.'

def occurrences(text1, text2): """return number of times characters text1 occur in text2  occurrences(string, string) -> int """ text1_dict = {char:0 char in text1}  char in text2:     if char in text1_dict:         text1_dict[char] += 1  return sum(list(text1_dict.values())) 

i wrote code solves in o(n) instead of o(n^3) makes dict ouf of text1's chars, counts occurance of each char in text 2 counts text2's chars , update dict. returns sum of values (unfortunatly cant sum on dict values used list before).


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -