Skip to content

Instantly share code, notes, and snippets.

@chase-lambert
Last active November 18, 2023 01:01
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 chase-lambert/44783853e7f55ac8effeeb057895111f to your computer and use it in GitHub Desktop.
Save chase-lambert/44783853e7f55ac8effeeb057895111f to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 23-11-15
(ns do-tasks
(:require [clojure.test :refer [deftest is]]))
(defn do-tasks [tasks time-to-work]
(let [time-remaining (atom time-to-work)
sorted-tasks (sort-by :duration tasks)
completed-tasks (set
(reduce (fn [completed-tasks next-task]
(if (>= @time-remaining (:duration next-task))
(do
(swap! time-remaining - (:duration next-task))
(conj completed-tasks next-task))
(reduced completed-tasks)))
[]
sorted-tasks))]
(for [task tasks
:when (contains? completed-tasks task)]
(:name task))))
(deftest do-tasks-test
(let [tasks [{:name "Task 1" :duration 4}
{:name "Task 2" :duration 2}
{:name "Task 3" :duration 7}
{:name "Task 4" :duration 5}
{:name "Task 5" :duration 1}
{:name "Task 6" :duration 3}]]
(is (= ["Task 2" "Task 5" "Task 6"] (do-tasks tasks 6)))
(is (= ["Task 2" "Task 5"] (do-tasks tasks 4)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment