Sha256: a5292da219d35a48358e1aa6c2956b381143307e3d365d24ce6abe8c1efa1d75
Contents?: true
Size: 1.51 KB
Versions: 90
Compression:
Stored size: 1.51 KB
Contents
require 'exercise_cases' class AlphameticsCase < OpenStruct def test_name "test_#{description.tr(' ', '_')}" end def skipped index.zero? ? '# skip' : 'skip' end def input "'#{puzzle}'" end def expect expected.nil? ? {} : expected_values end def workload body = "input = %s\n" % input, "expected = %s\n" % expect, "assert_equal expected, Alphametics.solve(input)" indent(body, 4) end def runtime_comment if slow? comments = '', "# The obvious algorithm can take a long time to solve this puzzle,\n", "# but an optimised solution can solve it fairly quickly.\n", "# (It's OK to submit your solution without getting this test to pass.)\n" indent(comments, 2) end end private def slow? (expected||{}).size > 7 end def expected_values "{ #{indent(expected_values_as_lines, 17)} }" end def expected_values_as_lines lines = expected_values_as_strings.each_slice(4).map { |line| line.join(', ') } add_trailing_comma_and_newline(lines) end def expected_values_as_strings expected.sort.map { |(key, value)| "'#{key}' => #{value}" } end def add_trailing_comma_and_newline(lines) lines[0...-1].map { |line| "#{line},\n" }.push(lines.last) end def indent(lines, spaces) lines.join(' ' * spaces) end end AlphameticsCases = proc do |data| JSON.parse(data)['solve']['cases'].map.with_index do |row, i| row = row.merge('index' => i) AlphameticsCase.new(row) end end
Version data entries
90 entries across 90 versions & 1 rubygems