Skip to content

Instantly share code, notes, and snippets.

@npras
Created July 3, 2012 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npras/3038191 to your computer and use it in GitHub Desktop.
Save npras/3038191 to your computer and use it in GitHub Desktop.
Ruby Challenge 4: Puns, Palindromes and Parrots
# 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