Skip to content

Commit

Permalink
responses to @BNAndras review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
colinleach committed Sep 9, 2024
1 parent 5e9edd7 commit 1eebaff
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 56 deletions.
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`.

## 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
11 changes: 7 additions & 4 deletions exercises/concept/lasagna/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ In this exercise you're going to write some code to help you cook a brilliant la

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

## 1.
## 1. Store the expected bake time in a constant

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.
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> expected_bake_time
Expand All @@ -15,10 +17,10 @@ julia> expected_bake_time

## 2. Calculate the preparation time in minutes

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 `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> preptime(4)
julia> preparation_time(4)
8
```

Expand All @@ -34,6 +36,7 @@ julia> remaining_time(50)
## 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.

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
44 changes: 11 additions & 33 deletions exercises/concept/lasagna/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,10 @@ For that reason, we will introduce named functions as the very first concept.
Julia is a dynamic, strongly-typed programming langauge.
The programming style is mainly functional, though with more flexibility than in languages such as Haskell.

## Comments

Two options are possible in Julia:
- Single-line comments start with `#`
- Multi-line comments start with `#=` and end with `=#`. Nesting is allowed.

```julia
# This is a single-line comment

x = 3 # This is an inline comment

#=
Multi-line comments can be used for longer explanations.
They are especially useful to comment out blocks of code during debugging.
=#
```

## Variables and assignment

To create a variable, just assign a value to it:
There is no need to declare a variable in advance.
Just assign a value to a suitable name:

```julia-repl
julia> myvar = 42 # an integer
Expand All @@ -36,20 +19,13 @@ julia> name = "Maria" # strings are surrounded by double-quotes ""
"Maria"
```

Types are an important subject in Julia, but for now it is best to ignore them them.
The compiler will infer a suitable type for any expression you use.

## Constants

Global variables, created outside any function, are:
- Allowed.
- Sometimes necessary.
- Usually discouraged (only within `*.jl` files; the REPL operates differently).

If a value needs to be available throughout the program, but is not expected to change, use a constant instead.
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.

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

Constants also help to protect you against errors in coding.
Accidentally trying to change the `const` value will give a warning:

```julia-repl
Expand All @@ -61,6 +37,9 @@ WARNING: redefinition of constant Main.answer. This may fail, cause incorrect an
24
```

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.

## Arithmetic operators

These mostly work conventionally:
Expand Down Expand Up @@ -88,7 +67,7 @@ There are two common ways to define a named function in Julia:
Indentation by 4 spaces is conventional for readability, but the compiler ignores this.
The `end` keyword is essential: more like Ruby than like Python.

Note that we could have written `return x * y + z`.
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.

Expand All @@ -98,7 +77,8 @@ There are two common ways to define a named function in Julia:
muladd(x, y, z) = x * y + z
```

This is most commonly used for one-line function definitions or mathematical functions, where the function body is a single expression.
This is most commonly used for making concise single-expression functions.

A `return` keyword is *never* used in the assignment form.

The two forms are equivalent, and are used in exactly the same way, so choose whichever is more readable.
Expand All @@ -118,5 +98,3 @@ square_plus_one(x) = muladd(x, x, 1)
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.

By convention, variable, constant, and function names are *lowercase*, with underscores kept to a reasonable minimum.

However, Julia uses Unicode throughout, so "letter" is interpreted quite flexibly: not just *English* letters.
7 changes: 0 additions & 7 deletions exercises/concept/lasagna/.docs/introduction.md.tpl

This file was deleted.

6 changes: 3 additions & 3 deletions exercises/concept/lasagna/.meta/exemplar.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
expected_bake_time = 60
const expected_bake_time = 60

preptime(layers) = 2 * layers
preparation_time(layers) = 2 * layers

remaining_time(current_time) = expected_bake_time - current_time

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

# Define the `preptime(layers)` function.
# Define the `preparation_time(layers)` function.

# Define the `remaining_time(time_in_oven)` function.

Expand Down
6 changes: 3 additions & 3 deletions exercises/concept/lasagna/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ include("lasagna.jl")
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

0 comments on commit 1eebaff

Please sign in to comment.