Skip to content

Instantly share code, notes, and snippets.

@0x0dea
Created January 1, 2016 07:21
Show Gist options
  • Save 0x0dea/0eb219557e262b19d7be to your computer and use it in GitHub Desktop.
Save 0x0dea/0eb219557e262b19d7be to your computer and use it in GitHub Desktop.
require 'minitest/autorun'
require 'minitest/pride'
# Given a flat array of positive integers, partition it into three sub-arrays
# whose sums are 5, 7, and 5, respectively, or nil if doing so is impossible.
#
# The argument is the syllable (mora) count of each word in some phrase, and
# the purpose of this method is cutting (kiru) the phrase into chunks which
# satisfy the 5-7-5 rule imposed upon modern haiku.
def kiru morae
end
describe '#kiru' do
it 'must reject obviously invalid inputs' do
kiru([1]).must_be_nil
kiru([6,6,6]).must_be_nil
kiru([1, 1, 2, 3, 5, 8]).must_be_nil
end
it 'must handle the very simple cases' do
kiru([1] * 17).must_equal [[1] * 5, [1] * 7, [1] * 5]
kiru([5,7,5]).must_equal [[5], [7], [5]]
end
it 'must handle slightly more complex cases' do
kiru([1,3,1,4,1,2,2,1,2]).must_equal [[1,3,1], [4,1,2], [2,1,2]]
kiru([5,4,2,1,1,3,1]).must_equal [[5], [4,2,1], [1,3,1]]
end
it 'must handle ALL THE CASES!' do
five = %w[11111 1112 1121 113 1211 122 131 14 2111 212 221 23 311 32 41 5]
five.map! { |s| s.chars.map &:to_i }
seven = %w[1111111 111112 111121 11113 111211 11122 11131 1114 112111 11212
11221 1123 11311 1132 1141 115 121111 12112 12121 1213 12211 1222
1231 124 13111 1312 1321 133 1411 142 151 16 211111 21112 21121
2113 21211 2122 2131 214 22111 2212 2221 223 2311 232 241 25 31111
3112 3121 313 3211 322 331 34 4111 412 421 43 511 52 61 7]
seven.map! { |s| s.chars.map &:to_i }
five.product(seven, five).each do |morae|
kiru(morae.flatten).must_equal morae
end
end
it 'must return nil for sequences of which Master Basho would not approve' do
kiru([3, 3, 5, 3, 3]).must_be_nil # can't get initial 5
kiru([1, 1, 1, 1, 2, 4, 2, 5]).must_be_nil # ditto
kiru([2, 1, 2, 4, 2, 2, 3, 1]).must_be_nil # can't get 7
kiru([2, 1, 2, 3, 3, 2, 2, 2]).must_be_nil # ditto
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment