-
Notifications
You must be signed in to change notification settings - Fork 9
/
ex_08_09.py
30 lines (26 loc) · 849 Bytes
/
ex_08_09.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/python
#AUTHOR: alexxa
#DATE: 25.12.2013
#SOURCE: Think Python: How to Think Like a Computer Scientist by Allen B. Downey
# http://www.greenteapress.com/thinkpython/html/index.html
#PURPOSE: Chapter 8. Strings
# Exercise 8.8
# Starting with this diagram, execute the program on paper,
# changing the values of i and j during each iteration. Find
# and fix the second error in this function.
def is_reverse(word1, word2):
if len(word1) != len(word2):
return False
i = 0
j = len(word2) - 1 # the first mistake was defined by author
while j >= 0: # the second mistake was here, it must be >= and not >
print(i,j)
if word1[i] != word2[j]:
return False
i = i+1
j = j-1
return True
print(is_reverse('pots', 'stop'))
print()
print(is_reverse('pats', 'stop'))
#END