Skip to content

Commit

Permalink
Merge pull request #170 from exercism/10_forces_implementation_concerns
Browse files Browse the repository at this point in the history
No longer drives the solution
  • Loading branch information
kytrinyx committed Jun 18, 2015
2 parents 226362b + 7e52f2d commit b1dba08
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 82 deletions.
51 changes: 30 additions & 21 deletions largest-series-product/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,43 @@
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
validate_length
return 1 if @digits.empty?
collection_of_digits
select_max { reduce_to_product { validate { separate } } }
end

private

def validate_length
@length > digits.length and
fail(ArgumentError.new 'Not enough digits')
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
def validate
yield.take_while { |array| array.size == @length }
end

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

def select_max
yield.max
end

def separate
digits.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
73 changes: 12 additions & 61 deletions largest-series-product/largest_series_product_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,10 @@

# Rubocop directives
# rubocop:disable Lint/ParenthesesAsGroupedExpression
# rubocop:disable Style/AlignParameters:
#
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)
end

def test_largest_product_of_2
skip
series = Series.new('0123456789')
assert_equal 72, series.largest_product(2)
end

def test_largest_product_of_a_tiny_number
skip
series = Series.new('12')
assert_equal 2, series.largest_product(2)
end
Expand All @@ -77,10 +19,18 @@ def test_another_tiny_number
assert_equal 9, series.largest_product(2)
end

def test_largest_product_of_2
skip
series = Series.new('0123456789')
assert_equal 72, series.largest_product(2),
'Largest product of all sets of 2 result from 8 and 9'
end

def test_largest_product_of_2_shuffled
skip
series = Series.new('576802143')
assert_equal 48, series.largest_product(2)
assert_equal 48, series.largest_product(2),
'Largest product of all sets of 2 result from 6 and 8'
end

def test_largest_product_of_3
Expand Down Expand Up @@ -118,7 +68,8 @@ def test_some_other_big_number
def test_identity
skip
series = Series.new('')
assert_equal 1, series.largest_product(0)
assert_equal 1, series.largest_product(0),
'Identity of an empty group is 1'
end

def test_slices_bigger_than_number
Expand Down

0 comments on commit b1dba08

Please sign in to comment.