Skip to content

Instantly share code, notes, and snippets.

@flyingmachine
Created February 6, 2013 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flyingmachine/4723386 to your computer and use it in GitHub Desktop.
Save flyingmachine/4723386 to your computer and use it in GitHub Desktop.
dissipation in two languages
(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))))))
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