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

Pages: 1-

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 2:16

Excuse the 2 lines after the if statement, they are unnecessary. I only used those for testing.

Name: Anonymous 2013-03-13 4:02

... That's why I don't want Python to be the first langage they learn in Uni. How the fuck do you want them to have any inventivity ?

Name: Anonymous 2013-03-13 4:08

I dunno. Have you tried polymorphic multiple inheritance? I always just tell my employees to do stuff for me. Programming is a demeaning job anyway. I'll ride on the back of a fucking trash truck before I reduce myself to the level of a mere machine and write a line of code.

Name: Anonymous 2013-03-13 4:11

of course it is! iteration can be replaced with recursion

Name: Anonymous 2013-03-13 6:01

substr? No?

Name: Anonymous 2013-03-13 6:14

>>6
substr ? Are you serious ?
This is probably the worst idea ever.
I smell something using a while loop and the str.find method, using the optional second argument.

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.

Name: Anonymous 2013-03-13 6:39

>>8
Just build the state machine directly.

Name: Anonymous 2013-03-13 6:40

str.replace() to exclude non alphabet symbols
str.split()
check word vs every element of the resulting list

Name: Anonymous 2013-03-13 6:50

>>5 lool

in octave you could expand and then vectorize

Name: Anonymous 2013-03-13 6:57

does python have matrix ops?

Name: Anonymous 2013-03-13 7:04

or (the best way ^^) is to invert the loop =D and only iterate over the length of the search word =) zfilter style

Name: Anonymous 2013-03-13 7:10

>>13
OH GOOD, YOU'RE BACK.

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