# kakuro-cross-sums / solver / ruby / kakuro-perms-1.rb

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65``` ```class KakuroPermutations def initialize() @min_digit = 1 @max_digit = 9 end def get_permutations_from(start, sum, num_places) if num_places == 1 if (sum >= start) && (sum <= @max_digit) return [[sum]] else return [] end end results = [] (start .. [@max_digit, sum].min).each do |first| results += get_permutations_from( first+1, sum-first, num_places-1 ).map {|rest| [first, *rest]} end return results end def get_permutations(sum, num_places) return get_permutations_from(@min_digit, sum, num_places) end end describe "Kakuro" do before do @perm = KakuroPermutations.new end it "should give [1,2] for 3" do @perm.get_permutations(3,2).should eql([[1,2]]) end it "should give [1,2,4] for 7" do @perm.get_permutations(7,3).should eql([[1,2,4]]) end it "should give [1,2,3,4,5] for 15 over 5" do @perm.get_permutations(15,5).should eql([[1,2,3,4,5]]) end it "should give correct results for 25 over 5" do @perm.get_permutations(25,5).should eql([ [1,2,5,8,9], [1,2,6,7,9], [1,3,4,8,9], [1,3,5,7,9], [1,3,6,7,8], [1,4,5,6,9], [1,4,5,7,8], [2,3,4,7,9], [2,3,5,6,9], [2,3,5,7,8], [2,4,5,6,8], [3,4,5,6,7], ]) end end ```