diff --git a/problems/9-palindrome-number/Makefile b/problems/9-palindrome-number/Makefile deleted file mode 120000 index 25eaf27..0000000 --- a/problems/9-palindrome-number/Makefile +++ /dev/null @@ -1 +0,0 @@ -../template/Makefile \ No newline at end of file diff --git a/problems/9-palindrome-number/lib.c b/problems/9-palindrome-number/lib.c deleted file mode 100644 index 7fea0bd..0000000 --- a/problems/9-palindrome-number/lib.c +++ /dev/null @@ -1,32 +0,0 @@ -#include "lib.h" - -bool isPalindrome(int x) { - if (x < 0) - return false; - - // Find length and calculate divisor - int len = 0; - int div = 1; - int copy = x; - while (copy > 0) { - copy /= 10; - if (len != 0) - div *= 10; - len++; - } - - // Compare digits - for (int i=0; i - -bool isPalindrome(int x); diff --git a/problems/9-palindrome-number/main.c b/problems/9-palindrome-number/main.c deleted file mode 100644 index 37d3532..0000000 --- a/problems/9-palindrome-number/main.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include "lib.h" - -int main() { - int a = 121; - printf("Test 1: %d\n", isPalindrome(a) == 1); - - int b = -121; - printf("Test 2: %d\n", isPalindrome(b) == 0); - - int c = 1234554321; - printf("Test 3: %d\n", isPalindrome(c) == 1); - - int d = 2147483647; - printf("Test 4: %d\n", isPalindrome(d) == 0); - - return 0; -} diff --git a/problems/9-palindrome-number/main.rb b/problems/9-palindrome-number/main.rb new file mode 100644 index 0000000..93dea31 --- /dev/null +++ b/problems/9-palindrome-number/main.rb @@ -0,0 +1,15 @@ +# @param {Integer} x +# @return {Boolean} +def is_palindrome(x) + return false if x.negative? + + copy = x + inv = 0 + + while copy.positive? + inv = inv * 10 + copy % 10 + copy /= 10 + end + + inv == x +end diff --git a/problems/9-palindrome-number/test.rb b/problems/9-palindrome-number/test.rb new file mode 100644 index 0000000..e5355db --- /dev/null +++ b/problems/9-palindrome-number/test.rb @@ -0,0 +1,16 @@ +require_relative 'main' +require 'test/unit' + +class TestIsPalindrome < Test::Unit::TestCase + def test_palindrome + assert_equal(true, is_palindrome(121)) + end + + def test_negative + assert_equal(false, is_palindrome(-121)) + end + + def test_regular_number + assert_equal(false, is_palindrome(10)) + end +end