Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
## Solution by James def matches(index, base) places = 4 permutation = "%0#{places * 2}d" % index.to_s(base) original = (0...places).map { |i| permutation[i].chr } guess = (places...(places * 2)).map { |i| permutation[i].chr } matches = 0 guess.each do |g| (0...places).each do |i| if g == original[i] original[i] = nil matches += 1 break end end end matches end ## Solution by Nathan def matches(index, base) counts = [0] * base 4.times do counts[index % base] += 1 index /= base end count = 0 4.times do count += counts[index % base] index /= base end count end ## Tests require 'test/unit' class MatchesTest < Test::Unit::TestCase def test_should_have_no_matches assert_equal 0, matches(12345678, 10) end def test_should_have_one_match_in_same_positiion assert_equal 1, matches(12341678, 10) end def test_should_have_one_match_in_different_positiion assert_equal 1, matches(12345671, 10) end def test_should_have_two_matches_in_same_positiion assert_equal 2, matches(12341278, 10) end def test_should_have_two_duplicate_matches_in_same_positiion assert_equal 2, matches(11341178, 10) end def test_should_have_two_matches_in_different_positiion assert_equal 2, matches(12345621, 10) end def test_should_have_two_duplicate_matches_in_different_positiion assert_equal 2, matches(11345611, 10) end def test_should_have_three_matches_in_same_positiion assert_equal 3, matches(12341238, 10) end def test_should_have_three_duplicate_matches_in_same_positiion assert_equal 3, matches(11141118, 10) end def test_should_have_three_matches_in_different_positiion assert_equal 3, matches(12345321, 10) end def test_should_have_three_duplicate_matches_in_different_positiion assert_equal 3, matches(11145111, 10) end def test_should_have_four_matches_in_same_position assert_equal 4, matches(12341234, 10) end def test_should_have_four_duplicate_matches assert_equal 4, matches(11111111, 10) end def test_should_have_four_matches_in_different_position assert_equal 4, matches(12344321, 10) end def test_should_have_one_match_when_digit_appears_twice_on_lhs_but_once_on_rhs assert_equal 1, matches(11345178, 10) end def test_should_have_one_match_when_digit_appears_once_on_lhs_but_twice_on_rhs assert_equal 1, matches(12345118, 10) end def test_should_have_two_matches_when_digit_appears_three_times_on_lhs_but_twice_on_rhs assert_equal 2, matches(11145118, 10) end def test_should_have_two_matches_when_digit_appears_twice_on_lhs_but_three_times_on_rhs assert_equal 2, matches(12315111, 10) end def test_should_have_three_matches_when_digit_appears_four_times_on_lhs_but_three_times_on_rhs assert_equal 3, matches(11115111, 10) end def test_should_have_three_matches_when_digit_appears_three_times_on_lhs_but_four_times_on_rhs assert_equal 3, matches(11141111, 10) end end ## James test output Loaded suite /Users/jamesmead/Desktop/matches.rb Started .................... Finished in 0.002072 seconds. 20 tests, 20 assertions, 0 failures, 0 errors ## Nathan test output Loaded suite /Users/jamesmead/Desktop/matches.rb Started F.....FFFF..FFFF..FF Finished in 0.026939 seconds. 1) Failure: test_should_have_four_duplicate_matches:86 <4> expected but was <16>. 2) Failure: test_should_have_one_match_when_digit_appears_once_on_lhs_but_twice_on_rhs:98 <1> expected but was <2>. 3) Failure: test_should_have_one_match_when_digit_appears_twice_on_lhs_but_once_on_rhs:94 <1> expected but was <2>. 4) Failure: test_should_have_three_duplicate_matches_in_different_positiion:78 <3> expected but was <9>. 5) Failure: test_should_have_three_duplicate_matches_in_same_positiion:70 <3> expected but was <9>. 6) Failure: test_should_have_three_matches_when_digit_appears_four_times_on_lhs_but_three_times_on_rhs:110 <3> expected but was <12>. 7) Failure: test_should_have_three_matches_when_digit_appears_three_times_on_lhs_but_four_times_on_rhs:114 <3> expected but was <12>. 8) Failure: test_should_have_two_duplicate_matches_in_different_positiion:62 <2> expected but was <4>. 9) Failure: test_should_have_two_duplicate_matches_in_same_positiion:54 <2> expected but was <4>. 10) Failure: test_should_have_two_matches_when_digit_appears_three_times_on_lhs_but_twice_on_rhs:102 <2> expected but was <6>. 11) Failure: test_should_have_two_matches_when_digit_appears_twice_on_lhs_but_three_times_on_rhs:106 <2> expected but was <6>. 20 tests, 20 assertions, 11 failures, 0 errors
This paste will be private.
From the Design Piracy series on my blog: