In the flavor of the work begun there, continue to modify the is_pal_phrase function to work for palindromic phrases and sentences by having it ignore spaces ignore case (upper versus lower) ignore common punctuation, that is the thirteen characters , ; : - ' " . ? ! ( ) [ ]

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Problem 7.1: Phinding Palindromic Phrases 

Purpose: Learn string manipulation in the context of a simple program.

For this problem, you will modify an existing program to be more flexible.

 

Go to your copy of CheckPalindromicPhrase.ipynb from Lab Assignment 7.

 

In the flavor of the work begun there, continue to modify the is_pal_phrase function to work for palindromic phrases and sentences by having it

  • ignore spaces
  • ignore case (upper versus lower)
  • ignore common punctuation, that is the thirteen characters , ; : - ' " . ? ! ( ) [ ]

For example, "a Toyota" and "Madam, I'm Adam." and "Was it a rat I saw?" should be identified as palindromes; ditto for "A man, a plan, a canal--Panama!" and "Madam (in Eden), I'm Adam."

 

Comment every line you added, using the following format:

# YOURINITIALS: your comment

For example, here is a line from my function using one of the lines suggested in LA7:

s = s.replace(" ","")  # MSB: remove spaces from string

A good plan of attack would be to add a line, comment it as to what it is doing, test it against the positive_test_cases by running the third block, debug, repeat.

 

Next, IF THEY ARE NOT ALREADY THERE add two more tests to positive_test_cases for the phrases highlighted in blue above.

 

Turn-In:  Cut-and-paste of (1) just your commented is_pal_phrase function (2) just the output of the 25 positive test cases.

CheckPalindromicPhrase.ipynb from Lab Assignment 7 has been copied below:-

# boolean function returning True/False for
# "Is s is a palindromic word or phrase?",
# disregarding case, spaces, punctuation
def is_pal_phrase(s):
    return s == s[::-1]
print(is_pal_phrase(""))
print(is_pal_phrase("r"))
print(is_pal_phrase(">>"))
print(is_pal_phrase("<>"))
print(is_pal_phrase("bad"))
print(is_pal_phrase("dad"))
print(is_pal_phrase("Dad"))
print(is_pal_phrase("racecar"))
print(is_pal_phrase("race car"))
print(is_pal_phrase("A Toyota"))
 
positive_test_cases = [
    "I",
    "aa",
    ">>",
    "$#$",
    "dad",
    "Dad",
    "777",
    "deed",
    "s:eyes",
    "radar",
    "RADAR",
    "13|.|31",
    "ATOYOTA",
    "A Toyota",
    "Taco cat",
    "Don't nod",
    "I did, did I?",
    "madamimadam",
    "Madam, I'm Adam.",
    "able was I ere I saw elba",
    "Able was I, ere I saw Elba.",
    "a man, a plan aca nalp a ,nam a",
    "A man, a plan, a canal--Panama!",
]

passed = 0
for t in positive_test_cases:
    if is_pal_phrase(t):
        #print(f"+ {t:>32}  IS  palindromic phrase")
        print("+", t)
        passed += 1
    else:
        #print(f"{t:>32}  NOT palindromic phrase")
        print("-", t)
print(f"\nPassed {passed} of {len(positive_test_cases)} positive tests.\n")
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY