Some solutions for the julia path

This commit is contained in:
2025-08-04 19:44:03 +02:00
parent 5c52e8e34d
commit 8296c79f68
104 changed files with 4373 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
{
"authors": [
"SaschaMann"
],
"contributors": [
"ErikSchierboom",
"glennj",
"colinleach"
],
"files": {
"solution": [
"lasagna.jl"
],
"test": [
"runtests.jl"
],
"exemplar": [
".meta/exemplar.jl"
]
},
"language_versions": "≥1.0",
"forked_from": [
"fsharp/lucians-luscious-lasagna"
],
"blurb": "Learn the basics of functions in Julia by cooking delicious lasagna."
}

View File

@@ -0,0 +1 @@
{"track":"julia","exercise":"lasagna","id":"72394dfe8aac4bf0a1eda56025bdb913","url":"https://exercism.org/tracks/julia/exercises/lasagna","handle":"Kimawari","is_requester":true,"auto_approve":false}

34
julia/lasagna/HELP.md Normal file
View File

@@ -0,0 +1,34 @@
# Help
## Running the tests
To run the tests, run this command from within the exercise directory:
```bash
$ julia runtests.jl
```
## Submitting your solution
You can submit your solution using the `exercism submit lasagna.jl` 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 [Julia track's documentation](https://exercism.org/docs/tracks/julia)
- The [Julia track's programming category on the forum](https://forum.exercism.org/c/programming/julia)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- 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, we recommend that you submit your code and request mentoring :)
If you don't want to do that for whatever reason, then you can find the wider Julia community channels [here](https://julialang.org/community/).

26
julia/lasagna/HINTS.md Normal file
View File

@@ -0,0 +1,26 @@
# Hints
## General
- You will need to define [functions][functions] and use [arithmetic operators][arithmetics].
## 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`.
## 3. Calculate the remaining oven time in minutes
- Use the [`minus` arithmetic operator][arithmetics] to subtract the argument from `60`.
## 4. Calculate the total working time in minutes
- 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

164
julia/lasagna/README.md Normal file
View File

@@ -0,0 +1,164 @@
# Lasagna
Welcome to Lasagna on Exercism's Julia 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
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.
Julia is a dynamic, strongly-typed programming langauge.
The programming style is mainly functional, though with more flexibility than in languages such as Haskell.
## Variables and assignment
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
42
julia> name = "Maria" # strings are surrounded by double-quotes ""
"Maria"
```
## Constants
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 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
julia> const answer = 42
42
julia> answer = 24
WARNING: redefinition of constant Main.answer. This may fail, cause incorrect answers, or produce other errors.
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 are the same as in many other languages:
```julia
2 + 3 # 5 (addition)
2 - 3 # -1 (subtraction)
2 * 3 # 6 (multiplication)
8 / 2 # 4.0 (division with floating-point result)
8 % 3 # 2 (remainder)
```
## Functions
There are two common ways to define a named function in Julia:
1. Using the `function` keyword
```julia
function muladd(x, y, z)
x * y + z
end
```
Indentation by 4 spaces is conventional for readability, but the compiler ignores this.
The `end` keyword is essential.
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.
2. Using the "assignment form"
```julia
muladd(x, y, z) = x * y + z
```
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.
Invoking a function is done by specifying its name and passing arguments for each of the function's parameters:
```julia
# invoking a function
muladd(10, 5, 1)
# and of course you can invoke a function within the body of another function:
square_plus_one(x) = muladd(x, x, 1)
```
## 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.
By convention, variable, constant, and function names are *lowercase*, with underscores kept to a reasonable minimum.
## 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. 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.
```julia-repl
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
```
## 3. 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 a parameter and returns how many minutes the lasagna still has to remain in the oven.
```julia-repl
julia> remaining_time(50)
10
```
## 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 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.
```julia-repl
julia> total_working_time(3, 20)
26
```
## Source
### Created by
- @SaschaMann
### Contributed to by
- @ErikSchierboom
- @glennj
- @colinleach

14
julia/lasagna/lasagna.jl Normal file
View File

@@ -0,0 +1,14 @@
# Define the `expected_bake_time` constant`
const expected_bake_time = 60
# Define the `preparation_time(layers)` function.
function preparation_time(layers)
return layers * 2
end
# Define the `remaining_time(time_in_oven)` function.
function remaining_time(time_in_oven)
return expected_bake_time - time_in_oven
end
# Define the `total_working_time(layers, time_in_oven)` function.
function total_working_time(layers, time_in_oven)
return preparation_time(layers) + time_in_oven
end

29
julia/lasagna/runtests.jl Normal file
View File

@@ -0,0 +1,29 @@
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(@__MODULE__, :expected_bake_time) == true
end
end
@testset "preparation time" begin
@test preparation_time(2) == 4
@test preparation_time(3) == 6
@test preparation_time(8) == 16
end
@testset "remaining time" begin
@test remaining_time(30) == 30
@test remaining_time(50) == 10
@test remaining_time(60) == 0
end
@testset "total working time" begin
@test total_working_time(3, 20) == 26
end
end