Skip to content

Commit

Permalink
added tests, made correct.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattknox committed Feb 21, 2012
1 parent d3a8d8d commit 049fc6f
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions palindrome.rb
@@ -1,30 +1,50 @@
def expand_from_center(str, top, bottom)
# return the max length you can get to given a top/bottom
while(bottom >= 0 && top < str.length) do
break if str[bottom] != str[top]
bottom -= 1
top += 1
require "rubygems"
require "test/unit"
class PalindromeTest < Test::Unit::TestCase
def expand_from_center(str, top, bottom)
# return the max length you can get to given a top/bottom
while(bottom >= 0 && top < str.length) do
break if str[bottom] != str[top]
bottom -= 1
top += 1
end
# top and bottom are one char too far away from center, and if they are ==,
# then we want length 1
return top - bottom - 1
end
return top - bottom - 1 # explain this.
end

def find_longest_palindrome(str)
max_length = 0
longest_pal = nil
def find_longest_palindrome(str)
max_length = 0
longest_pal = ""

(0...str.length).each do |i|
odd = expand_from_center str, i, i
if odd > max_length
max_length = odd
start = i - odd / 2 # ruby truncates
longest_pal = str[start, odd ]
end
even = expand_from_center str, i, i + 1
if even > max_length
max_length = even
start = i - even / 2
longest_pal = str[start, even ]
(0...str.length).each do |i|
odd = expand_from_center str, i, i
if odd > max_length
max_length = odd
start = i - odd / 2 # ruby truncates
longest_pal = str[start, odd ]
end
even = expand_from_center str, i, i + 1
if even > max_length
max_length = even
start = i - even / 2 + 1
longest_pal = str[start, even ]
end
end
longest_pal
end

def test_palindrome
assert_equal "", find_longest_palindrome("")
assert_equal "a", find_longest_palindrome("abc")
assert_equal "c", find_longest_palindrome("cba")
assert_equal "aa", find_longest_palindrome("aa")
assert_equal "aa", find_longest_palindrome("aabb")
assert_equal "aa", find_longest_palindrome("aabbcc")
assert_equal "racecar", find_longest_palindrome("racecar")
assert_equal "racecar", find_longest_palindrome("I have a racecar.")
assert_equal "ooooooooooo", find_longest_palindrome("I have a racecar.bxoooooooooooaxb")
assert_equal "bxoooooooooooxb", find_longest_palindrome("I have a racecar.bxoooooooooooxb")
# assert_equal "", find_longest_palindrome()
end
longest_pal
end

0 comments on commit 049fc6f

Please sign in to comment.