Last active
August 29, 2015 14:25
-
-
Save radcliff/a8e18d9ce5d43019e287 to your computer and use it in GitHub Desktop.
Print out a multiplication table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'multiplication-table' | |
require 'minitest/autorun' | |
class TestPrinter < MiniTest::Unit::TestCase | |
def test_for_create_table | |
table = create_mult_table(3) | |
assert_equal(3, table.size) | |
assert_equal(3, table[0].size) | |
assert_equal(9, table[2][2]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# print out a multiplication table | |
# 1 2 3 4 5 | |
# 2 4 6 8 10 | |
# 3 6 9 12 15 | |
# 4 8 12 16 20 | |
# 5 10 15 20 25 | |
def create_mult_table(max) | |
return if max.nil? | |
table = [] | |
for i in 1..max | |
row = Array.new(max) { |idx| ( idx + 1 ) * i } | |
table << row | |
end | |
return table | |
end | |
def print_table(table) | |
table.each do |row| | |
p row.join(" ") | |
end | |
return nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment