3
\$\begingroup\$

This question already has an answer here:

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!

\$\endgroup\$

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ To clarify... in your example, if r=0, would the appropriate result be [3 1 2]? And no negative values of r? \$\endgroup\$ – Darren Stone Dec 31 '13 at 18:52
  • \$\begingroup\$ @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"... \$\endgroup\$ – Bob Jarvis Dec 31 '13 at 18:59
  • \$\begingroup\$ Sheesh! Three answers, and I like 'em all enough to upvote 'em. I'm too easy... \$\endgroup\$ – Bob Jarvis Dec 31 '13 at 19:03
  • \$\begingroup\$ 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) \$\endgroup\$ – Bob Jarvis Jan 7 '14 at 17:20
4
\$\begingroup\$

Golf-Basic 84 - 10 characters

Executed from a TI-84 calculator.

i`N,Rd`N11

Sample run

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

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.

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

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]
\$\endgroup\$
  • \$\begingroup\$ codegolf.stackexchange.com/a/3822/194 \$\endgroup\$ – Peter Taylor Dec 31 '13 at 19:04
  • \$\begingroup\$ @PeterTaylor Ups, didn't realize that. Reinvented the method on my own ;-) \$\endgroup\$ – Howard Dec 31 '13 at 19:05
  • \$\begingroup\$ @PeterTaylor Voted to close as duplicate - although different it is quite similar. \$\endgroup\$ – Howard Dec 31 '13 at 19:17
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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}
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Please add an explanation for this code since iX2Web doesn't appear to have a free interpreter. \$\endgroup\$ – lirtosiast Oct 5 '15 at 21:02
  • \$\begingroup\$ 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). \$\endgroup\$ – Timtech Oct 5 '15 at 23:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.