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": [
"mixed-juices.jl"
],
"test": [
"runtests.jl"
],
"exemplar": [
".meta/exemplar.jl"
]
},
"forked_from": [
"javascript/mixed-juices"
],
"blurb": "Help Li Mei operate her juice store with loops"
}

View File

@@ -0,0 +1 @@
{"track":"julia","exercise":"mixed-juices","id":"2bcf07bd0c844f98af28a5616fcafdae","url":"https://exercism.org/tracks/julia/exercises/mixed-juices","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 mixed-juices.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,21 @@
# Hints
## 1. Determine how long it takes to mix a juice
- Julia has no `switch` statement, in contrast to many other languages.
- The `if` ... `elsefif` ... `else` syntax was covered in the Conditionals concept.
## 2. Replenish the lime wedge supply
- A `while` loop can run until you have enough lime wedges.
- The `popfirst!()` function was covered in the Vectors concept.
## 3. List the times to mix each order in the queue
- Looping over a vector is easy with `for` ... `in`.
- Building a new vector could use `push!()`, which was covered in the Vectors concept.
- Later in the syllabus we will explore various other ways to do this in Julia.
## 4. Finish up the shift
- See task 2.

View File

@@ -0,0 +1,148 @@
# Mixed Juices
Welcome to Mixed Juices 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
There are basically two types of loops:
1. Loop until a condition is satisfied.
2. Loop over the elements in a collection.
Both are possible in Julia, though the second may be more common.
## The `while` loop
For open-ended problems where the number of times round the loop is unknown in advance, Julia has the `while` loop.
The basic form is fairly simple:
```julia
while condition
do_something()
end
```
In this case, the program will keep going round the loop until `condition` is no longer `true`.
Two ways to exit the loop early are available:
- A `break` causes the loop to exit, with execution continuing at the next line after the loop `end`.
- A `return x` stops execution of the current function, passing the return value `x` back to the caller.
With these options available, it can sometimes be convenient to create an "infinite" loop with `while true ... end`, then rely on finding a stopping condition within the loop body to trigger a `break` or `return`.
## Loop over a collection
The simplest illustration is to loop over a range.
If we want to do something 10 times:
```julia
for n in 1:10
do_something(n)
end
```
If the current iteration fails to satisfy some condition, it is possible to skip immediately to the next iteration with a `continue`:
```julia
for n in 1:10
if is_useless(n)
continue
end
# we decided this iteration could be useful
do_something_slow(n)
end
```
In a shorter form, the `if` block could be replaced by `is_useless(n) && continue`.
Many other collection types can be looped over: elements in an array, characters in a string, keys in a dictionary...
The examples so far loop over the range `1:10`, where the value is also the loop index.
More generally, the index may be needed and not just the value.
For this, the `eachindex()` function is used, for example `for i in eachindex(my_array) ... end`.
## Instructions
Your friend Li Mei runs a juice bar where she sells delicious mixed fruit juices.
You are a frequent customer in her shop and realized you could make your friend's life easier.
You decide to use your coding skills to help Li Mei with her job.
## 1. Determine how long it takes to mix a juice
Li Mei likes to tell her customers in advance how long they have to wait for a juice from the menu that they ordered.
She has a hard time remembering the exact numbers because the time it takes to mix the juices varies.
`"Pure Strawberry Joy"` takes 0.5 minutes, `"Energizer"` and `"Green Garden"` take 1.5 minutes each, `"Tropical Island"` takes 3 minutes and `"All or Nothing"` takes 5 minutes.
For all other drinks (e.g., special offers) you can assume a preparation time of 2.5 minutes.
To help your friend, write a function `time_to_mix_juice` that takes a juice from the menu as an argument and returns the number of minutes it takes to mix that drink.
```julia-repl
julia> time_to_mix_juice("Tropical Island")
3
julia> time_to_mix_juice("Berries & Lime")
2.5
```
## 2. Replenish the lime wedge supply
A lot of Li Mei's creations include lime wedges, either as an ingredient or as part of the decoration.
So when she starts her shift in the morning she needs to make sure the bin of lime wedges is full for the day ahead.
Implement the function `limes_to_cut` which takes the number of lime wedges Li Mei needs to cut and an array representing the supply of whole limes she has at hand.
She can get 6 wedges from a `"small"` lime, 8 wedges from a `"medium"` lime and 10 from a `"large"` lime.
She always cuts the limes in the order in which they appear in the list, starting with the first item.
She keeps going until she reached the number of wedges that she needs or until she runs out of limes.
Li Mei would like to know in advance how many limes she needs to cut.
The `limes_to_cut` function should return the number of limes to cut.
```julia-repl
julia> limes_to_cut(25, ["small", "small", "large", "medium", "small"])
4
```
## 3. List the times to mix each order in the queue
Li Mei likes to keep track of how long it will take to mix the orders customers are waiting for.
Implement the `order_times` function, which takes a queue of orders and returns a vector of times to mix.
```julia-repl
julia> order_times(["Energizer", "Tropical Island"])
[1.5, 3.0]
```
## 4. Finish up the shift
Li Mei always works until 3pm.
Then her employee Dmitry takes over.
There are often drinks that have been ordered but are not prepared yet when Li Mei's shift ends.
Dmitry will then prepare the remaining juices.
To make the hand-over easier, implement a function `remaining_orders` which takes the number of minutes left in Li Mei"s shift and an array of juices that have been ordered but not prepared yet.
The function should return the orders that Li Mei cannot start preparing before the end of her workday.
The time left in the shift will always be greater than 0.
The array of juices to prepare will never be empty.
Furthermore, the orders are prepared in the order in which they appear in the array.
If Li Mei starts to mix a certain juice, she will always finish it even if she has to work a bit longer.
If there are no remaining orders left that Dmitry needs to take care of, an empty vector should be returned.
```julia-repl
julia> remaining_orders(5, ["Energizer", "All or Nothing", "Green Garden"])
["Green Garden"]
```
## Source
### Created by
- @colinleach

View File

@@ -0,0 +1,19 @@
function time_to_mix_juice(juice)
end
function wedges_from_lime(size)
end
function limes_to_cut(needed, limes)
end
function order_times(orders)
end
function remaining_orders(time_left, orders)
end

View File

@@ -0,0 +1,171 @@
using Test
include("mixed-juices.jl")
@testset verbose = true "tests" begin
@testset "1. time_to_mix_juice" begin
@testset "Returns the correct time for 'Pure Strawberry Joy'" begin
@test time_to_mix_juice("Pure Strawberry Joy") == 0.5
end
@testset "Returns the correct time for 'Energizer'" begin
@test time_to_mix_juice("Energizer") == 1.5
end
@testset "Returns the correct time for 'Green Garden'" begin
@test time_to_mix_juice("Green Garden") == 1.5
end
@testset "Returns the correct time for 'Tropical Island'" begin
@test time_to_mix_juice("Tropical Island") == 3
end
@testset "Returns the correct time for 'All or Nothing'" begin
@test time_to_mix_juice("All or Nothing") == 5
end
@testset "Returns the correct time for all other juices" begin
default_time = 2.5
@test time_to_mix_juice("Limetime") == default_time
@test time_to_mix_juice("Manic Organic") == default_time
@test time_to_mix_juice("Papaya & Peach") == default_time
end
end
@testset "2. limes_to_cut" begin
@testset "Medium order" begin
limes = [
"small",
"large",
"large",
"medium",
"small",
"large",
"large",
"medium"
]
@test limes_to_cut(42, limes) == 6
end
@testset "Small order" begin
limes = [
"medium",
"small"
]
@test limes_to_cut(4, limes) == 1
end
@testset "Large order" begin
limes = [
"small",
"large",
"large",
"medium",
"small",
"large",
"large"
]
@test limes_to_cut(80, limes) == 7
end
@testset "If no new wedges are needed, no limes are cut" begin
limes = [
"small",
"large",
"medium"
]
@test limes_to_cut(0, limes) == 0
end
@testset "works if no limes are available" begin
limes = []
@test limes_to_cut(10, limes) == 0
end
end
@testset "3. order_times" begin
@testset "correctly determines the times for current orders" begin
orders = [
"Tropical Island",
"Energizer",
"Limetime",
"All or Nothing",
"Pure Strawberry Joy"
]
expected = [3.0, 1.5, 2.5, 5.0, 0.5]
@test order_times(orders) == expected
end
@testset "correctly determines the times for current orders" begin
orders = [
"Pure Strawberry Joy",
"Pure Strawberry Joy",
"Vitality",
"Tropical Island",
"All or Nothing",
"All or Nothing",
"All or Nothing",
"Green Garden",
"Limetime"
]
expected = [0.5, 0.5, 2.5, 3.0, 5.0, 5.0, 5.0, 1.5, 2.5]
@test order_times(orders) == expected
end
@testset "correctly returns an empty list if there are no orders" begin
orders = []
@test order_times(orders) == []
end
end
@testset "4. remaining_orders" begin
@testset "correctly determines the remaining orders" begin
orders = [
"Tropical Island",
"Energizer",
"Limetime",
"All or Nothing",
"Pure Strawberry Joy"
]
expected = ["All or Nothing", "Pure Strawberry Joy"]
@test remaining_orders(7, orders) == expected
end
@testset "correctly determines the remaining orders" begin
orders = [
"Pure Strawberry Joy",
"Pure Strawberry Joy",
"Vitality",
"Tropical Island",
"All or Nothing",
"All or Nothing",
"All or Nothing",
"Green Garden",
"Limetime"
]
expected = ["All or Nothing", "Green Garden", "Limetime"]
@test remaining_orders(13, orders) == expected
end
@testset "counts all orders as fulfilled if there is enough time" begin
orders = [
"Energizer",
"Green Garden",
"Ruby Glow",
"Pure Strawberry Joy",
"Tropical Island",
"Limetime"
]
@test remaining_orders(12, orders) == []
end
@testset "works if there is only very little time left" begin
orders = [
"Bananas Gone Wild",
"Pure Strawberry Joy"
]
expected = ["Pure Strawberry Joy"]
@test remaining_orders(0.2, orders) == expected
end
end
end