Question

In: Computer Science

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 a one-word palindrome. Here’s a sample output.

Please enter a string: redder

redder is a palindrome.

Please enter a string: zap4554paz

zap4554paz is a palindrome.

Please enter a string: hello

hello is not a palindrome.

Solutions

Expert Solution

To check for one-word palindrome, start from beginning of the string till half the length of string and for each string character in the check if the same character is also at the corresponding place from the end of the string, that is, the first character should also be the last character, second character should be the second last character, and so on. If any check fails then it is not a palindrome.

Code:

# palindrome function
# returns true if a word is palindrome
def isPalindrome(s):
  
   n = len(s)
  
# Run loop from 0 to half of length of string(exclusive).
# For even length string, loop is from 0 to length/2 - 1,
# as the string is zero indexed therefore,
# one of the two middle elements is at length/2 - 1
# If length of string is odd, then the middle element need not to be concerned.
   for i in range(0, int(n/2)):
       if(s[i] != s[n-1-i]):
           return False
  
   return True
  
# sample run
s = input("Please enter a string: ")
ans = isPalindrome(s)
  
if (ans):
print(s + " is a palindrome.")
else:
print(s + " is not a palindrome.")

Sample output:

Please take care of indentation in python. Attached code screenshot for your reference.


Related Solutions

#Python: A palindrome is a sequence of characters that reads the same backwards as forwards. For...
#Python: A palindrome is a sequence of characters that reads the same backwards as forwards. For example, ‘Eve’, ‘madam’, and 20502, are palindromes. Write a function called testPalindrome() that asks the user to input a string and returns if that string is a palindrome with the output as follows, without red: >>> Please enter a string: eve Your string "eve" is a palindrome. >>> testPalindrome() Please enter a string: end Your string "end" is not a palindrome. >>> testPalindrome() Please...
making a python code for this: A palindrome is a sequence that reads the same backwards...
making a python code for this: A palindrome is a sequence that reads the same backwards as forwards. Numbers can also be palindromes if we consider their digits as a sequence, for example 12121 and 8228 are palindromes. We can find palindromes from an initial seed number using the reverse and add method: choose a number, reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number...
Please provide Python code that does the following: 3) Write a program that takes a string...
Please provide Python code that does the following: 3) Write a program that takes a string as input, checks to see if it is comprised entirely of letters, and if all those letters are lower case. The output should be one of three possible messages: Your string is comprised entirely of lower case letters. Your string is comprised entirely of letters but some or all are upper case. Your string is not comprised entirely of letters. Your program may NOT:...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please show all outputs. Instructions: Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file. Postfix Expression                Result 4 5 7 2 + - * = -16 3 4 + 2  * 7 / = 2 5 7 + 6 2 -  * = 48 4 2 3 5 1 - + * + = 18   List as Stack Code: """...
Use python programming to write this code and provide a screen short for the code. 2....
Use python programming to write this code and provide a screen short for the code. 2. Write a function that takes one argument (a string) and returns a string consisting of the single character from that string with the largest value. Your function should contain a for loop. You can assume that the input to your function will always be a valid string consisting of at least one character. You can assume that the string will consist only of lower-case...
provide a JavaScript code that finds if the given word by user (prompt) is a Palindrome...
provide a JavaScript code that finds if the given word by user (prompt) is a Palindrome or no.
a) write a python programme for converting url.to html please provide valid code and please share...
a) write a python programme for converting url.to html please provide valid code and please share screenshots to me
Write code in Python that does the following : An anagram is when the order of...
Write code in Python that does the following : An anagram is when the order of a word can be changed to create a new word (evil, live, and vile for example). Using the dictionary provided, find all of the anagram groups. Which words contain the most anagrams? How large is this group? Here is another example: Alert, alter, and later have 3 in their group. Use the provided dict.txt as your word list, solution must also finish in less...
how do you code a psuedo-random probability(%) in python? please provide an example.
how do you code a psuedo-random probability(%) in python? please provide an example.
write a python code that Returns a string composed of characters drawn, in strict alternation, from...
write a python code that Returns a string composed of characters drawn, in strict alternation, from s1 and s2. If one string is longer than the other, the excess characters are added to the end of the string as shown in the examples below #Example 1 - blend("ape", "BANANA") returns "aBpAeNANA" #Example 2 - blend("BOOT", "gold") returns "BgOoOlTd"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT