# Pascal's Trapezoid [duplicate]

Write a function which for any given input array of numbers and a row number r, returns the r-th vector or array where each subsequent vector or array is constructed from the previous following the rules used in Pascal's Triangle. For example, for an input vector of [3 1 2] and a row number of 1 (indicating the 'next' row), the correct return vector or array is [3 4 3 2].

As an example, a Clojure solution to this problem might be:

(defn pascals-trapezoid-row [r n]
(last (take (+ n 1) ((fn p [c] (cons c (lazy-seq (p (flatten (conj [(first c)] (map #(apply +' %) (partition 2 1 c)) [(last c)] )))))) r))))


The winning and accepted solution will be the one with the fewest number of characters in the solution code. Legibility is not a requirement. You're on your honor as to whether or not your proposed solution actually works, but remember that while cheaters often win nobody likes them very much, and karma is real (except in Fortran). Solutions must be posted by 07-Jan-2014 at 12:00 noon Eastern Standard Time (-5 timezone) to be considered. Extra credit (i.e. upvotes) will be awarded by me (and perhaps others) for interesting or novel solutions, including but not limited to those done in unusual languages, one-liners, unbelievably ugly solutions, unbelievably beautiful solutions, and anything I happen to like. I will not downvote any answer, although I make no guarantees about what others may do.

GO!

## marked as duplicate by Peter Taylor, Howard, ProgramFOX, Justin, Konrad BorowskiJan 1 '14 at 10:36

• To clarify... in your example, if r=0, would the appropriate result be [3 1 2]? And no negative values of r? – Darren Stone Dec 31 '13 at 18:52
• @DarrenStone: in the case of the code I posted, r=1 would result in '[3 4 3 2]' and an arg of 0 would result in [3 1 2]. I'm not going to be all that picky about "is r an index or an offset?", and I'll always supply a zero-or-positive row number - but make it work, argue that you're consistent with whatever, and I'm going to go with it. I'd rather have a good time than argue over "rules"... – Bob Jarvis Dec 31 '13 at 18:59
• Sheesh! Three answers, and I like 'em all enough to upvote 'em. I'm too easy... – Bob Jarvis Dec 31 '13 at 19:03
• And our winner is @Timtech, whose solution has pushed back the frontiers of computing+! In addition to having his answer accepted, he will also be receiving an autographed set of Ginsu steak knives++, a lifetime supply of McDonalds gift certificates+++, and a lifetime subscription to Code Golf Digest++++! Congratulations!! (+ = not really, but it was a cool answer; ++ = blatant hyperbole; +++ = this would be very unhealthy, so you're lucky this is not true; ++++ = no such publication exists, and even if it did this would also be, yes, a lie) – Bob Jarvis Jan 7 '14 at 17:20

## Golf-Basic 84 - 10 characters

Executed from a TI-84 calculator.

iN,RdN11


Sample run

?(3;1;2)            <== Array of numbers
?1                  <== Row number
3432

• The phrase "executed from a calculator" in the context of code-golf makes me smile. – Darren Stone Dec 31 '13 at 18:55
• @DarrenStone When code golf = math, it's time to pull out the calculator. – Timtech Dec 31 '13 at 18:57
• WINNER! Congratulations! – Bob Jarvis Jan 7 '14 at 17:16
• @BobJarvis Thanks! I'm happy :) First code-golf competition I've ever won! – Timtech Jan 7 '14 at 22:04
• What does this compile to? – lirtosiast Jun 8 '15 at 2:43

## APL (16 / 14)

{{2+/0,⍵,0}⍣⍺⍨⍵}


Takes the vector as the right argument, and the row number as the left argument, like so:

      1{{2+/0,⍵,0}⍣⍺⍨⍵}3 1 2
3 4 3 2
2{{2+/0,⍵,0}⍣⍺⍨⍵}3 1 2
3 7 7 5 2
3{{2+/0,⍵,0}⍣⍺⍨⍵}3 1 2
3 10 14 12 7 2


If I get to take input from the keyboard, instead of as a function, I can shave off two characters:

{2+/0,⍵,0}⍣⎕⍨⎕


Takes the vector on the first line and the row number on the second.

• Ah, brings back fond memories of my days in college, back at dear Miskatonic U... Ia! Ia! Cthulhu fhtagn!!! – Bob Jarvis Dec 31 '13 at 18:54

### GolfScript, 14 characters

{[0\{.@+\}/]}*


Takes input (vector plus number of rows) on top of stack and replaces that by the result. Example (also online):

[3 1 2] 3    {[0\{.@+\}/]}*      # -> [3 10 14 12 7 2]

• codegolf.stackexchange.com/a/3822/194 – Peter Taylor Dec 31 '13 at 19:04
• @PeterTaylor Ups, didn't realize that. Reinvented the method on my own ;-) – Howard Dec 31 '13 at 19:05
• @PeterTaylor Voted to close as duplicate - although different it is quite similar. – Howard Dec 31 '13 at 19:17

## R, 45

function(x,r){for(i in 1:r)x=c(x,0)+c(0,x);x}


Example usage:

fun <- function(x,r){for(i in 1:r)x=c(x,0)+c(0,x);x}

fun(c(3, 1, 2), 1)
[1] 3 4 3 2

fun(c(3, 1, 2), 2)
[1] 3 7 7 5 2

fun(c(3, 1, 2), 5)
[1]  3 16 37 50 45 28 11  2


## Ruby, 51

a=eval$_;a.pop.times{a<<j=0;p a.map!{|i|(v=j)+j=i}}  Run from command line with [array,R] as input. It shows each increment of R along the way. The last line is the solution. For example, if the array is [3,1,2] and R=7: $ ruby -ne 'a=eval\$_;a.pop.times{a<<j=0;p a.map!{|i|(v=j)+j=i}}' <<< [3,1,2,7]
[3, 4, 3, 2]
[3, 7, 7, 5, 2]
[3, 10, 14, 12, 7, 2]
[3, 13, 24, 26, 19, 9, 2]
[3, 16, 37, 50, 45, 28, 11, 2]
[3, 19, 53, 87, 95, 73, 39, 13, 2]
[3, 22, 72, 140, 182, 168, 112, 52, 15, 2]   <-- answer


For the same char count but different calling convention:

lambda{|a,r|r.times{a<<j=0;a.map!{|i|(v=j)+j=i}};a}


## iX2Web, 94

**iX2004FB 800345MTAJ Tj18fAkwDQ oxMAlSPXx8 CTANCjIyCU 89W05dKjEx fEMJMA0KMw lbT118XzAJ MA0K=*


Not likely to beat my Golf-Basic solution. Makes up for it with pretty GUI and output. Windows only.

• Please add an explanation for this code since iX2Web doesn't appear to have a free interpreter. – lirtosiast Oct 5 '15 at 21:02
• It gets input via GUI in the form of [R] and [N] and outputs in [O] (uses instantEXE3 framework, which I stopped using some years ago). – Timtech Oct 5 '15 at 23:57