Last active
December 15, 2015 21:49
-
-
Save ScottGo/5328213 to your computer and use it in GitHub Desktop.
Exercise: Print out a pretty right triangle
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
describe 'print_triangle' do | |
before(:each) do | |
@output = StringIO.new | |
@old_stdout = $stdout | |
$stdout = @output | |
end | |
it "prints nothing if given 0" do | |
print_triangle(0) | |
@output.rewind | |
@output.read.should be_empty | |
end | |
it "prints '*' if given 1" do | |
print_triangle(1) | |
@output.rewind | |
@output.read.should eq "*\n" | |
end | |
it "prints a 5-row triangle" do | |
print_triangle(5) | |
@output.rewind | |
@output.read.should eq "*\n**\n***\n****\n*****\n" | |
end | |
it "has the correct last line for a 100-row triangle" do | |
print_triangle(100) | |
@output.rewind | |
@output.read.split("\n").last.should eq '*'*100 | |
end | |
after(:each) do | |
$stdout = @old_stdout | |
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_triangle(rows) prints out a right triangle of +rows+ rows consisting | |
# of * characters | |
# | |
# +rows+ is an integer | |
# | |
# For example, print_triangle(4) should print out the following: | |
# * | |
# ** | |
# *** | |
# **** | |
def print_triangle(rows) | |
# Your code goes here! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment