Question

In: Computer Science

Answer question in Python, show all code 2) Recall the palindrome problem. Write a new program...

Answer question in Python, show all code

2) Recall the palindrome problem. Write a new program that prompts the user for a string and checks to see if that string is a palindrome. For this updated program, input strings may have punctuation marks, spaces, and capitalizations, but these are ignored in the palindrome check. Here are some sample interactions.

Please enter a string to check: A man, a plan, a canal: Panama!

The string "A man, a plan, a canal: Panama!" is a palindrome.

Please enter a string to check: No ‘x’ in Nixon

The string "No ‘x’ in Nixon" is a palindrome.

Please enter a string to check: hello students

The string "hello students" is not a palindrome.

Hint: Note that you already know how to check if a string is a palindrome, as long as that string doesn’t include punctuation marks, spaces and both upper and lower case characters. Therefore, your job here is to figure out how to get the input string transformed into a string that does not include these things, and then do the same check you did before

Solutions

Expert Solution

Code:-

def is_palindome(new_string): #palindrome checker
for i in range(len(new_string)):
if new_string[i]!=new_string[len(new_string)-i-1]:
return False
return True
string=input("please enter string to check: ") #taking input from user
new_string ="" #take new_string
for i in string: #for each character in string
if i.isalpha(): #if i is character
new_string+=i.lower() #append it's lower case character in new_string

#use can use new_string == new_string[::-1] to check for palindrome
if (is_palindome(new_string)): #calling function
print("The String "+string+" is a palindrome")
else:
print("The String "+string+" is not a palindrome")
  

Screenshot:-

Output:-


Related Solutions

Answer question in Python, show all code: Write a program that calculates and displays the end...
Answer question in Python, show all code: Write a program that calculates and displays the end of year balances in a savings account if $1,000 is put in the account at 6% interest for five years. For this problem, assume interest is calculated annually. (That is, if I put $1000 in a bank account at the beginning of the year, then the balance at the end of the year is $1,000 + $1,000*6%.) You may assume no money is removed...
Use Python to solve, show all code Write a program that sums a sequence of integers...
Use Python to solve, show all code Write a program that sums a sequence of integers from a starting integer to and ending integer. The sequence is generated by skipping a value (e.g. 1,3,5,… or 2,6,10,14…). Prompt the user for a) a starting value greater than 0. Validate the value is greater than 0. b) the ending value. c) the value to skip Print out the sum of the numbers for the sequence. If the starting value is 2, the...
please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
Use Python to solve: show all code: 2) For the last assignment you had to write...
Use Python to solve: show all code: 2) For the last assignment you had to write a program that calculated and displayed the end of year balances in a savings account if $1,000 is put in the account at 6% interest for five years. The program output looked like this: Balance after year 1 is $ 1060.0 Balance after year 2 is $ 1123.6 Balance after year 3 is $ 1191.02 Balance after year 4 is $ 1262.48 Balance after...
Answer in Python: show all code 3) Modify your code to take as input from the...
Answer in Python: show all code 3) Modify your code to take as input from the user the starting balance, the starting and ending interest rates, and the number of years the money is in the account. In addition, change your code (from problem 2) so that the program only prints out the balance at the end of the last year the money is in the account. (That is, don’t print out the balance during the intervening years.) Sample Interaction:...
all python Question One [2 * 2.5] Write a program that prompts the user for two...
all python Question One [2 * 2.5] Write a program that prompts the user for two integers and then prints •The sum •The difference •The product •The average •The distance (absolute value of the difference) •The maximum (the larger of the two) •The minimum (the smaller of the two) Hint: Python defines max and min functions that accept a sequence of values, each separated with a comma. Write a program that prompts the user for a measurement in meters and...
Write PYTHON CODE to answer the following question: Consider the following data: x = [0, 2,...
Write PYTHON CODE to answer the following question: Consider the following data: x = [0, 2, 4, 6, 9, 11, 12, 15, 17, 19] y = [5, 6, 7, 6, 9, 8, 8, 10, 12, 12] Using Python, use least-squares regression to fit a straight line to the given data. Along with the slope and intercept, compute the standard error of the estimate and the correlation coefficient. Best fit equation y = ___ + ___ x Standard error, Sy/x =...
Python Problem Problem 1: In this problem you are asked to write a Python program to...
Python Problem Problem 1: In this problem you are asked to write a Python program to find the greatest and smallest elements in the list. The user gives the size of the list and its elements (positive and negative integers) as the input. Sample Output: Enter size of the list: 7 Enter element: 2 Enter element: 3 Enter element: 4 Enter element: 6 Enter element: 8 Enter element: 10 Enter element: 12 Greatest element in the list is: 12 Smallest...
Please provide Python code that does the following: 2) A palindrome is any sequence of characters...
Please provide Python code that does the following: 2) A palindrome is any sequence of characters that reads the same backwards as forwards. One-word examples include: Dad madam rotor Kayak redder noon For the sake of this exercise, the following may also be considered one-word palindromes: 1881 zap4554paz That is, numeric strings and “nonsense” strings that read the same backwards as forwards should be classified as palindromes. Write a program that takes as input a string and determines if it’s...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT