# Pp Pascal (#84)

The three rules of Ruby Q.:

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 Q. by submitting ideas as often as you can:

http://www.rubyquiz.com/

1. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone
message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Dirk M.

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 P.'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,
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]

quoth the Alexandru E. Ungur

``````       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

conform with the ‘proper triangle’ definition?

Wikipedia* says: “a polygon with three vertices and three sides which
are
straight line segments” so I guess the above is a ‘proper’ triangle by
anyone’s definition.

Thanks,
Alex

``````           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.
question:

``````Does this triangle:
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

conform with the ‘proper triangle’ definition?

Thanks,
Alex

On Jun 23, 2006, at 17:20, Cliff C. wrote:

``````               1     4     6     4     1
``````

straight line segments" so I guess the above is a ‘proper’
triangle by
anyone’s definition.

Look at the last row. It looks off. Seems that the 1’s on the
outer ends
are off by a space so that it’s not a straight line. But I’m sure
we all get
the point.

It’s not the last row, but each row where the length of the longest
entry changes - row 5 is off as well.

What’s the policy on hints before the waiting period is up?

matthew smillie.

On Sat, Jun 24, 2006 at 12:45:21AM +0900, darren kirby wrote:

``````           1     5     10     10     5     1
``````

Look at the last row. It looks off. Seems that the 1’s on the outer
ends
are off by a space so that it’s not a straight line. But I’m sure we
all get
the point.

On Jun 23, 2006, at 12:30 PM, Matthew S. wrote:

``````                  1     3     3     1
``````

which are

It’s not the last row, but each row where the length of the longest
entry changes - row 5 is off as well.

What’s the policy on hints before the waiting period is up?

matthew smillie.

Anyone have any critique on this one?

``````                         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

-Mat

On 6/23/06, Mat S. [email protected] wrote:

`````` 1     8    28    56    70    56    28     8     1
``````

1 9 36 84 126 126 84 36 9 1
A) — – -- – … (spaces between the ones) … – -- -
B) ^^ ^^ ^^ ^^ ^^ ^^

A) The amount of horizontal space between the ones on the top “edge”
of the triangle isn’t consitent for the last row. All the other rows
have two spaces (three characters with the one) per column, but the
lead and final columns for the last row are off.

B) It looks like you’re intending to right align all the columns, and
it’s working for the first rows, but for the last row the alignment is
off for all the numbers with fewer than three digits. My first thought
was that you were just missing one leading space on the whole row, as
adding one fixes the both (A) and the alignment for all the one and
two digit numbers – but then the alignment on the 126’s is off.

Jacob F.

``````                       1     1
``````

we all get
the point.

It’s not the last row, but each row where the length of the longest
entry changes - row 5 is off as well.

What’s the policy on hints before the waiting period is up?
I’m also curious what is the policy in asking for clarifications,
and also if :clarifications == :hints or not…

I did not ask for hints, I just wanted to understand completely
the request. My problem was that, indeed, the triangle I obtained
has some lines not perfectly straight. I agree.
However, the triangle shown as an example in the quiz description
also has lines that are not straight, just look careful enough,
and you will notice them.
So, there’s where my confusion comes from… and that’s what I wanted
to get this clarified, if possible/allowed, of course.

Thanks,
Alex

For another display example, up to row 15:

