September 25, 2018

Just Juxt #42: Pascal's trapezoid (4clojure #147)

Pascal

Write a function that, for any given input vector of numbers, returns an infinite lazy sequence of vectors, where each next one is constructed from the previous following the rules used in Pascal's Triangle. For example, for [3 1 2], the next row is [3 4 3 2].

Beware of arithmetic overflow! In clojure (since version 1.3 in 2011), if you use an arithmetic operator like + and the result is too large to fit into a 64-bit integer, an exception is thrown. You can use +' to indicate that you would rather overflow into Clojure's slower, arbitrary-precision bigint.

(ns live.test
  (:require [cljs.test :refer-macros [deftest is run-tests]]))
  
(defn pascal [ss]
  (iterate (fn [v] (apply map +(map #(conj % 0)
                                     ((juxt vec seq) v)))) ss))

(deftest pascal-test
  (is (= (second (pascal [2 3 2])) [2 5 5 2]))
  (is (= (take 5 (pascal [1])) [[1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1]]))
  (is (= (take 2 (pascal [3 1 2])) [[3 1 2] [3 4 3 2]]))
  (is (= (take 100 (pascal [2 4 2])) (rest (take 101 (pascal [2 2]))))))
  
(run-tests)
Tags: coding exercises KLIPSE 4clojure Cryogen juxt