Обратите внимание, что string.downcase.split(/\W+/).join('') == string.downcase.split(/\W+/).join('').reverse ? 'yes is a palindrome' : ' is not a palindrome. :('
такой же, как этот:
if string.downcase.split(/\W+/).join('') == string.downcase.split(/\W+/).join('').reverse
'yes is a palindrome'
else
' is not a palindrome. :('
end
Ответ:
def palindrome(string)
string.downcase.split(/\W+/).join('') == .
string.downcase.split(/\W+/).join('').reverse ? 'yes is a palindrome' : ' is not a palindrome. :('
end
# test code
p palindrome("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.")
p palindrome("Depardieu, go razz a rogue I draped")
p palindrome("Desserts I stressed.")
p palindrome("Detartrated.")
p palindrome("Devo met a Mr., eh, DNA and her mate moved.")
p palindrome("Di as dad said.")
p palindrome("Did I draw Della too tall, Edward? I did?")
p palindrome("Dior droid.")
p palindrome("DNA-land.")
p palindrome("Do geese see god?")
p palindrome("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.")
p palindrome("abracadabra!")
p palindrome("Mister, mister, on a see-saw with your sister.")
p palindrome("Almost every sentence is NOT a palindrome! How unfair!")
Это вернет:
"yes is a palindrome"
"yes is a palindrome"
"yes is a palindrome"
"yes is a palindrome"
"yes is a palindrome"
"yes is a palindrome"
"yes is a palindrome"
"yes is a palindrome"
"yes is a palindrome"
"yes is a palindrome"
"yes is a palindrome"
" is not a palindrome. :("
" is not a palindrome. :("
" is not a palindrome. :("