--OgqxwSJOaUobr8KG
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Hi all,
Here's my solution to the quiz:
I attached two different solutions actually.
First is pascal.rb, which is the first I did before asking for
any clarifications/hints :) The triangle resulted is far from
perfect, but I liked being able to use the 'multi' module which
I just learned about :)
The second, pascal-mini, does output a perfect triangle, and allows for
playing with two options: 'B' and 'S'. B represents the numerotation
based. By default it is 10, so nothing unusual, but you can change it
to 16 and display hexa numbers, or even to 32 and display numbers in
the 32 based numerotation system :) Looks nicer and more compact for
larger triangles. All triangles in the attached sample.txt are from
this script. The Ruby code however is very ugly I agree, I just wanted
to find out how much I can squeeze it :)
Thanks for the fun Ruby quiz!
Have a great day everyone,
Alex
--OgqxwSJOaUobr8KG
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="pascal-mini.rb"
f oc{|n|n>1?n*f[n-1]:1} # Factorial
c oc{|r,i|f[r]/(f[i]*f[r-i])} # Compute cell[r(ow),i(ndex)]
t,rows,B,S ,(ARGV[0]||10).to_i,10,' '; Wows-1,rows/2].to_s(B).length # Init vars
rows.times{|r|l ;(r+1).times{|i|l<<(((s¥¿r,i].to_s(B)).length<W)?S*(W-s.length)+s:s).upcase};t<< l}
tl -1].join(' ').length; t.map{|r|c join(' ');puts ' '*((tl-c.length)/2)<< c} # Display result
--OgqxwSJOaUobr8KG
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="pascal.rb"
require 'rubygems'
require 'multi'
SEP '*3; rows ARGV[0] || 10).to_i
multi(:pascal, [1]) { [1] }
multi(:pascal, [2]) { [1, 1] }
multi(:pascal, Array) { |a| result]; 0.upto(a.length-2){ |i| result << a[i]+a[i+1] }; result+[1] }
last, triangle ], []
rows.times { |n| triangle << last n<2) ? pascal([n+1]) : pascal(last) }
len riangle[-1].join(SEP).length
triangle.map { |row| c ow.join(SEP); puts ' '*((len - c.length) / 2) << c }
--OgqxwSJOaUobr8KG
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="samples.txt"
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
001
001 001
001 002 001
001 003 003 001
001 004 006 004 001
001 005 010 010 005 001
001 006 015 020 015 006 001
001 007 021 035 035 021 007 001
001 008 028 056 070 056 028 008 001
001 009 036 084 126 126 084 036 009 001
01
01 01
01 02 01
01 03 03 01
01 04 06 04 01
01 05 0A 0A 05 01
01 06 0F 14 0F 06 01
01 07 15 23 23 15 07 01
01 08 1C 38 46 38 1C 08 01
01 09 24 54 7E 7E 54 24 09 01
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 A A 5 1
1 6 F K F 6 1
1 7 L 13 13 L 7 1
1 8 S 1O 26 1O S 8 1
1 9 14 2K 3U 3U 2K 14 9 1
1 A 1D 3O 6I 7S 6I 3O 1D A 1
1 B 1N 55 AA EE EE AA 55 1N B 1
1 C 22 6S FF OO SS OO FF 6S 22 C 1
1 D 2E 8U MB 187 1LK 1LK 187 MB 8U 2E D 1
1 E 2R BC V9 1UI 2TR 3B8 2TR 1UI V9 BC 2R E 1
1 F 39 E7 1AL 2TR 4SD 693 693 4SD 2TR 1AL E7 39 F 1
1 G 3O HG 1OS 48G 7Q8 B5G CI6 B5G 7Q8 48G 1OS HG 3O G 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
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1
--OgqxwSJOaUobr8KG--