Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Python

Name: Anonymous 2013-03-13 2:15

Is there any decent way to do this without the use of iteration?
It basically asks for 2 strings, and it'll search the word you input from the second string in the first while accounting for overlapping substrings.

def find_email():
    email_message = raw_input("Enter the email message: ")
    find_word = raw_input("Enter the word to find: ")
    occurrence_count = 0
    print range(len(email_message) - len(find_word) + 1)
    for x in range(len(email_message) - len(find_word) + 1):
        if email_message[x:x + len(find_word)] == find_word:
            print email_message[x:x + len(find_word)]
            print x
            occurrence_count += 1
    print occurrence_count

Name: Anonymous 2013-03-13 6:20

Well, first off you can start with this instead:
if email_message[len(find_word)] == find_word[len(find_word)]
That'll save you a few iterations if python doesn't check for word equality backwards.
Also if you're looking for whole words, you can split the email message on word boundaries. In fact, just use a regex for the whole thing.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List