Hi everybody,
here's my solution, next to few examples.
The result might look pretty poor displayed on an email client :-(, but the
script does print a perfectly equilateral triangle.
Thanks for the interesting quiz!
$ ./pascal.rb 12
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
$ ./pascal.rb 12 -1
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
$ ./pascal.rb 12 1
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
$ ./pascal.rb 12 0.7
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
By tweaking a bit the script you can easily obtain a Sierpinski triangle, as
mentioned in a previous post:
./sierpinski.rb 32
*
* * *
* *
* * *
* * * * * * *
* * * * * *
* * * * * * *
* * * *
* * * * * * * * *
* * * * * *
* * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
#! /usr/bin/ruby
#
############### Pascal Triangle by Eric DUMINIL ###############
#
# This program is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 2 of the License, or (at your option) any
# later version.
#
# How-to use it?
# pascal.rb height excentricity
# with:
# height, pretty self-explanatory
# excentricity, float between -1 and 1.
#
# height set to 5 and excentricity set to -1:
#
# 1
# 1 1
# 1 2 1
# 1 3 3 1
# 1 4 6 4 1
#
# height set to 5 and excentricity set to 1 :
#
# 1
# 1 1
# 1 2 1
# 1 3 3 1
# 1 4 6 4 1
#
#
# By default, excentricity is set to 0:
#
# 1
# 1 1
# 1 2 1
# 1 3 3 1
# 1 4 6 4 1
#
#Just to make sure we can calculate C(n,n/2)
#without having to build the whole tree
class Fixnum
def fact
return 1 if self<2
self*(self-1).fact
end
def cnp(p)
self.fact/(p.fact*(self-p).fact)
end
end
class PascalTriangle
def initialize (height=15,excentricity=0)
@height=height
@excentricity=excentricity
#maxLength should be odd, so that the alignment is preserved
@maxLength=(height-1).cnp((height-1)/2).to_s.length|1
createAndShow
end
attr_reader :height, :maxLength, :excentricity
def createAndShow
previous=[1]
current=Array.new
height.times{|i|
current[0]=current[i]=1
#Taking care of the symetry
1.upto(i/2){|j|
current[j]=current[i-j]=previous[j]+previous[j-1]
}
puts " "*((maxLength+1)*(excentricity+1)/2)*(height-i-1)+
current.map{|number|
number.to_s.rjust(maxLength)
}.join(" ")
#No need to remember the whole triangle,
#the previous row will be enough
previous.replace(current)
}
end
end
PascalTriangle.new(ARGV[0].to_i,ARGV[1].to_f)
###############################################################################
Bye,
Eric
Le Vendredi 23 Juin 2006 15:31, Ruby Quiz a crit:
> The three rules of Ruby Quiz:
>
> 1. Please do not post any solutions or spoiler discussion for this quiz
> until 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz by submitting ideas as often as you can:
>
> http://www.rubyquiz.com/
>
> 3. Enjoy!
>
> Suggestion: A [QUIZ] in the subject of emails about the problem helps
> everyone on Ruby Talk follow the discussion. Please reply to the original
> quiz message, if you can.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>=-=-=
>
> by Dirk Meijer
>
> I recently showed a friend what an amazing language Ruby was, by quickly
> programming up a script to calculate Fibonacci's Sequence, and his first
> response was: "Can you do Pascal's Triangle?" So I did, which proved
> harder than expected.
>
> For those not familiar with Pascal's Triangle, it is very similar to
> Fibonacci's Sequence. It's a pyramid of numbers. The outside of the pyramid
> is all ones, the other numbers are the sum of the two numbers above, like
> this:
>
> 1
> 1 1
> 1 2 1
> 1 3 3 1
> 1 4 6 4 1
>
> The input and output should be as follows:
>
> $ ruby pp_pascal.rb 10
> 1
> 1 1
> 1 2 1
> 1 3 3 1
> 1 4 6 4 1
> 1 5 10 10 5 1
> 1 6 15 20 15 6 1
> 1 7 21 35 35 21 7 1
> 1 8 28 56 70 56 28 8 1
> 1 9 36 84 126 126 84 36 9 1
>
> A number should be given as command-line argument. This is the amount of
> rows the triangle has. For the output, there should be spacing between the
> numbers based on the size of the numbers with the most digits, so it will
> look like a proper triangle.
>
> Good luck!
>
> [Editor's Note: If you are working through Chris Pine's Learn to Program,
> you can do this problem using only things you learned in the first eight
> chapters. Since he doesn't teach how to grab the row count in those pages
> though, just add this as the first line of your program: `rows =
> ARGV[0].to_i]`. After that, the rows variable will hold the number of rows
> to print. --JEG2]