Skip to content

Commit

Permalink
No longer drives the solution
Browse files Browse the repository at this point in the history
Does not rely on on any specifically named 'helper' methods.  Tests
things through the implementation via public facing API

re: #10
  • Loading branch information
kotp committed Jun 16, 2015
1 parent e080970 commit ecdf7fb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 77 deletions.
45 changes: 24 additions & 21 deletions largest-series-product/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,37 @@
class Series
attr_reader :digits
def initialize(numeric_string)
@digits = convert_to_digits(numeric_string)
@digits = numeric_string
end

def largest_product(length)
fail ArgumentError.new('Not enough digits') if length > digits.length
products = []
slices(length).each do |slice|
products << slice.inject(1) do |product, n|
product * n
end
end
products.sort.last
@length = length
fail ArgumentError.new('Not enough digits') if @length > digits.length
analyzer
end

def slices(length)
result = []
i = -1
begin
i += 1
i2 = i + length - 1
result << digits[i..i2]
end while i2 < digits.size - 1
result
private

def analyzer
collection_of_digits
reduce_to_product { validate { separate } }.max
end

private
def validate
yield.take_while { |array| array.size == @length }
end

def reduce_to_product
yield.map { |array| array.inject(1, :*) }
end

def separate
0.upto(digits.length - 1).map.with_index do |_, index|
digits[index, @length]
end
end

def convert_to_digits(s)
s.chars.to_a.map(&:to_i)
def collection_of_digits
@digits = digits.chars.map(&:to_i)
end
end
58 changes: 2 additions & 56 deletions largest-series-product/largest_series_product_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,8 @@
# rubocop:disable Lint/ParenthesesAsGroupedExpression
#
class Seriestest < Minitest::Test
def test_digits
assert_equal (0..9).to_a, Series.new('0123456789').digits
end

def test_same_digits_reversed
skip
assert_equal (0..9).to_a.reverse, Series.new('9876543210').digits
end

def test_fewer_digits
skip
assert_equal (4..8).to_a.reverse, Series.new('87654').digits
end

def test_some_other_digits
skip
assert_equal [9, 3, 6, 9, 2, 3, 4, 6, 8], Series.new('936923468').digits
end

def test_slices_of_zero
skip
assert_equal [], Series.new('').digits
end

def test_slices_of_2
skip
series = Series.new('01234')
expected = [[0, 1], [1, 2], [2, 3], [3, 4]]
assert_equal expected, series.slices(2)
end

def test_other_slices_of_2
skip
series = Series.new('98273463')
expected = [[9, 8], [8, 2], [2, 7], [7, 3], [3, 4], [4, 6], [6, 3]]
assert_equal expected, series.slices(2)
end

def test_slices_of_3
skip
series = Series.new('01234')
expected = [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
assert_equal expected, series.slices(3)
end

def test_other_slices_of_3
skip
series = Series.new('982347')
expected = [[9, 8, 2], [8, 2, 3], [2, 3, 4], [3, 4, 7]]
assert_equal expected, series.slices(3)
def test_required_method_called_largest_product
assert_respond_to Series.allocate, :largest_product
end

def test_largest_product_of_2
Expand Down Expand Up @@ -115,12 +67,6 @@ def test_some_other_big_number
assert_equal 28_350, series.largest_product(6)
end

def test_identity
skip
series = Series.new('')
assert_equal 1, series.largest_product(0)
end

def test_slices_bigger_than_number
skip
series = Series.new('123')
Expand Down

0 comments on commit ecdf7fb

Please sign in to comment.