Created
February 27, 2013 04:49
-
-
Save astamm78/5045166 to your computer and use it in GitHub Desktop.
Times Table
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
# Implement a method called times_table which takes as its input an | |
# integer and prints out a times table with that number of rows. | |
# The numbers can be separated by any spaces or tabs, but each row must | |
# be on a new line. This means it's ok if the columns don't line up. | |
# For example, times_table(5) should print the following out to the screen: | |
# 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 | |
# Again, you don't need to worry about the spacing between columns. The point | |
# of the exercise is to understand the logic, not master the formatting. You | |
# should be at least one space/tab between the numbers, though, otherwise it | |
# won't look like a times table! | |
def times_table(rows) | |
start = 1 | |
mult = 1 | |
line = [] | |
until start > rows | |
line.push(start) | |
start += 1 | |
end | |
until mult > rows | |
result = [] | |
line.each do |var| | |
result.push(var * mult) | |
end | |
mult += 1 | |
puts result.join(" ") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment