Sunday, 11 August 2013

Processing Strings Using a For Loop

Processing Strings Using a For Loop

I need to write a function that counts the number of times characters from
one string occur in another.
This is what I have so far,
def occurrences(text1, text2):
"""Return the number of times characters from text1 occur in text2
occurrences(string, string) -> int
"""
count = 0
#setup = set('text1')
for text1 in text2:
if text1 == text2:
count += 1
return count
I don't understand how to compare the strings given that they will be
randomly generated. I know I need to look at each character in the second
argument and see if it is in the first argument.

No comments:

Post a Comment