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

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: """...
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.
Please write a pep/9 assembly code that checks if a word or number is a palindrome
Please write a pep/9 assembly code that checks if a word or number is a palindrome
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
I would like the following code in PYTHON please, there was a previous submission of this...
I would like the following code in PYTHON please, there was a previous submission of this question but I am trying to have the file save as a text file and not a json. Any help would be greatly appreciated. Create an application that does the following: The user will enter product codes and product numbers from the keyboard as strings. The product code must consist of four capital letters. The product number can only consist of numeric characters, but...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT