Some solutions for the julia path
This commit is contained in:
26
julia/annalyns-infiltration/.exercism/config.json
Normal file
26
julia/annalyns-infiltration/.exercism/config.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"authors": [
|
||||
"SaschaMann",
|
||||
"colinleach"
|
||||
],
|
||||
"contributors": [
|
||||
"cmcaine",
|
||||
"BNAndras"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"annalyns-infiltration.jl"
|
||||
],
|
||||
"test": [
|
||||
"runtests.jl"
|
||||
],
|
||||
"exemplar": [
|
||||
".meta/exemplar.jl"
|
||||
]
|
||||
},
|
||||
"language_versions": "≥1.0",
|
||||
"forked_from": [
|
||||
"javascript/annalyns-infiltration"
|
||||
],
|
||||
"blurb": "Learn about boolean expressions by implementing the quest logic for a new RPG."
|
||||
}
|
||||
1
julia/annalyns-infiltration/.exercism/metadata.json
Normal file
1
julia/annalyns-infiltration/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"julia","exercise":"annalyns-infiltration","id":"30cf911b357741e8a7c62eb77cf57308","url":"https://exercism.org/tracks/julia/exercises/annalyns-infiltration","handle":"Kimawari","is_requester":true,"auto_approve":false}
|
||||
34
julia/annalyns-infiltration/HELP.md
Normal file
34
julia/annalyns-infiltration/HELP.md
Normal 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 annalyns-infiltration.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/).
|
||||
23
julia/annalyns-infiltration/HINTS.md
Normal file
23
julia/annalyns-infiltration/HINTS.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# Hints
|
||||
|
||||
## General
|
||||
|
||||
- There are three [boolean operators][boolean-operators] to work with boolean values.
|
||||
- Multiple operators can be combined in a single expression.
|
||||
|
||||
## 1. Check if a fast attack can be made
|
||||
|
||||
- The logical NOT operator (`!`) can be placed before an expression to negate its value.
|
||||
|
||||
## 2. Check if the group can be spied upon
|
||||
|
||||
- Boolean operators are typically used to evaluate whether two or more expressions are true or not true.
|
||||
|
||||
## 3. Check if the prisoner can be signaled
|
||||
|
||||
- Boolean operators execute in the order of their precedence (from highest to lowest): `!`, `&&`, `||`.
|
||||
- In general, use of parentheses is encouraged to make your intention clearer.
|
||||
- For more details check out the Operator Precedence section on the [official Julia documentation][operator-precedence].
|
||||
|
||||
[boolean-operators]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Boolean-Operators
|
||||
[operator-precedence]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity
|
||||
162
julia/annalyns-infiltration/README.md
Normal file
162
julia/annalyns-infiltration/README.md
Normal file
@@ -0,0 +1,162 @@
|
||||
# Annalyns Infiltration
|
||||
|
||||
Welcome to Annalyns Infiltration 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
|
||||
|
||||
## Booleans in Julia
|
||||
|
||||
True or false values are represented by the `Bool` type.
|
||||
It contains only two values: `true` and `false`.
|
||||
|
||||
```julia-repl
|
||||
julia> true
|
||||
true
|
||||
|
||||
julia> false
|
||||
false
|
||||
```
|
||||
|
||||
## Boolean Operators
|
||||
|
||||
There are three Boolean operators in Julia.
|
||||
|
||||
`&&` is Boolean "and".
|
||||
It evaluates to `true` if the expressions on *both* sides of `&&` are `true`.
|
||||
|
||||
```julia-repl
|
||||
julia> true && true
|
||||
true
|
||||
|
||||
julia> true && false
|
||||
false
|
||||
```
|
||||
|
||||
`||` is Boolean "or".
|
||||
It evaluates to `true` if an expression on *either* side of `||` is `true`.
|
||||
|
||||
```julia-repl
|
||||
julia> true || true
|
||||
true
|
||||
|
||||
julia> false || true
|
||||
true
|
||||
```
|
||||
|
||||
`!` is Boolean "not".
|
||||
It exchanges `true` and `false` values.
|
||||
|
||||
```julia-repl
|
||||
julia> !true
|
||||
false
|
||||
|
||||
julia> !false
|
||||
true
|
||||
```
|
||||
|
||||
For longer and more complicated expressions, it is best to use parentheses to make your intention clear.
|
||||
|
||||
```julia-repl
|
||||
julia> (true || false) && (false && true)
|
||||
false
|
||||
```
|
||||
|
||||
## Instructions
|
||||
|
||||
In this exercise, you'll be writing some logic for a video game a friend is developing.
|
||||
The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog.
|
||||
Unfortunately, disaster strikes, as her best friend is kidnapped while searching for berries in the forest.
|
||||
Annalyn will try to find and free her friend, optionally taking her dog with her on this quest.
|
||||
|
||||
Annalyn eventually finds the camp in which her friend is imprisoned and it turns out there are two kidnappers: a mighty knight and a cunning archer.
|
||||
|
||||
The player is presented with some options for what to do next.
|
||||
For each of the four possible options you need to write a function that tells the game whether it should show that option or not.
|
||||
|
||||
## 1. Check if the 'Fast Attack' option should be shown
|
||||
|
||||
If the knight is sleeping, then Annalyn will be able to make a quick attack into the camp before he can wake up properly and get his armour on.
|
||||
|
||||
Implement a function named `can_do_fast_attack` that takes a boolean value which indicates if the knight is awake.
|
||||
This function returns `true` if the 'Fast Attack' action is available based on the state of the character. Otherwise, returns `false`:
|
||||
|
||||
```julia-repl
|
||||
julia> knight_awake = true
|
||||
|
||||
julia> can_do_fast_attack(knight_awake)
|
||||
false
|
||||
```
|
||||
|
||||
## 2. Check if the 'Spy' option should be shown
|
||||
|
||||
The group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time.
|
||||
|
||||
Implement a function named `can_spy` that takes three boolean values, indicating if the knight, archer and prisoner, respectively, are awake.
|
||||
The function returns `true` if the 'Spy' action is available based on the state of the characters.
|
||||
Otherwise, returns `false`:
|
||||
|
||||
```julia-repl
|
||||
# Output suppressed from the next 3 lines
|
||||
julia> knight_awake = false
|
||||
julia> archer_awake = true
|
||||
julia> prisoner_awake = false
|
||||
|
||||
julia> can_spy(knight_awake, archer_awake, prisoner_awake)
|
||||
true
|
||||
```
|
||||
|
||||
## 3. Check if the 'Signal Prisoner' option should be shown
|
||||
|
||||
The prisoner can be signalled using bird sounds if she is awake and the archer is sleeping.
|
||||
If the archer is awake then she can't be safely signaled because the archer is also trained in bird signalling.
|
||||
|
||||
Implement a function named `can_signal_prisoner` that takes two boolean values, indicating if the archer and prisoner, respectively, are awake.
|
||||
The function returns `true` if the 'Signal Prisoner' action is available based on the state of the characters.
|
||||
Otherwise, returns `false`:
|
||||
|
||||
```julia-repl
|
||||
julia> archer_awake = false
|
||||
julia> prisoner_awake = true
|
||||
|
||||
julia> can_signal_prisoner(archer_awake, prisoner_awake)
|
||||
true
|
||||
```
|
||||
|
||||
## 4. Check if the 'Free Prisoner' option should be shown
|
||||
|
||||
Annalyn can try sneaking into the camp to free her friend. This is a risky thing to do and can only succeed in one of two ways:
|
||||
|
||||
- If Annalyn has her pet dog with her, she can rescue the prisoner if the archer is asleep.
|
||||
The knight is scared of the dog and the archer will not have time to get ready before Annalyn and her friend can escape.
|
||||
|
||||
- If Annalyn does not have her dog then she and the prisoner must be very sneaky!
|
||||
Annalyn can free the prisoner if she is awake and the knight and archer are both sleeping, but if the prisoner is sleeping, she can't be rescued: she would be startled by Annalyn's sudden appearance and wake up the knight and archer.
|
||||
|
||||
Implement a function named `can_free_prisoner` that takes four boolean values.
|
||||
The first three parameters indicate if the knight, archer and prisoner, respectively, are awake.
|
||||
The last parameter indicates if Annalyn's pet dog is present.
|
||||
The function returns `true` if the 'Free Prisoner' action is available based on the state of the characters. Otherwise, it returns `false`:
|
||||
|
||||
```julia-repl
|
||||
julia> knight_awake = false
|
||||
julia> archer_awake = true
|
||||
julia> prisoner_awake = false
|
||||
julia> dog_present = false
|
||||
|
||||
julia> can_free_prisoner(knight_awake, archer_awake, prisoner_awake, dog_present)
|
||||
false
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @SaschaMann
|
||||
- @colinleach
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @cmcaine
|
||||
- @BNAndras
|
||||
15
julia/annalyns-infiltration/annalyns-infiltration.jl
Normal file
15
julia/annalyns-infiltration/annalyns-infiltration.jl
Normal file
@@ -0,0 +1,15 @@
|
||||
function can_do_fast_attack(knight_awake)
|
||||
return !knight_awake
|
||||
end
|
||||
|
||||
function can_spy(knight_awake, archer_awake, prisoner_awake)
|
||||
return (knight_awake || archer_awake || prisoner_awake)
|
||||
end
|
||||
|
||||
function can_signal_prisoner(archer_awake, prisoner_awake)
|
||||
return !archer_awake && prisoner_awake
|
||||
end
|
||||
|
||||
function can_free_prisoner(knight_awake, archer_awake, prisoner_awake, dog_present)
|
||||
return (dog_present && !archer_awake) || (!dog_present && prisoner_awake && !knight_awake && !archer_awake)
|
||||
end
|
||||
71
julia/annalyns-infiltration/runtests.jl
Normal file
71
julia/annalyns-infiltration/runtests.jl
Normal file
@@ -0,0 +1,71 @@
|
||||
using Test
|
||||
|
||||
include("annalyns-infiltration.jl")
|
||||
|
||||
# Julia 1.0 compat
|
||||
# The function definition of eachrow is taken from Julia Base,
|
||||
# released under the MIT license: https://julialang.org/license
|
||||
if VERSION < v"1.1"
|
||||
@eval eachrow(A) = (view(A, i, :) for i in axes(A, 1))
|
||||
end
|
||||
|
||||
@testset verbose = true "tests" begin
|
||||
@testset "fast attack" begin
|
||||
@test !can_do_fast_attack(true)
|
||||
@test can_do_fast_attack(false)
|
||||
end
|
||||
|
||||
@testset "spying" begin
|
||||
character_state_combinations = Bool[
|
||||
0 0 0 0;
|
||||
0 0 1 1;
|
||||
0 1 0 1;
|
||||
0 1 1 1;
|
||||
1 0 0 1;
|
||||
1 0 1 1;
|
||||
1 1 1 1;
|
||||
]
|
||||
|
||||
for state in eachrow(character_state_combinations)
|
||||
@test can_spy(state[1:3]...) == state[4]
|
||||
end
|
||||
end
|
||||
|
||||
@testset "signaling prisoner" begin
|
||||
character_state_combinations = Bool[
|
||||
0 0 0;
|
||||
0 1 1;
|
||||
1 0 0;
|
||||
1 1 0;
|
||||
]
|
||||
|
||||
for state in eachrow(character_state_combinations)
|
||||
@test can_signal_prisoner(state[1:2]...) == state[3]
|
||||
end
|
||||
end
|
||||
|
||||
@testset "freeing prisoner" begin
|
||||
character_state_combinations = Bool[
|
||||
0 0 0 0 0;
|
||||
0 0 0 1 1;
|
||||
0 0 1 0 1;
|
||||
0 0 1 1 1;
|
||||
0 1 0 0 0;
|
||||
0 1 0 1 0;
|
||||
0 1 1 0 0;
|
||||
0 1 1 1 0;
|
||||
1 0 0 0 0;
|
||||
1 0 0 1 1;
|
||||
1 0 1 0 0;
|
||||
1 0 1 1 1;
|
||||
1 1 0 0 0;
|
||||
1 1 0 1 0;
|
||||
1 1 1 0 0;
|
||||
1 1 1 1 0;
|
||||
]
|
||||
|
||||
for state in eachrow(character_state_combinations)
|
||||
@test can_free_prisoner(state[1:4]...) == state[5]
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user