Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lasagna] revision to existing concept exercise #780

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 1 addition & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
"name": "Lasagna",
"uuid": "39ebdd04-4f84-4817-bf9d-f1f9e066c283",
"concepts": [
"functions",
"integer-introduction",
"comments"
"basics"
],
"prerequisites": [],
"status": "wip"
Expand Down
15 changes: 10 additions & 5 deletions exercises/concept/lasagna/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@

- You will need to define [functions][functions] and use [arithmetic operators][arithmetics].

## 1. Calculate the preparation time in minutes
## 1. Store the expected bake time in a constant

- Define a [const][const] to store this value.

## 2. Calculate the preparation time in minutes

- Use the [`times` arithmetic operator][arithmetics] to multiply the argument by `2`.
depial marked this conversation as resolved.
Show resolved Hide resolved

## 2. Calculate the remaining oven time in minutes
## 3. Calculate the remaining oven time in minutes

- Use the [`minus` arithmetic operator][arithmetics] to subtract the argument from `60`.

## 3. Calculate the total working time in minutes
## 4. Calculate the total working time in minutes

- Reuse the `preptime` function defined in the first step.
- Use the [`plus` arithmetic operator][arithmetics] to add the return value of `preptime` to the argument.
- Reuse the `preparation_time` function defined in the first step.
- Use the [`plus` arithmetic operator][arithmetics] to add the return value of `preparation_time` to the argument.

[const]: https://docs.julialang.org/en/v1/base/base/#const
[functions]: https://docs.julialang.org/en/v1/manual/functions/
[arithmetics]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Arithmetic-Operators-1
25 changes: 18 additions & 7 deletions exercises/concept/lasagna/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,41 @@

In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book.

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

## 1. Calculate the preparation time in minutes
## 1. Store the expected bake time in a constant

Define the `preptime` 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.
Define the `expected_bake_time` constant that returns how many minutes the lasagna should bake in the oven.

According to the cooking book, lasagna needs to be in the oven for a total of 60 minutes.

```julia-repl
julia> preptime(4)
julia> expected_bake_time
60
```

## 2. Calculate the preparation time in minutes

Define the `preparation_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.

```julia-repl
julia> preparation_time(4)
8
```

## 2. Calculate the remaining oven time in minutes
## 3. Calculate the remaining oven time in minutes

According to the cooking book, lasagna needs to be in the oven for a total of 60 minutes.
Define the `remaining_time` function 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.

```julia-repl
julia> remaining_time(50)
10
```

## 3. Calculate the total working time in minutes
## 4. Calculate the total working time in minutes

Define the `total_working_time` function that takes two arguments: the first argument is the number of layers you added to the lasagna, and the second parameter is the number of minutes the lasagna has been in the oven.
colinleach marked this conversation as resolved.
Show resolved Hide resolved

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.

```julia-repl
Expand Down
146 changes: 61 additions & 85 deletions exercises/concept/lasagna/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,122 +3,98 @@
The entire Julia track will require you to treat your solution like small libraries, i.e. you need to define functions, types etc. which will then be run against a test suite.
For that reason, we will introduce named functions as the very first concept.

## Functions

### Defining functions

There are two common ways to define a named function in Julia:

1. Using the `function` keyword

```julia
function muladd(x, y, z)
return x * y + z
end
```

2. Using the "assignment form"
Julia is a dynamic, strongly-typed programming langauge.
The programming style is mainly functional, though with more flexibility than in languages such as Haskell.

```julia
muladd(x, y, z) = x * y + z
```
## Variables and assignment

This is most commonly used for one-line function definitions or mathematical functions, where the function body is a single expression.
There is no need to declare a variable in advance.
Just assign a value to a suitable name:

### Invoking functions
```julia-repl
julia> myvar = 42 # an integer
42

Invoking a function is done by specifying its name and passing arguments for each of the function's parameters:
julia> name = "Maria" # strings are surrounded by double-quotes ""
"Maria"
```

```julia
# invoking a function
muladd(10, 5, 1)
## Constants

# and of course you can invoke a function within the body of another function:
square_plus_one(x) = muladd(x, x, 1)
```
If a value needs to be available throughout the program, but is not expected to change, it is best to mark it as a constant.

## Integers and Arithmetic operations
Prefacing an assignment with the `const` keyword allows the compiler to generate more efficient code than is possible for a variable.

Integer literals in Julia are represented in the usual way.
Underscores may be used as a digit separator.
Constants also help to protect you against errors in coding.
Accidentally trying to change the `const` value will give a warning:

```julia-repl
julia> 1
1

julia> -1234
-1234
julia> const answer = 42
42

julia> 1_234_567_890
1234567890
julia> answer = 24
WARNING: redefinition of constant Main.answer. This may fail, cause incorrect answers, or produce other errors.
24
```

### Arithmetic operations
Note that a `const` can only be declared *outside* any function.
This will typically be near the top of the `*.jl` file, before the function definitions.

The standard prefix and infix operations are available: `+`, `-`, `*`, `%`.
## Arithmetic operators

```julia-repl
julia> +16
16

julia> -16
-16
These mostly work conventionally:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is a little confusing since we don’t detail what’s not conventional in the next few lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update tomorrow.


julia> 16 + 6
22
```julia
2 + 3 # 5 (addition)
2 - 3 # -1 (subtraction)
2 * 3 # 6 (mutlplication)
colinleach marked this conversation as resolved.
Show resolved Hide resolved
8 / 2 # 4.0 (division with floating-point result)
8 % 3 # 2 (remainder)
```

julia> 16 - 6
10
## Functions

julia> 16 * 6
96
There are two common ways to define a named function in Julia:

julia> 16 % 6
4
```
1. Using the `function` keyword

