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!
r=0
, would the appropriate result be[3 1 2]
? And no negative values ofr
? \$\endgroup\$[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\$