-
-
Save brpxqzme/5d7c3fa70dfff0962232 to your computer and use it in GitHub Desktop.
Coding for Interviews Issue #18 challenge: dynamic programming
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
(defun change-combinations (amount denominations) | |
"Return the number of (orderless) ways change can be made for integer AMOUNT | |
given a list of integer DENOMINATIONS." | |
(let ((dyntable (make-array (1+ amount) :initial-element 0))) | |
(setf (elt dyntable 0) 1) | |
(loop for denom in denominations | |
do (loop for dyndex from denom to amount | |
do (incf (elt dyntable dyndex) | |
(elt dyntable (- dyndex denom))))) | |
(elt dyntable amount))) | |
(defun csv-to-ints (csvstring) | |
"Return a list of integers given a string of comma-separated values. | |
Input presumed valid (whitespace is okay)." | |
(let ((strvals (loop for i = 0 then (1+ j) | |
as j = (position #\, csvstring :start i) | |
collect (subseq csvstring i j) | |
while j))) | |
(mapcar #'parse-integer strvals))) | |
(let ((coins (csv-to-ints (read-line))) | |
(amount (parse-integer (read-line)))) | |
(print (change-combinations amount coins))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(In practice, though, you'd probably prefer to install CL-UTILITIES and use SPLIT-SEQUENCE to get that list, and maybe do more string cleanup.)