Created
July 3, 2012 07:07
-
-
Save npras/3038191 to your computer and use it in GitHub Desktop.
Ruby Challenge 4: Puns, Palindromes and Parrots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Puns, Palindromes and Parrots | |
# Challenge 4: http://www.therubygame.com/challenges/4/submissions | |
#str = "abacdfgdcabaypqqpy" # 324 171 163 | |
#str = "natarajan" # 81 45 42 # "ata" | |
str = "ghjiiooiijplr" | |
# desired output "ypqqpy" | |
def find_longest_palindrome(string) | |
p = "" | |
s = string | |
l = s.length - 1 | |
0.upto l do |x| | |
l.downto x do |y| | |
a = s[x,y] | |
if a == a.reverse && a.length > p.length | |
p = a | |
break | |
end | |
end | |
end | |
p | |
end | |
puts "i/p: #{str}" | |
print "o/p: ", find_longest_palindrome(str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment