From 945bb9a2979300239d61820af0a66808ee7d1ff3 Mon Sep 17 00:00:00 2001 From: giuseppe de santis Date: Tue, 24 Nov 2015 18:29:15 +0000 Subject: [PATCH] added more tests and edited README --- README.md | 23 +++++++++++++++++++++++ lib/palindrome_counter.rb | 1 + spec/palindrome_counter_spec.rb | 9 +++++++++ 3 files changed, 33 insertions(+) diff --git a/README.md b/README.md index 478abbd..f5656f4 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,28 @@ *The range of number is given in a text file (the first two lines represent the starting and the ending numbers of the range respectively) +## Installation + +In your terminal do the following + +```bash +$ git clone https://github.com/giusepped/palindrome_numbers +$ cd palindrome numbers +$ ruby counter.rb +``` + +## Test + +In order to run the Rspec tests (there are 5 tests that test the same range numbers that are in the test files and one other), do the following + +```bash +$ rspec +``` + +## Approach + +I created an external class that does the counting in a range. I have to methods for checking wether a number (once converted into a string) is a palindrome: +* the first one uses the reverse ruby in-built method +* the second one uses recursion diff --git a/lib/palindrome_counter.rb b/lib/palindrome_counter.rb index 57d3d94..8e09ceb 100644 --- a/lib/palindrome_counter.rb +++ b/lib/palindrome_counter.rb @@ -20,6 +20,7 @@ def check_palindrome(string) # when checking for a large range, I implemented also a recursive method to check # if improved performance but it did not too much, I guess the problem is iterating # through every single element of a large range + def check_palindrome_recursive(string) return true if string.length < 2 return false if string[0] != string[-1] diff --git a/spec/palindrome_counter_spec.rb b/spec/palindrome_counter_spec.rb index ca2bfdd..45f3a7d 100644 --- a/spec/palindrome_counter_spec.rb +++ b/spec/palindrome_counter_spec.rb @@ -12,4 +12,13 @@ it 'should return 90 when counting from 1000 to 10000' do expect(subject.count(1000, 10000)).to eq 90 end + + it 'should return 10 when counting from 11200000 to 11300000' do + expect(subject.count(11200000, 11300000)).to eq 10 + end + + it 'should return 1854 when counting from 1034567 to 2888888' do + expect(subject.count(1034567, 2888888)).to eq 1854 + end + end \ No newline at end of file