From 621eea9579e5ea94480ec1aef2e927418eb30aa5 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 30 Sep 2021 12:14:58 -0300 Subject: [PATCH] Exercism: Lucians Luscious Lasagna --- .../.exercism/config.json | 20 ++++ .../.exercism/metadata.json | 1 + clojure/lucians-luscious-lasagna/HELP.md | 75 +++++++++++++ clojure/lucians-luscious-lasagna/HINTS.md | 29 +++++ clojure/lucians-luscious-lasagna/README.md | 101 ++++++++++++++++++ clojure/lucians-luscious-lasagna/deps.edn | 6 ++ clojure/lucians-luscious-lasagna/project.clj | 4 + .../src/lucians_luscious_lasagna.clj | 26 +++++ .../test/lucians_luscious_lasagna_test.clj | 21 ++++ 9 files changed, 283 insertions(+) create mode 100644 clojure/lucians-luscious-lasagna/.exercism/config.json create mode 100644 clojure/lucians-luscious-lasagna/.exercism/metadata.json create mode 100644 clojure/lucians-luscious-lasagna/HELP.md create mode 100644 clojure/lucians-luscious-lasagna/HINTS.md create mode 100644 clojure/lucians-luscious-lasagna/README.md create mode 100644 clojure/lucians-luscious-lasagna/deps.edn create mode 100644 clojure/lucians-luscious-lasagna/project.clj create mode 100644 clojure/lucians-luscious-lasagna/src/lucians_luscious_lasagna.clj create mode 100644 clojure/lucians-luscious-lasagna/test/lucians_luscious_lasagna_test.clj diff --git a/clojure/lucians-luscious-lasagna/.exercism/config.json b/clojure/lucians-luscious-lasagna/.exercism/config.json new file mode 100644 index 0000000..add4966 --- /dev/null +++ b/clojure/lucians-luscious-lasagna/.exercism/config.json @@ -0,0 +1,20 @@ +{ + "blurb": "Learn about the basics of Clojure by following a lasagna recipe", + "authors": [ + "porkostomus" + ], + "forked_from": [ + "fsharp/lucians-luscious-lasagna" + ], + "files": { + "solution": [ + "src/lucians_luscious_lasagna.clj" + ], + "test": [ + "test/lucians_luscious_lasagna_test.clj" + ], + "exemplar": [ + ".meta/exemplar.clj" + ] + } +} diff --git a/clojure/lucians-luscious-lasagna/.exercism/metadata.json b/clojure/lucians-luscious-lasagna/.exercism/metadata.json new file mode 100644 index 0000000..9d74169 --- /dev/null +++ b/clojure/lucians-luscious-lasagna/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"clojure","exercise":"lucians-luscious-lasagna","id":"7dbdddd6a0df41de95e6aecbfdd0360d","url":"https://exercism.org/tracks/clojure/exercises/lucians-luscious-lasagna","handle":"JBiason","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/clojure/lucians-luscious-lasagna/HELP.md b/clojure/lucians-luscious-lasagna/HELP.md new file mode 100644 index 0000000..05fca60 --- /dev/null +++ b/clojure/lucians-luscious-lasagna/HELP.md @@ -0,0 +1,75 @@ +# Help + +## Running the tests + +## Clojure CLI + +The Clojure exercises on Exercism ship with a `deps.edn` file with a `:test` alias to invoke the [cognitect-labs test-runner](https://github.com/cognitect-labs/test-runner): + +``` bash +$ clj -X:test +``` + +## Leiningen + +Leiningen can also be used to run the exercise's test by running the following command from the exercise's directory: + +```bash +lein test +``` + +## REPL + +To use the REPL to run the exercise's test, run the following command from the exercise's directory: + +```bash +$ clj +``` + +-or- + +```bash +$ lein repl +``` + +Then `require` the exercise's test namespace and the Clojure test namespace): + +```clojure +;; replace with the exercise's name +=> (require '-test) +``` + +Then call `run-tests` on `-test`: + +```clojure +;; replace with the exercise's name +=> (clojure.test/run-tests '-test) +``` + +## Submitting your solution + +You can submit your solution using the `exercism submit src/lucians_luscious_lasagna.clj` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Clojure track's documentation](https://exercism.org/docs/tracks/clojure) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +To get help if you're having trouble, you can use one of the following resources: + +- [Ask Clojure](https://ask.clojure.org/) Official forum for Clojure Q & A. +- [ClojureDocs](https://clojuredocs.org) A repository of language references and examples by function or keyword. +- [/r/clojure](https://www.reddit.com/r/clojure) is the Clojure subreddit. +- [StackOverflow](http://stackoverflow.com/questions/tagged/clojure) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. +- [Clojureverse](https://clojureverse.org/) Friendly and inclusive Clojure(Script) Community \ No newline at end of file diff --git a/clojure/lucians-luscious-lasagna/HINTS.md b/clojure/lucians-luscious-lasagna/HINTS.md new file mode 100644 index 0000000..8a5d002 --- /dev/null +++ b/clojure/lucians-luscious-lasagna/HINTS.md @@ -0,0 +1,29 @@ +# Hints + +## 1. Define the expected oven time in minutes + +- You need to define a [var][vars] using [def]. + +## 2. Calculate the remaining oven time in minutes + +- You need to define a [function][functions] with a single parameter. +- You can use and refer to the previously defined var by its name. +- The result of evaluating the last expression in a function is automatically returned from the function; all functions return this value. +- You can use the core functions for [operations on numbers][operators] to subtract values. + +## 3. Calculate the preparation time in minutes + +- You need to define a [function][functions] with a single parameter. +- You can use the core functions for [operations on numbers][operators] to multiply values. + +## 4. Calculate the elapsed time in minutes + +- You need to define a [function][functions] with two parameters. +- You can [call][calling] one of the other functions you've defined previously. +- You can use the core functions for [operations on numbers][operators] to add values. + +[def]: https://clojure.org/guides/learn/syntax#_def +[vars]: https://clojure.org/reference/vars +[functions]: https://clojure.org/guides/learn/functions +[operators]: https://clojuredocs.org/quickref#numbers +[calling]: https://clojure.org/guides/learn/functions#_creating_functions \ No newline at end of file diff --git a/clojure/lucians-luscious-lasagna/README.md b/clojure/lucians-luscious-lasagna/README.md new file mode 100644 index 0000000..c46294a --- /dev/null +++ b/clojure/lucians-luscious-lasagna/README.md @@ -0,0 +1,101 @@ +# Lucian's Luscious Lasagna + +Welcome to Lucian's Luscious Lasagna on Exercism's Clojure Track. +If you need help running the tests or submitting your code, check out `HELP.md`. +If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) + +## Introduction + +In Clojure, binding a value to a name is referred to as a _var_. Top-level (global) vars are similar to constants in other languages, but are commonly redefined to facilitate dynamic development. + +Top-level vars are defined using `def`: + +```clojure +(def fingers 10) +``` + +The `defn` macro can be used to define a function taking zero or more arguments. A function always returns the result of the last expression in its body. + +```clojure +(defn add [x y] + (+ x y)) +``` + +Invoking a function is done by specifying its name and passing arguments for each of the function's parameters. + +```clojure +(def five (add 2 3)) +``` + +Functions and values in Clojure can only be used _after_ they have been defined. Using it before it has been defined results in a compile error. + +```clojure +;; Compile error as `add` has not yet been defined +(def seven (add 3 4)) + +(defn add [x y] + (+ x y)) +``` + +In Clojure, whitespace has no significance other than formatting. + +Clojure functions and vars are organized in namespaces. A namespace groups related functionality and is defined using the `ns` macro: + +```clojure +(ns calculator) + +(def pi 3.14) + +(defn add [x y] + (+ x y)) +``` + +Clojure supports two types of comments. Single line comments are preceded by `;` and the `comment` form is used to prevent evaluation of everything between `(comment` and `)`. + +## Instructions + +In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book. + +You have four tasks, all related to the time spent cooking the lasagna. + +## 1. Define the expected oven time in minutes + +Define the `expected-time` var to check how many minutes the lasagna should be in the oven. According to the cooking book, the expected oven time in minutes is 40: + +```clojure +expected-time +;;=> 40 +``` + +## 2. Calculate the remaining oven time in minutes + +Define the `remaining-time` function that takes the actual minutes the lasagna has been in the oven as an argument and returns how many minutes the lasagna still has to remain in the oven, based on the expected time oven time in minutes from the previous task. + +```clojure +(remaining-time 30) +;;=> 10 +``` + +## 3. Calculate the preparation time in minutes + +Define the `prep-time` function that takes the number of layers you added to the lasagna as an argument and returns how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare. + +```clojure +(prep-time 2) +;;=> 4 +``` + +## 4. Calculate the total working time in minutes + +Define the `total-time` function that takes two arguments: the first argument is the number of layers you added to the lasagna, and the second argument is the number of minutes the lasagna has been in the oven. The function should return how many minutes in total you've worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment. + +```clojure +(total-time 3 20) +;;=> 26 +``` + +## Source + +### Created by + +- @porkostomus \ No newline at end of file diff --git a/clojure/lucians-luscious-lasagna/deps.edn b/clojure/lucians-luscious-lasagna/deps.edn new file mode 100644 index 0000000..d74ca0e --- /dev/null +++ b/clojure/lucians-luscious-lasagna/deps.edn @@ -0,0 +1,6 @@ +{:aliases {:test {:extra-paths ["test"] + :extra-deps {io.github.cognitect-labs/test-runner + {:git/url "https://github.com/cognitect-labs/test-runner.git" + :sha "705ad25bbf0228b1c38d0244a36001c2987d7337"}} + :main-opts ["-m" "cognitect.test-runner"] + :exec-fn cognitect.test-runner.api/test}}} \ No newline at end of file diff --git a/clojure/lucians-luscious-lasagna/project.clj b/clojure/lucians-luscious-lasagna/project.clj new file mode 100644 index 0000000..c565435 --- /dev/null +++ b/clojure/lucians-luscious-lasagna/project.clj @@ -0,0 +1,4 @@ +(defproject lucians-luscious-lasagna "0.1.0-SNAPSHOT" + :description "lucians-luscious-lasagna exercise." + :url "https://github.com/exercism/clojure/tree/main/exercises/concept/lucians-luscious-lasagna" + :dependencies [[org.clojure/clojure "1.10.0"]]) \ No newline at end of file diff --git a/clojure/lucians-luscious-lasagna/src/lucians_luscious_lasagna.clj b/clojure/lucians-luscious-lasagna/src/lucians_luscious_lasagna.clj new file mode 100644 index 0000000..b82ac4b --- /dev/null +++ b/clojure/lucians-luscious-lasagna/src/lucians_luscious_lasagna.clj @@ -0,0 +1,26 @@ +(ns lucians-luscious-lasagna) + +(def expected-time + 40 + ) + +(defn remaining-time + "Takes the actual time in minutes the lasagna has been in the oven, + and returns how many minutes the lasagna still has to remain in the oven." + [actual-time] + (- expected-time actual-time) + ) + +(defn prep-time + "Takes the number of layers added to the lasagna, + and returns how many minutes you spent preparing the lasagna" + [num-layers] + (* 2 num-layers) + ) + +(defn total-time + "Takes the number of layers of lasagna and the actual time in minutes it has been in the oven. + Returns how many minutes in total you've worked on cooking the lasagna" + [num-layers actual-time] + (+ (prep-time num-layers) actual-time) + ) diff --git a/clojure/lucians-luscious-lasagna/test/lucians_luscious_lasagna_test.clj b/clojure/lucians-luscious-lasagna/test/lucians_luscious_lasagna_test.clj new file mode 100644 index 0000000..4945a45 --- /dev/null +++ b/clojure/lucians-luscious-lasagna/test/lucians_luscious_lasagna_test.clj @@ -0,0 +1,21 @@ +(ns lucians-luscious-lasagna-test + (:require [clojure.test :refer [deftest testing is]] + lucians-luscious-lasagna)) + +(deftest expected-time-test + (is (= 40 lucians-luscious-lasagna/expected-time))) + +(deftest remaining-time-test + (is (= 15 (lucians-luscious-lasagna/remaining-time 25)))) + +(deftest prep-time-one-layer-test + (is (= 2 (lucians-luscious-lasagna/prep-time 1)))) + +(deftest prep-time-multiple-layers-test + (is (= 8 (lucians-luscious-lasagna/prep-time 4)))) + +(deftest total-time-one-layer-test + (is (= 32 (lucians-luscious-lasagna/total-time 1 30)))) + +(deftest total-time-multiple-layers-test + (is (= 16 (lucians-luscious-lasagna/total-time 4 8)))) \ No newline at end of file