Last active
November 18, 2023 01:01
-
-
Save chase-lambert/44783853e7f55ac8effeeb057895111f to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 23-11-15
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 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