Browse Source

Exercism: Lucians Luscious Lasagna

master
Julio Biason 3 years ago
parent
commit
621eea9579
  1. 20
      clojure/lucians-luscious-lasagna/.exercism/config.json
  2. 1
      clojure/lucians-luscious-lasagna/.exercism/metadata.json
  3. 75
      clojure/lucians-luscious-lasagna/HELP.md
  4. 29
      clojure/lucians-luscious-lasagna/HINTS.md
  5. 101
      clojure/lucians-luscious-lasagna/README.md
  6. 6
      clojure/lucians-luscious-lasagna/deps.edn
  7. 4
      clojure/lucians-luscious-lasagna/project.clj
  8. 26
      clojure/lucians-luscious-lasagna/src/lucians_luscious_lasagna.clj
  9. 21
      clojure/lucians-luscious-lasagna/test/lucians_luscious_lasagna_test.clj

20
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"
]
}
}

1
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}

75
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 <exercise> with the exercise's name
=> (require '<exercise>-test)
```
Then call `run-tests` on `<exercise>-test`:
```clojure
;; replace <exercise> with the exercise's name
=> (clojure.test/run-tests '<exercise>-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

29
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

101
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

6
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}}}

4
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"]])

26
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)
)

21
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))))
Loading…
Cancel
Save