#### Division
```julia
function muladd(x, y, z)
x * y + z
end
```

Dividing two numbers with the `/` operator will result in a floating point value.
To perform integer division
Indentation by 4 spaces is conventional for readability, but the compiler ignores this.
The `end` keyword is essential.

- use the `div(x, y)` function, or
- use the `÷` operator (Julia source code is unicode-aware)
Note that we could have written `return x * y + z`.
However, Julia functions always return the last expression evaluated, so the `return` keyword is optional.
Many programmers prefer to include it to make their intentions more explicit.

```julia-repl
julia> 16 / 6
2.6666666666666665

julia> div(16, 6)
2
2. Using the "assignment form"

julia> 16 ÷ 6
2
```
```julia
muladd(x, y, z) = x * y + z
```

~~~~exercism/note
It's natural to use Unicode symbols in Julia source files, typically in mathematical expressions.
When using the Julia REPL, or in other Julia editing environments, the division symbol can be entered by typing `\div` followed by the `Tab` key.
More details can be found in the manual at [Unicode Input][unicode].
This is most commonly used for making concise single-expression functions.

[unicode]: https://docs.julialang.org/en/v1/manual/unicode-input/#Unicode-Input
~~~~
A `return` keyword is *never* used in the assignment form.

## Comments
The two forms are equivalent, and are used in exactly the same way, so choose whichever is more readable.

Julia supports two kinds of comments.
Single line comments are preceded by `#` and multiline comments are inserted between `#=` and `=#`.
Invoking a function is done by specifying its name and passing arguments for each of the function's parameters:

```julia
add(1, 3) # returns 4
# invoking a function
muladd(10, 5, 1)

#= Some random code that's no longer needed but not deleted
sub(x, y) = x - y
mulsub(x, y, z) = sub(mul(x, y), z)
=#
# and of course you can invoke a function within the body of another function:
square_plus_one(x) = muladd(x, x, 1)
```

## Types
## Naming conventions

Like many languages, Julia requires that names (of variables, functions, and many other things) start with a letter, followed by any combination of letters, digits and underscores.

Depending on which other programming languages you know, you may expect parameters, variables or return values to have explicit type annotations.
For now, assume that Julia will infer the types automagically and don't worry about them, we will get to the specifics of the type system in later exercises.
By convention, variable, constant, and function names are *lowercase*, with underscores kept to a reasonable minimum.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe some examples just to be clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like something that should be expanded in basics/about.md. I'm trying hard to keep introduction.md to the bare minimum needed to solve the exercise. Jeremy has said repeatedly that this is what he wants, and I'm not disagreeing.

21 changes: 0 additions & 21 deletions exercises/concept/lasagna/.docs/introduction.md.tpl

This file was deleted.

3 changes: 2 additions & 1 deletion exercises/concept/lasagna/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
],
"contributors": [
"ErikSchierboom",
"glennj"
"glennj",
"colinleach"
],
"files": {
"solution": [
Expand Down
9 changes: 5 additions & 4 deletions exercises/concept/lasagna/.meta/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Goal

The goal of this exercise is to teach the student the fundamentals of the Concept of functions in Julia, so that they are able to understand the basic structure of Exercism exercises.
The goal of this exercise is to teach the student the fundamentals of the `basics` Concept in Julia, so that they are able to understand the basic structure of Exercism exercises.

## Learning objectives

Expand All @@ -11,17 +11,18 @@ The goal of this exercise is to teach the student the fundamentals of the Concep
- Know how to call a function.
- Know about implicit `return`s.
- Know how to define an integer.
- Know how to define a global constant.
- Know how to use mathematical operators on integers.

## Out of scope

Everything beyond the simplest minimum for the first exercise of a novice.

## Concepts

The Concepts this exercise practices are:

- `functions`: Defining a function with one or more positional arguments that returns something. Bare minimum to be able to understand the basic structure of Exercism exercises.
- `integers-introduction`: Present integer literals and basic arithmetic operations.
- `comments`
- `basics`: Defining a constant. Defining a function with one or more positional arguments that returns something. Bare minimum to be able to understand the basic structure of Exercism exercises.

## Prerequisites

Expand Down
10 changes: 7 additions & 3 deletions exercises/concept/lasagna/.meta/exemplar.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
preptime(layers) = 2 * layers
remaining_time(current_time) = 60 - current_time
total_working_time(layers, current_time) = preptime(layers) + current_time
const expected_bake_time = 60

preparation_time(layers) = 2 * layers

remaining_time(current_time) = expected_bake_time - current_time

total_working_time(layers, current_time) = preparation_time(layers) + current_time
5 changes: 4 additions & 1 deletion exercises/concept/lasagna/lasagna.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Define the `preptime(layers)` function.
# Define the `expected_bake_time` constant`

# Define the `preparation_time(layers)` function.

# Define the `remaining_time(time_in_oven)` function.

# Define the `total_working_time(layers, time_in_oven)` function.

14 changes: 11 additions & 3 deletions exercises/concept/lasagna/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ using Test
include("lasagna.jl")

@testset verbose = true "tests" begin

@testset "expected bake time" begin
@test expected_bake_time == 60
if VERSION > v"1.7"
@test isconst(typeof(expected_bake_time), expected_bake_time) == true
end
end

@testset "preparation time" begin
@test preptime(2) == 4
@test preptime(3) == 6
@test preptime(8) == 16
@test preparation_time(2) == 4
@test preparation_time(3) == 6
@test preparation_time(8) == 16
end

@testset "remaining time" begin
Expand Down
Loading