Tuesday, July 14, 2015

Exercism: "Largest Series Product in Clojure"

I solved the Largest Series Product problem in Clojure.

This is my solution:

(ns largest-series-product)
(defn digits [string-of-digits]
(map #(Integer/parseInt (str %))
string-of-digits))
(defn slices [size string-of-digits]
(partition size 1 (digits string-of-digits)))
(defn- compute-largest-product [size string-of-digits]
(->> string-of-digits
(slices size)
(map (partial apply *))
(apply max)))
(defn- invalid-input? [size string-of-digits]
(let [too-small-input? #(> size (count string-of-digits))
empty-input? #(clojure.string/blank? string-of-digits)]
(or (empty-input?) (too-small-input?))))
(defn largest-product [size string-of-digits]
(if (invalid-input? size string-of-digits)
1
(compute-largest-product size string-of-digits)))
This Exercism problem is based on a Project Euler's exercise I solved some time ago.

You can nitpick my solution here and/or see all the exercises I've done so far in this repository.

No comments:

Post a Comment