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,20 @@
{
"authors": [
"colinleach"
],
"files": {
"solution": [
"cars-assemble.jl"
],
"test": [
"runtests.jl"
],
"exemplar": [
".meta/exemplar.jl"
]
},
"forked_from": [
"csharp/cars-assemble"
],
"blurb": "Learn about conditionals by analyzing the production of an assembly line."
}

View File

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

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 cars-assemble.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/).

View File

@@ -0,0 +1,22 @@
# Hints
## 1. Calculate the success rate
- You need to translate the speed into a success rate, using the rules given in the instructions.
- The returned value is a floating-point number.
- Julia does not have the `select`/`case` syntax present in some other languages, but the Introduction discussed `if-else` syntax.
## 2. Calculate the production rate per hour
- The total theoretical production rate depends on the base production rate (a constant) and the speed.
- Return the actual production rate, which also depends on the success rate.
## 3. Calculate the number of working items produced per minute
- The hourly production rate was calculated in task 2.
- This task requires the rate per minute.
- Only complete, working cars are counted in this exercise, so you will need to remove part-complete cars from the count.
- The return value must be an integer (for example `7`, not `7.0`).
- The [Numbers][numbers] Concept already discussed ways to round values.
[numbers]: https://exercism.org/tracks/julia/concepts/numbers

View File

@@ -0,0 +1,150 @@
# Cars Assemble
Welcome to Cars Assemble 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
## Comparison operators
Comparison operators in Julia are similar to many other languages, though with some extra options for math-lovers.
For equality, the operators are `==` (equal) and `!=` or `≠` (not equal).
```julia
txt = "abc"
txt == "abc" # true
txt != "abc" # false
txt "abc" # false (synonym for !=)
```
In addition, we have the various greater/less than operators.
```julia
1 < 3 # true
3 > 3 # false
3 <= 3 # true
3 3 # true (synonym for <=)
4 >= 3 # true
4 3 # true (synonym for >=)
```
As often with Julia, an appropriate editor makes use of the mathematical symbol easy.
Type `\ne`, `\le` or `\ge` then `TAB` to get `≠`, `≤` or `≥`.
The previous example uses only numbers, but we will see in other parts of the syllabus that various additional types have a sense of ordering and can be tested for greater/less than.
Comparison operators can be chained, which allows a clear and concise syntax:
```julia
n = 3
1 n 5 # true (n "between" two limits)
```
The previous example is a synonym for `1 ≤ n && n ≤ 5`.
## Branching with `if`
This is the full form of an `if` statement:
```julia
if conditional1
statements...
elseif conditional2
statements...
else
statements...
end
```
There is no need for parentheses `()` or braces `{}`, and indentation is "only" to improve readability _(but readability is very important!)_.
Both `elseif` and `else` are optional, and there can be multiple `elseif` blocks.
However, the `end` is required.
It is possible to nest `if` statements, though you might want to help readability with the thoughtful use of parentheses, indents and comments.
The shortest form of an `if` statement would be something like this:
```julia
if n < 0
n = 0
end
```
As a reminder: only expressions that evaluate to `true` or `false` can be used as conditionals.
Julia deliberately avoids any concept of "truthiness", so zero values, empty strings and empty arrays are _not_ equivalent to `false`.
## Ternary operator
A simple and common situation is picking one of two values based on a conditional.
Julia, like many languages, has a ternary operator to make this more concise.
The syntax is `conditional ? value_if_true : value_if_false`.
So the previous example could be rewritten:
```julia
n = n < 0 ? 0 : n
```
Parentheses are not required by the compiler, but may improve readability.
## Instructions
In this exercise you will be writing code to analyze the production of an assembly line in a car factory.
The assembly line's speed can range from `0` (off) to `10` (maximum).
At its lowest speed (`1`), `221` cars are produced each hour.
The production increases linearly with the speed.
So with the speed set to `4`, it should produce `4 * 221 = 884` cars per hour.
However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded.
You have three tasks.
Each of the required functions takes a single integer parameter, the speed of the assembly line.
## 1. Calculate the success rate
Implement the `success_rate()` method to calculate the probability of an item being created without error for a given speed.
The following table shows how speed influences the success rate:
- `0`: 0% success rate.
- `1` to `4`: 100% success rate.
- `5` to `8`: 90% success rate.
- `9`: 80% success rate.
- `10`: 77% success rate.
```julia-repl
julia> success_rate(10)
0.77
```
## 2. Calculate the production rate per hour
Implement the `production_rate_per_hour()` method to calculate the assembly line's production rate per hour, taking into account its success rate.
```julia-repl
julia> production_rate_per_hour(6)
1193.4
```
Note that the value returned is floating-point.
## 3. Calculate the number of working items produced per minute
Implement the `working_items_per_minute()` method to calculate how many working cars are produced per minute:
```julia-repl
julia> working_items_per_minute(6)
19
```
Note that the value returned is an integer: incomplete items are not included.
## Source
### Created by
- @colinleach

