EvilZone

Programming and Scripting => Scripting Languages => : wolverine September 16, 2014, 05:57:03 AM

: [Python] Need Help ! Not sure where i'm wrong.
: wolverine September 16, 2014, 05:57:03 AM
:

def is_rev(word1, word2):
    if len(word1) != len(word2):
        return False
    i = 0
    j = len(word2)-1
    print(i,j)


    while j > 0:
        if word1[i] != word2[j]:
            return false
        i = i + 1
        j = j - 1
When i run this script it doesn't go through the i and j part in the while loop. I'm not sure why.
The output is like
:

>>> is_rev('pots', 'stop')
0 3
: Re: [Python] Need Help ! Not sure where i'm wrong.
: HTH September 16, 2014, 06:27:19 AM
Lets work through this nice and slow for everyone.


When you first his the while loop j equals 3 and i equals 0.
For each iteration j will decrement and i will increment
The while loop will only continue if the letter in one string coresponds with the letter in another.
So with a table (similar to my first year comp sci course...)
:
j|i | will run?   |   letters being compared
------------------|   p and p
3 0 | yes         }   o and o
2 1 | yes         |   t and t
1 2 | yes         |   s and s
0 3 | no

YEAH WHOOHOO so i got all the way through the loop without ever having different letts, which a prerequisite for an output...


Oh yeah. You forgot a return true statement, but I was having fun...

Sidenote: please try with racecar racecar and kayak kayak, just for the lulz


removed silly font n stuff,added code tags