Created
February 6, 2013 15:42
-
-
Save flyingmachine/4723386 to your computer and use it in GitHub Desktop.
dissipation in two languages
This file contains hidden or 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
(defn dissipation | |
[decrements] | |
(let [size (count decrements)] | |
(loop [acc [] | |
sum size | |
acc-position 0 | |
decrements decrements] | |
(cond | |
(= size (count acc)) | |
acc | |
(or (empty? decrements) (> (first decrements) acc-position)) | |
(recur (conj acc sum) sum (inc acc-position) decrements) | |
:else | |
(recur acc (dec sum) acc-position (rest decrements)))))) |
This file contains hidden or 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
function dissipation(decrements) { | |
var size = decrements.length; | |
var dissipate = function(acc, sum, accPosition, decrements) { | |
if(size == acc.length){ | |
return acc; | |
} else if (decrements[0] <= accPosition) { | |
return dissipate(acc, sum - 1, accPosition, _.rest(decrements)); | |
} else { | |
acc.push(sum) | |
return dissipate(acc, sum, accPosition + 1, decrements); | |
} | |
} | |
return dissipate([], size, 0, decrements); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment