Last active
December 15, 2015 21:18
-
-
Save ScottGo/5324243 to your computer and use it in GitHub Desktop.
20130405_printing out a times 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
def times_table(rows) | |
first_row = [] | |
int = 5 | |
1.upto(5) do |i| | |
first_row.push(i) | |
end | |
first_row | |
end | |
#I'm not sure from the instructions below if the integer is known/given or if I have to determine it? | |
#Is the integer 5 or 500? | |
#I have a feeling I'm going to need multiple (2?) loops, one nested in the other | |
#if integer == 0 print out an empty table | |
#if integer == 1 print out "1" | |
#the next possible integer is 2..x (infinity), | |
#if integer == 2, the first row would be all 1's (1 1) and the second row 2, (2 *= 2) | |
#if it was "2" the tabel would look like: | |
#1 1 | |
#2 2 | |
#I know the first possible integer is 2, from that point, it could be anything else | |
#should I just assume for the sake of this exercise that the integer is 5 OR, | |
#can I determine what the integer is? |
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
# This is a crazy regular expression which matches the output | |
# of the 5x5 times table in a way that doesn't care how you | |
# separate columns, so long as they're separated by whitespace | |
REGEX_FIVE = /1[ \t]+2[ \t]+3[ \t]+4[ \t]+5\s*\n2[ \t]+4[ \t]+6[ \t]+8[ \t]+10\s*\n3[ \t]+6[ \t]+9[ \t]+12[ \t]+15\s*\n4[ \t]+8[ \t]+12[ \t]+16[ \t]+20\s*\n5[ \t]+10[ \t]+15[ \t]+20[ \t]+25\s*$/m | |
describe 'times_table' do | |
before(:each) do | |
@output = StringIO.new | |
$stdout = @output | |
end | |
it "prints nothing when called with 0" do | |
times_table(0) | |
@output.rewind | |
@output.read.should be_empty | |
end | |
it "correctly prints a 1x1 times table" do | |
times_table(1) | |
@output.rewind | |
@output.read.should =~ /1\s*$/ | |
end | |
it "correctly prints a 5x5 times table" do | |
times_table(5) | |
@output.rewind | |
@output.read.should =~ REGEX_FIVE | |
end | |
after(:each) do | |
$stdout = STDOUT | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.