Python
/
Applications
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 6
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tree S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Applications Palindrome
Word spelled the same forward / backword first = word[0] last = word[:-1] middle = word[1:-1]Palindrome
Palindrome (A)
Use incremental developent to check if word is palindrome. A palindrome is a word that is spelled the same backward and forward.First step
Make the function's definition skeleton.
"""Palindrome string
Use incremental development to check if string is palindrome
A palindrome is a word that is spelled the same backward and forward.
Example: noon, redivider
We use built-in function len() to check the string length.
Version 1: define function
"""
def is_palindrome(word):
return True
assert is_palindrome("abc") == True
Second step
Test if first is different than last.
"""Palindrome string
Use incremental development to check if string is palindrome
A palindrome is a word that is spelled the same backward and forward.
Example: noon, redivider
We use built-in function len() to check the string length.
Version 2: test first/last
"""
def is_palindrome(word):
first = word[0]
last = word[-1]
if (first != last):
return False
return True
assert is_palindrome("abca") == True
assert is_palindrome("abc") == False
Third step
Test if the middle is palindrome.
"""Palindrome string
Use incremental development to check if string is palindrome
A palindrome is a word that is spelled the same backward and forward.
Example: noon, redivider
Version 3: test middle
"""
def is_palindrome(word):
if (word[0] != word[-1]):
return False
if (len(word) > 2):
middle = word[1:-1]
return is_palindrome(middle)
return True
assert is_palindrome("abcba") == True
assert is_palindrome("abca") == False
Final version
The final verions contains all the refactoring.
"""Palindrome string
Use incremental development to check if string is palindrome
A palindrome is a word that is spelled the same backward and forward.
Example: noon, redivider
Final version: refactoring
"""
def first(word):
return word[0]
def last(word):
return word[-1]
def middle(word):
return word[1:-1]
def is_palindrome(word):
if (first(word) != last(word)):
return False
if (len(word) > 2):
return is_palindrome(middle(word))
return True
assert is_palindrome("noon") == True
assert is_palindrome("redivider") == True
assert is_palindrome("to") == False
assert is_palindrome("moomm") == False
➥ Questions