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 Q. a écrit :