View File

@@ -0,0 +1,24 @@
CARS_PER_HOUR = 221
function success_rate(speed)
if speed == 0
success = 0
elseif 1 speed 4
success = 100
elseif 5 speed 8
success = 90
elseif speed == 9
success = 80
elseif speed == 10
success = 77
end
return success / 100
end
function production_rate_per_hour(speed)
return CARS_PER_HOUR * speed * success_rate(speed)
end
function working_items_per_minute(speed)
return Int(production_rate_per_hour(speed) ÷ 60)
end

View File

@@ -0,0 +1,108 @@
using Test
include("cars-assemble.jl")
@testset verbose = true "tests" begin
@testset "success_rate" begin
@testset "Success rate for speed zero" begin
speed = 0
@test isapprox(success_rate(speed), 0.0, atol=1e-3)
end
@testset "Success rate for speed one" begin
speed = 1
@test isapprox(success_rate(speed), 1.0, atol=1e-3)
end
@testset "Success rate for speed four" begin
speed = 4
@test isapprox(success_rate(speed), 1.0, atol=1e-3)
end
@testset "Success rate for speed five" begin
speed = 5
@test isapprox(success_rate(speed), 0.9, atol=1e-3)
end
@testset "Success rate for speed nine" begin
speed = 9
@test isapprox(success_rate(speed), 0.8, atol=1e-3)
end
@testset "Success rate for speed ten" begin
speed = 10
@test isapprox(success_rate(speed), 0.77, atol=1e-3)
end
end
@testset "production_rate_per_hour" begin
@testset "Production rate per hour for speed zero" begin
speed = 0
@test isapprox(production_rate_per_hour(speed), 0.0, atol=1e-3)
end
@testset "Production rate per hour for speed one" begin
speed = 1
@test isapprox(production_rate_per_hour(speed), 221.0, atol=1e-3)
end
@testset "Production rate per hour for speed four" begin
speed = 4
@test isapprox(production_rate_per_hour(speed), 884.0, atol=1e-3)
end
@testset "Production rate per hour for speed seven" begin
speed = 7
@test isapprox(production_rate_per_hour(speed), 1392.3, atol=1e-3)
end
@testset "Production rate per hour for speed nine" begin
speed = 9
@test isapprox(production_rate_per_hour(speed), 1591.2, atol=1e-3)
end
@testset "Production rate per hour for speed ten" begin
speed = 10
@test isapprox(production_rate_per_hour(speed), 1701.7, atol=1e-3)
end
end
@testset "working_items_per_minute" begin
@testset "Working items per minute for speed zero" begin
speed = 0
@test typeof(working_items_per_minute(speed)) == Int
@test working_items_per_minute(speed) == 0
end
@testset "Working items per minute for speed one" begin
speed = 1
@test typeof(working_items_per_minute(speed)) == Int
@test working_items_per_minute(speed) == 3
end
@testset "Working items per minute for speed five" begin
speed = 5
@test typeof(working_items_per_minute(speed)) == Int
@test working_items_per_minute(speed) == 16
end
@testset "Working items per minute for speed eight" begin
speed = 8
@test typeof(working_items_per_minute(speed)) == Int
@test working_items_per_minute(speed) == 26
end
@testset "Working items per minute for speed nine" begin
speed = 9
@test typeof(working_items_per_minute(speed)) == Int
@test working_items_per_minute(speed) == 26
end
@testset "Working items per minute for speed ten" begin
speed = 10
@test typeof(working_items_per_minute(speed)) == Int
@test working_items_per_minute(speed) == 28
end
end
end