In: Computer Science
PYTHON:
Your script should test each string in the list of palindromes below. Be aware there's a second list of palindromes embedded in the palindromes list. In other words, palindromes contains strings and it contains a list of strings. Your code should be able to detect whether the item is a list by using the isinstance() function.
Code to get you started
# YOUR COMMENT BLOCK GOES HERE from random import shuffle palindromes = [ "Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane, Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette, Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette, Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee, Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned.", "Depardieu, go razz a rogue I draped.", "Desserts I stressed.", "Detartrated.", "Devo met a Mr., eh, DNA and her mate moved.", "Di as dad said.", "Did I draw Della too tall, Edward? I did?", "Dior droid.", "DNA-land.", "Do geese see god?", "Do good? I? No. Evil anon I deliver. I maim nine more hero-men in Saginaw, sanitary sword a-tuck, Carol, I. Lo! Rack, cut a drowsy rat in Aswan. I gas nine more hero-men in Miami. Reviled, I (Nona) live on. I do, O God.",'"Do nine men interpret?" "Nine men," I nod.', [ "abracadabra!","Mister, mister, on a see-saw with your sister.","Almost every sentence is NOT a palindrome! How unfair!"] ] # Shuffle the palindromes shuffle(palindromes) ######### ######### YOUR CODE GOES HERE #########
The entire code is as follows:
def is_palindrome(s):
s = s.lower()
temp = ''
for each_letter in s:
if each_letter.isalpha():
temp += each_letter
s = temp
if s == s[::-1]:
return True
return False
from random import shuffle
palindromes = [
"Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane, Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette, Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette, Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee, Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned.", "Depardieu, go razz a rogue I draped.", "Desserts I stressed.", "Detartrated.", "Devo met a Mr., eh, DNA and her mate moved.", "Di as dad said.", "Did I draw Della too tall, Edward? I did?", "Dior droid.", "DNA-land.", "Do geese see god?", "Do good? I? No. Evil anon I deliver. I maim nine more hero-men in Saginaw, sanitary sword a-tuck, Carol, I. Lo! Rack, cut a drowsy rat in Aswan. I gas nine more hero-men in Miami. Reviled, I (Nona) live on. I do, O God.",'"Do nine men interpret?" "Nine men," I nod.', [ "abracadabra!","Mister, mister, on a see-saw with your sister.","Almost every sentence is NOT a palindrome! How unfair!"]
]
shuffle(palindromes)
for each in palindromes:
if each.__class__ != list().__class__:
print(each)
print(is_palindrome(each))
else:
for nested_list in each:
print(nested_list)
print(is_palindrome(nested_list))
Over here there are two main logics. The nested list logic and is_palindrome logic.
1) is_palidrome()
We pass in the string that we got from the palindrome list. Now first we have to refine the string in order to make it acceptable for our palindrome logic.
We need to remove all the punctuations and spaces from the string. First, we lower case all the letters in the string. Then we remove all the punctuations and spaces by running the first for-loop in the function. This for-loop iterates through the string letter by letter and eliminate anything that is not an alphabet. The palindrome logic isn't that difficult and is shown above in the if condition.
2) nested list workaround
As we are dealing with a nested list, what we can do is set in the condition while iterating through the palindrome list. Over here we check if the element in the palindrome list is a list or a string. If it is not a list we call the is_palindrome function. If it is a list we iterate through that list again and then call the is_palindrome function for the nested list elements.