Created
March 16, 2012 20:00
-
-
Save gberenfield/2052257 to your computer and use it in GitHub Desktop.
Week 1 - problem #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns codelesson.week1 | |
(:require [clojure.math.combinatorics :as mc])) | |
(defn add-up [coeffs terms] | |
"ax + by + cz + ... | |
this adds up a Specific set of coefficients for terms(coins)" | |
(reduce + (map #(* %1 %2) coeffs terms))) | |
(defn coeffs [dollar coin] | |
"return a sequence of coeffs from 0 to the divisor of the dollar amt by the coin | |
this is the range of possible coefficients for a given coin!" | |
(vec (range (inc (/ dollar coin))))) | |
(defn coeffs-for-all-terms [dollar coins] | |
"creates a map of coins to their range of possible coefficients that add up to dollar" | |
(loop [coin (first coins) | |
rcoins (rest coins) | |
allcoeffs (array-map coin (coeffs dollar coin)) ] | |
(if-not (empty? rcoins) | |
(recur (first rcoins) (rest rcoins) (assoc allcoeffs (first rcoins) ( coeffs dollar (first rcoins)))) | |
allcoeffs))) | |
(defn coeff-seq [dollar coins] | |
"A sequence of possible coeffecients for the coins to add up to dollar | |
may need lazy seq...this is huge!" | |
(apply mc/cartesian-product (vals (coeffs-for-all-terms dollar (sort coins))))) | |
(defn less-brutal-binomnoms [coins dollar] | |
(let [massive-possibilities (coeff-seq dollar coins)] | |
(count | |
(filter #(= dollar (add-up % coins)) massive-possibilities)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment