Permalink
Browse files

Extract array_consecutive_integers to helper

  • Loading branch information...
camillebaldock committed Sep 7, 2014
1 parent 7663fbe commit d4f9dc3f47163b17b5eb51af22a8de527e81aec4
Showing with 65 additions and 14 deletions.
  1. +3 −0 README.md
  2. +12 −0 lib/array_helper.rb
  3. +1 −11 lib/hand.rb
  4. +49 −3 spec/array_helper_spec.rb
View
@@ -19,3 +19,6 @@ the ```pip_count``` and ```suit_count``` methods are essentially duplicates of e
3) Address the ```##bob``` comment
##bob is a slightly silly notation I use when I have a method/class/variable with a name that does not please me (not descriptive enough, actually wrong, any possibility for confusion) but I can't think of a better name at time of writing
In this case ```pip_count``` and ```suit_count``` don't describe the intention of those methods
4) Address the ```#TODO: live in a helper, utility``` comment
The consecutive_cards? methods is mainly checking whether an array is made of consecutive integers. The logic for this is now moved to the ArrayHelper.
View
@@ -7,4 +7,16 @@ def value_occurence_count(array)
end
result
end
def array_consecutive_integers?(array)
array.sort!
difference_always_1 = true
i = 0
while (difference_always_1 && i < (array.size - 1)) do
difference_between_values = array[i+1] - array[i]
difference_always_1 = difference_between_values == 1
i += 1
end
difference_always_1
end
end
View
@@ -93,18 +93,8 @@ def straight_flush
straight && flush
end
#TODO: live in a helper, utility
def consecutive_cards?(card_values)
card_values.sort!
#Magic number 4 => always 5 cards per hand
difference_always_1 = true
i = 0
while (difference_always_1 && i < 4) do
difference_between_pips = card_values[i+1] - card_values[i]
difference_always_1 = difference_between_pips == 1
i += 1
end
difference_always_1
array_consecutive_integers?(card_values)
end
end
View
@@ -13,9 +13,55 @@ class ExampleClass
let(:sample_array) { [:a, 1, "b", "B", 1] }
it "gives the number of occurences of each value in the array" do
result = @example_class.value_occurence_count(sample_array)
describe "#value_occurence_count" do
it "gives the number of occurences of each value in the array" do
result = @example_class.value_occurence_count(sample_array)
expect(result).to eq({ :a => 1, 1 => 2, "b" => 1, "B" => 1 })
end
end
describe "#array_consecutive_integers?" do
let(:consecutive_array) { [2,3,4,5,6,7] }
let(:consecutive_duplicate_array) { [2,3,4,4] }
let(:consecutive_unordered_array) { [2,5,3,4,1] }
let(:non_consecutive_array) { [1,4,5,6,7] }
it "returns true if the array is made of consecutive integers" do
result = @example_class.array_consecutive_integers?(consecutive_array)
expect(result).to be true
end
it "returns false if the array is made of consecutive integers with a duplicate" do
result = @example_class.array_consecutive_integers?(consecutive_duplicate_array)
expect(result).to be false
end
it "returns true if the array is made of unordered consecutive integers" do
result = @example_class.array_consecutive_integers?(consecutive_unordered_array)
expect(result).to be true
end
it "returns false if the array is not made of consecutive integers" do
result = @example_class.array_consecutive_integers?(non_consecutive_array)
expect(result).to be false
end
it "returns true if the the array is empty" do
result = @example_class.array_consecutive_integers?([])
expect(result).to be true
end
it "returns true if the array has only one element" do
result = @example_class.array_consecutive_integers?([1])
expect(result).to be true
end
expect(result).to eq({ :a => 1, 1 => 2, "b" => 1, "B" => 1 })
end
end

0 comments on commit d4f9dc3

Please sign in to comment.