Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

Python - Objective 10 Learn How To Master The Basics - Problem 02

Uploaded by

asddsadsad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Python - Objective 10 Learn How To Master The Basics - Problem 02

Uploaded by

asddsadsad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

[Header

Learn text]
how to master the basics – problem 2 Craig’n’Dave
Craig’n’Dave

TRY
Sample program:
# Palindrome

# Function to return if a string is a palindrome


def Palindrome(Text):
Text = Text.lower()
Checks = len(Text) // 2
IsPalindrome = True
for Index in range(Checks):
if Text[Index] != Text[-Index-1]:
IsPalindrome = False
return IsPalindrome

# 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

# Function to return if a string is a palindrome


def Palindrome(Text):
Text = Text.lower()
Checks = len(Text) // 2
IsPalindrome = True
for Index in range(Checks):
if Text[Index] != Text[-Index-1]:
IsPalindrome = False
return IsPalindrome

# 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

2. # Function to return if a string is a palindrome Identify a string in the program.


3. def Palindrome(Text):
4. Text = Text.lower()
5. Checks = len(Text) // 2
6. IsPalindrome = True
7. for Index in range(Checks):
8. if Text[Index] != Text[-Index-1]:
9. IsPalindrome = False
10. return IsPalindrome

11.# Main program STRUCTURE Which variable holds a flag?


12.print(Palindrome("Sam"))
13.print(Palindrome("Hannah"))

Which lines of code are an iteration?

What would the output from the algorithm be for “Level”?

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

2. # Function to return if a string is a palindrome What is the purpose of line 8?


3. def Palindrome(Text):
4. Text = Text.lower()
5. Checks = len(Text) // 2
6. IsPalindrome = True
7. for Index in range(Checks):
8. if Text[Index] != Text[-Index-1]:
9. IsPalindrome = False
10. return IsPalindrome

11.# Main program REASON Why is integer division required in line 5?


12.print(Palindrome("Sam"))
13.print(Palindrome("Hannah"))

Why is it necessary to convert the string to lowercase in line 4?

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

MAKE Madam I’m


Adam.

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

Complete all the requirements of the problem specification?

Start your program with a comment to describe what it does?

Use subroutines for the main algorithms in your code?

Use a comment to describe what each subroutine does?

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 variable names that describe the data they hold?

Validate or sanitise the user inputs?

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

More problems to solve:


This is just one extension problem to help you master the basics of programming.
Craig’n’Dave will be releasing more problems like this over the coming months and years.

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

You might also like