Python - Objective 10 Learn How To Master The Basics - Problem 02
Python - Objective 10 Learn How To Master The Basics - Problem 02
Learn text]
how to master the basics – problem 2 Craig’n’Dave
Craig’n’Dave
TRY
Sample program:
# Palindrome
# Main program
print(Palindrome("Sam"))
print(Palindrome("Hannah"))
PYTHON T I M E
[Header
Learn text]
how to master the basics – problem 2 Craig’n’Dave
Craig’n’Dave
INVESTIGATE
Sample program:
# Palindrome
# Main program
print(Palindrome("Sam"))
print(Palindrome("Hannah"))
PYTHON T I M E
[Header
Learn text]
how to master the basics – problem 2 Craig’n’Dave
Craig’n’Dave
INVESTIGATE
Program comprehension: ITEM Identify a selection command in the program.
1. # Palindrome
PYTHON T I M E
[Header
Learn text]
how to master the basics – problem 2 Craig’n’Dave
Craig’n’Dave
INVESTIGATE
What is the purpose of the iteration in the Palindrome
Program comprehension: PURPOSE function?
1. # Palindrome
PYTHON T I M E
[Header
Learn text]
how to master the basics – problem 2 Craig’n’Dave
Craig’n’Dave
INVESTIGATE
What would be the effect of moving the selection statement in
Program comprehension: RELATION lines 8-9 to after the iteration before line 10?
1. # Palindrome
2. # Function to return if a string is a palindrome How do lines 6 and 9 relate to each other? What are they
achieving together?
3. def Palindrome(Text):
4. Text = Text.lower()
5. Checks = len(Text) // 2
6. IsPalindrome = True
7. for Index in range(0, Checks):
8. if Text[Index] != Text[-Index-1]:
9. IsPalindrome = False
10. return IsPalindrome
APPROACH Line 7 uses a for loop, why is this possibly less efficient than
11.# Main program using a while loop?
12.print(Palindrome("Sam"))
13.print(Palindrome("Hannah"))
PYTHON T I M E
[Header
Learn text]
how to master the basics – problem 2 Craig’n’Dave
Craig’n’Dave
Palindrome problem:
1 point.
If the Text parameter is an empty string, the Palindrome function should return False not True.
2 points.
Change the program so that the user can enter the string to be checked. The program should output a suitable message, and not just
True or False.
Write an efficient palindrome function that terminates as soon as character pairs do not match.
3 points.
“Was it a car or a cat I saw” is a palindrome. However because of the spaces, the Palindrome function will return False. Improve the
function so that spaces are ignored.
Madam, I’m Adam is considered to be perhaps the most well-known palindrome although the algorithm will return False because of the
apostrophe. Change the function to ignore all characters except letters.
PYTHON T I M E
[Header
Learn text]
how to master the basics – problem 2 Craig’n’Dave
Craig’n’Dave
EVALUATE
Test table:
Input 1 Type of test Expected output Pass/Fail Test
Empty string False
bob Palindrome lower case, odd number of characters True
anna Palindrome lower case, even number of characters True
ANNA Palindrome upper case True
Anna Palindrome mixed case True
CRAIG Uppercase string False
craig Lowercase string False
Craig Mixed case string False
Dave String with no spaces False
King are you glad you are king Palindrome with spaces False
Borrow or rob Palindrome with spaces True
Hello World String with spaces False
123454321 Odd number of digits True
12344321 Even number of digits True
PYTHON T I M E
[Header
Learn text]
how to master the basics – problem 2 Craig’n’Dave
Craig’n’Dave
EVALUATE
Review your solutions:
Did you: Self-assess: Yes/No
Use a comment after each program branch to explain the purpose of the section?
Use a comment before each iteration to explain the purpose of the loop?
Use exception handling to ensure your program cannot crash unexpectedly due to run-time errors?
Test your programs against different types of data to check they work?
PYTHON T I M E
[Header
Learn text]
how to master the basics – problem 2 Craig’n’Dave
Craig’n’Dave
Follow us for the latest news and downloads for this resource.
https://www.facebook.com/craigndave
https://twitter.com/craigndave1
https://www.youtube.com/craigndave
https://craigndave.org
https://student.craigndave.org
PYTHON T I M E