Last active
August 29, 2015 14:27
-
-
Save eternal44/5f11d4514c4630411ac7 to your computer and use it in GitHub Desktop.
Multiplication table generator - after
This file contains hidden or 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
# This method generates a multiplication table. | |
class Table | |
def generate_table( | |
table_size, | |
title = '', | |
decoration = '' | |
) | |
result = '' | |
heading = "Times table to #{table_size}\n" | |
table = multiplication_table(table_size) | |
length = table.lines[-1].size | |
border = decoration.to_s * (length) + "\n" | |
result << heading if title == "title" | |
result << border unless decorate? decoration | |
result << table | |
result << border unless decorate? decoration | |
result | |
end | |
private | |
def decorate?(decoration) | |
[ :nil? , :empty?, :blank?].any? do |method_name| | |
decoration.respond_to?(method_name) && decoration.send(method_name) | |
end | |
end | |
def multiplication_table(table_size) | |
table = '' | |
1.upto(table_size) do |i| | |
row = '' | |
1.upto(table_size) do |j| | |
row << build_row(table_size, [i, j]) | |
end | |
table << row << "\n" | |
end | |
table | |
end | |
def build_row(table_size, multiplands) | |
i, j = multiplands | |
# i = numbers[0] | |
# j = numbers[1] | |
' ' << spacing(product(i, j)[:length], spaces(table_size) + 1) << | |
product(i, j)[:value].to_s | |
end | |
def spaces(integer) | |
integer.to_s.size | |
end | |
def product(i, j) | |
{ value: i * j, length: (i * j).to_s.length } | |
end | |
def spacing(num_size, col_size) | |
num_size < col_size ? ' ' * (col_size - num_size) : '' | |
end | |
end | |
## This is a pass / fail test. To get get graded test just load it | |
## in irb and run or use the inspect method. | |
# doctest: Acceptance test & with no title but with decoration | |
# >> standard = "++++++++++\n 1 2 3\n 2 4 6\n 3 6 9\n++++++++++\n" | |
# >> Table.new().generate_table(3, "", "+") == standard | |
# => true | |
# doctest: Acceptance test with title and no decoration | |
# >> standard = "Times table to 3\n 1 2 3\n 2 4 6\n 3 6 9\n" | |
# >> Table.new().generate_table(3, "title") == standard | |
# => true | |
# doctest: Acceptance test with no decoration & no title | |
# >> standard = " 1 2 3\n 2 4 6\n 3 6 9\n" | |
# >> Table.new().generate_table(3, '', '') == standard | |
# => true | |
# doctest: Acceptance test with larger multiplands | |
# >> standard = " 1 2 3 4\n 2 4 6 8\n 3 6 9 12\n 4 8 12 16\n" | |
# >> Table.new().generate_table(4, '', '') == standard | |
# => true | |
# doctest: Acceptance test & with title and decoration | |
# >> standard ="Times table to 3\n++++++++++\n 1 2 3\n 2 4 6\n 3 6 9\n++++++++++\n" | |
# >> Table.new().generate_table(3, "title", "+") == standard | |
# => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment