Using Python programming Objectives : Practice string indexing. A palindrome is
ID: 3667334 • Letter: U
Question
Using Python programming
Objectives:
Practice string indexing.
A palindrome is a sequence of characters which reads the same backward or forward. For example, “racecar” is a palindrome. In this assignment, we treat upper- and lowercase letters as being different, i.e., case sensitive.
Write a function to determine whether a given string is a palindrome or not.
The function should be named isPalindrome and be able to accept a string as argument. It returns a Boolean value; True if the passed-in argument is a palindrome, False otherwise. For example, calling isPalindrome("racecar"), users should be able to receive True. However, calling isPpalindrome("Racecar"), users should receive False.
Function specifications:
Function name
isPalindrome
Input parameter
a string
return value
a Boolean value
Testing:
Develop at least 2 test cases, calculate the correct results by hand, and then confirm that your program gives the same results.
To test the correctness of your function, you can make function calls, one for each of your test cases. For example, print(isPalindrome("racecar")) should display True on screen. print(isPalindrome("Racecar")) should display False on screen.
When you are ready to submit, remove all such function calls from your script file. You do not need to submit your test cases.
Code specifications:
You must implement your own algorithm with either a while-loop or for-loop for palindrome determination. You are NOT allowed to use string slicing or reverse method.
Your submitted .py file should contain ONLY your function definition for isPalindrome. Do not include main function or your testing codes.
Explanation / Answer
Please find the required python isPalindrome function defenition( not in .py file) :
--------------------------------------------------------------------------------
def isPalindrome(S):
pali = True
for i in range (0, len(S) // 2):
if S[i] == S[(i * -1) - 1] and pali is True:
pali = True
else:
pali = False
return pali
------------------------------------------------------------------------------
The function is called isPalindrome(S) and requires a string "S". The return value is by defaultTRUE, to have the initial check on the first if statement.
After that, the for loop runs half the string length to check if the character from string "S" at the position "i" is the same at from the front and from the back. If once this is not the case, the function stops, prints out FALSE and returns false.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.