(Obviously this won’t display well in 80 columns, you’ll want to copy
it out to a wide fixed width scratchpad to see it best. But it does
make pretty patterns when it wraps

\$ ruby pascal.rb 15
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

Jacob F.

quoth the Cliff C.:

``````               1     4     6     4     1
``````

anyone’s definition.

Look at the last row. It looks off. Seems that the 1’s on the outer ends
are off by a space so that it’s not a straight line. But I’m sure we all
get the point.

OK, sure it’s a little off. I thought maybe it was just from my mail
(not a monospaced font ATM). In any event, I took the ‘proper triangle’
as
meaning the output should be laid out with three sides, skinny on top
and fat
on the bottom, not necessarily as a perfectly spaced equilateral
triangle.

-d

On Jun 23, 2006, at 11:49 AM, Alexandru E. Ungur wrote:

What’s the policy on hints before the waiting period is up?
I’m also curious what is the policy in asking for clarifications,
and also if :clarifications == :hints or not…

I’m fine with most discussion. Let’s let code out of it, and try not
to give away big secrets. Just helping people along is fine though.

I did not ask for hints, I just wanted to understand completely
the request.

I saw nothing wrong with your request.

However, the triangle shown as an example in the quiz description
also has lines that are not straight, just look careful enough,
and you will notice them.
So, there’s where my confusion comes from… and that’s what I wanted
to get this clarified, if possible/allowed, of course.

The quiz example reflects what the specification meant to me. Now
you’ll need to decide what it means to you.

Here’s a fun thought: which spaces would you remove from the quiz
example to “fix” it?

James Edward G. II

On 6/23/06, darren kirby [email protected] wrote:

OK, sure it’s a little off. I thought maybe it was just from my mail reader
(not a monospaced font ATM). In any event, I took the ‘proper triangle’ as
meaning the output should be laid out with three sides, skinny on top and fat
on the bottom, not necessarily as a perfectly spaced equilateral triangle.

I agree, it doesn’t need to be perfectly equilateral, etc. I’ve
actually had a litlle fun toying with my output mechanism to produce
slightly skewed columns (take a right aligned column, then shift each
number slightly so that the column looks like it’s leaning), right
triangles, etc. But the important thing is consistency – half the
problem in this quiz is working the output right so that you’re output
is in “columns” (whether or not those columns are straight up and
down). The “problem” with yours was that inconsistency in the columns.

But I don’t think anyone that’s not pedantic about it (like me) will
really care, so don’t worry too much about it.

Jacob F.

Did you know that you need only 36 bytes of Ruby code to build
the next row, based on the previous row? Really!

And that the 1000-rows-triangle eats 599.701.000 bytes of disk
space and approximately 82.116.176.508 clock cycles?..

gegroet,
Erik V. - http://www.erikveen.dds.nl/

Jacob F. wrote:

For another display example, up to row 15:

(Obviously this won’t display well in 80 columns, you’ll want to copy
it out to a wide fixed width scratchpad to see it best. But it does
make pretty patterns when it wraps

indeed, but up to 15 rows you can squeeze it in 80 columns:

``````                               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

and i think the one’s are on a perfect line (it’s just that the gradient
is a fraction)

cheers

Simon

I suppose that, due to the nature of Pascal’s triangle, the important
thing is being able to see visually which two numbers are the
“parents” for any given number. Given the example in the quiz, you
can clearly see that P[8,3] 21 comes from adding P[7,2] 6 and P[7,3]
15–even if the lines aren’t straight

Interestingly enough, it might be easier to generate postscript or pdf
output than trying to space out the numbers using fixed width
characters. That might be cool if anyone wanted to give it a shot.

On Jun 23, 2006, at 11:30 AM, Matthew S. wrote:

What’s the policy on hints before the waiting period is up?

Hints are fine. Just give them without code please.

James Edward G. II

Alexandru E. Ungur wrote:

sender: “James Edward G. II” date: “Sat, Jun 24, 2006 at 02:42:50AM +0900” <<<EOQ
[…]
The quiz example reflects what the specification meant to me. Now
you’ll need to decide what it means to you.

I must say through tweaking my output i have figured out a way as far as
i can tell to make sure the triangle is always equalateral i can’t test
it past row 20 because wrapping messes up the output. But it gaurantees
at least one space between numbers, it is very readable until around row
15 when 5 digit numbers start showing up and then my command line starts
getting a little hard to read. Very fun quiz one that i can finally
dable in.

Thanks for the entertainment
Tait P.

sender: “James Edward G. II” date: “Sat, Jun 24, 2006 at 02:42:50AM +0900” <<<EOQ
[…]
The quiz example reflects what the specification meant to me. Now
you’ll need to decide what it means to you.
I didn’t know that, that’s why I was trying so hard to understand
exactly what you meant, so that I could give a valid solution
Now, if I can decide what it means to me, that’s fine by me

The thing that I didn’t quite get is how to get them aligned perfectly?
What would perfect mean in this particular case? Align them on the left
side or the right side of the number?

For example, in the example triangle, if you remove a space in front
of the first 126 and add it after the second 126, what you get is
all numbers perfectly aligned on the right side of every number…
However you get the left side messed…

So, you need to compromise, no matter what… anyway, since I can
choose my own compromise, I’ll shut up

and thank you all!
Alex

On Jun 23, 2006, at 2:29 PM, Alexandru E. Ungur wrote:

So, you need to compromise, no matter what… anyway, since I can
choose my own compromise, I’ll shut up

I quite enjoyed right aligning on one side, left on the other and
centering in the center.

I don’t know if Dirk M.'s on the list, but thanks for the
exercise! I like these little ones that are easy to get going but
tricky to really smooth out.
-Mat

On Jun 23, 2006, at 1:29 PM, Alexandru E. Ungur wrote:

sender: “James Edward G. II” date: “Sat, Jun 24, 2006 at
02:42:50AM +0900” <<<EOQ
[…]
The quiz example reflects what the specification meant to me. Now
you’ll need to decide what it means to you.
I didn’t know that, that’s why I was trying so hard to understand
exactly what you meant, so that I could give a valid solution

To me it meant:

1. Find the longest number in the result set.
2. Center all numbers in a field that size.
3. Find the length of the last line.
4. Center all lines at that length.

Now, if I can decide what it means to me, that’s fine by me

I promise, I wont come and take your keyboard away, no matter what
you decide.

James Edward G. II

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.