3
\$\begingroup\$

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

6 Answers 6

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\$
5
  • 1
    \$\begingroup\$ The phrase "executed from a calculator" in the context of code-golf makes me smile. \$\endgroup\$ Commented Dec 31, 2013 at 18:55
  • 2
    \$\begingroup\$ @DarrenStone When code golf = math, it's time to pull out the calculator. \$\endgroup\$
    – Timtech
    Commented Dec 31, 2013 at 18:57
  • \$\begingroup\$ WINNER! Congratulations! \$\endgroup\$ Commented Jan 7, 2014 at 17:16
  • \$\begingroup\$ @BobJarvis Thanks! I'm happy :) First code-golf competition I've ever won! \$\endgroup\$
    – Timtech
    Commented Jan 7, 2014 at 22:04
  • \$\begingroup\$ What does this compile to? \$\endgroup\$
    – lirtosiast
    Commented Jun 8, 2015 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\$
1
  • \$\begingroup\$ Ah, brings back fond memories of my days in college, back at dear Miskatonic U... Ia! Ia! Cthulhu fhtagn!!! \$\endgroup\$ Commented Dec 31, 2013 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\$
3
  • \$\begingroup\$ codegolf.stackexchange.com/a/3822/194 \$\endgroup\$ Commented Dec 31, 2013 at 19:04
  • \$\begingroup\$ @PeterTaylor Ups, didn't realize that. Reinvented the method on my own ;-) \$\endgroup\$
    – Howard
    Commented Dec 31, 2013 at 19:05
  • \$\begingroup\$ @PeterTaylor Voted to close as duplicate - although different it is quite similar. \$\endgroup\$
    – Howard
    Commented Dec 31, 2013 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\$
2
  • \$\begingroup\$ Please add an explanation for this code since iX2Web doesn't appear to have a free interpreter. \$\endgroup\$
    – lirtosiast
    Commented Oct 5, 2015 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
    Commented Oct 5, 2015 at 23:57

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