Skip to content

Commit

Permalink
exercises: add new concept exercise basics
Browse files Browse the repository at this point in the history
Decisions:
- Use `inim` style repl commands
- Simplify by using `let`. We can address `const` and `var` in later
  exercises.
- Similarly, use `proc`, not `func`.

Co-authored-by: Erik Schierboom <[email protected]>
Co-authored-by: Jeremy Walker <[email protected]>
  • Loading branch information
3 people committed Jan 29, 2021
1 parent 4151608 commit d23c5bf
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
49 changes: 49 additions & 0 deletions exercises/concept/basics/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book.

You have five tasks, all related to the time spent cooking the lasagna.

## 1. Define the expected oven time in minutes

Define the `expectedMinutesInOven` immutable variable that returns how many minutes the lasagna should be in the oven. It must be exported. According to the cooking book, the expected oven time in minutes is 40:

```nim
nim> expectedMinutesInOven
40 == type int
```

## 2. Calculate the remaining oven time in minutes

Implement the `remainingMinutesInOven` procedure that takes the actual minutes the lasagna has been in the oven as a parameter and returns how many minutes the lasagna still has to remain in the oven, based on the expected oven time in minutes from the previous task.

```nim
nim> remainingMinutesInOven(30)
10 == type int
```

## 3. Calculate the preparation time in minutes

Implement the `preparationTimeInMinutes` procedure that takes the number of layers you added to the lasagna as a parameter and returns how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.

```nim
nim> preparationTimeInMinutes(2)
4 == type int
```

## 4. Calculate the total working time in minutes

Implement the `totalTimeInMinutes` procedure that takes two parameters: the `numberOfLayers` parameter is the number of layers you added to the lasagna, and the `actualMinutesInOven` parameter is the number of minutes the lasagna has been in the oven. The procedure 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.

```nim
nim> totalTimeInMinutes(3, 20)
26 == type int
```

## 5. Update the recipe with notes

Go back through the recipe, adding notes and documentation.

```nim
proc totalTimeInMinutes*(numberOfLayers, actualMinutesInOven: int): int =
## Calculate the total working time. That is, the time to prepare all the layers
## of lasagna, and the time already spent in the oven.
```
9 changes: 9 additions & 0 deletions exercises/concept/basics/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"authors": [
{
"github_username": "ynfle",
"exercism_username": "ynfle"
}
],
"forked_from": ["javascript/basics", "python/basics"]
}
22 changes: 22 additions & 0 deletions exercises/concept/basics/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Learning objectives

- Know about `proc` definition
- Know `const` definition
- Know about type inference
- Know about `proc` parameters
- Know the different way of returning values from a `proc` (implicit, `return`, `result`)
- Know how to export identifiers
- Know how to print values for debugging (`echo`, `debugEcho`)

## Out of scope

- `func` (I think we'll do another exercise on this so don't what to put here)
- `var`, `let`, `var` assignment

## Concepts

- `basics`

## Prerequisites

None
18 changes: 18 additions & 0 deletions exercises/concept/basics/.meta/example.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let expectedMinutesInOven* = 40

let preparationMinutesPerLayer = 2 ## \
## The amount of minutes it takes to prepare a single layer.

proc remainingMinutesInOven*(actualMinutesInOven: int): int =
## Determine the amount of minutes the lasagna still needs to remain in the
## oven to be properly prepared.
expectedMinutesInOven - actualMinutesInOven

proc preparationTimeInMinutes*(numberOfLayers: int): int =
## Given a number of layers, determine the total preparation time.
numberOfLayers * preparationMinutesPerLayer

proc totalTimeInMinutes*(numberOfLayers, actualMinutesInOven: int): int =
## Calculate the total working time. That is, the time to prepare all the layers
## of lasagna, and the time already spent in the oven.
preparationTimeInMinutes(numberOfLayers) + actualMinutesInOven

0 comments on commit d23c5bf

Please sign in to comment.