Some solutions for the julia path
This commit is contained in:
24
julia/elyses-enchantments/.exercism/config.json
Normal file
24
julia/elyses-enchantments/.exercism/config.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"authors": [
|
||||
"colinleach"
|
||||
],
|
||||
"contributors": [
|
||||
"cmcaine",
|
||||
"SaschaMann"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"elyses-enchantments.jl"
|
||||
],
|
||||
"test": [
|
||||
"runtests.jl"
|
||||
],
|
||||
"exemplar": [
|
||||
".meta/exemplar.jl"
|
||||
]
|
||||
},
|
||||
"forked_from": [
|
||||
"javascript/enchantments"
|
||||
],
|
||||
"blurb": "Help Elyse with her Enchantments and learn about vectors in the process."
|
||||
}
|
||||
1
julia/elyses-enchantments/.exercism/metadata.json
Normal file
1
julia/elyses-enchantments/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"julia","exercise":"elyses-enchantments","id":"8ff6920e905b4432a422b226ce01e7d4","url":"https://exercism.org/tracks/julia/exercises/elyses-enchantments","handle":"Kimawari","is_requester":true,"auto_approve":false}
|
||||
34
julia/elyses-enchantments/HELP.md
Normal file
34
julia/elyses-enchantments/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 elyses-enchantments.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/).
|
||||
42
julia/elyses-enchantments/HINTS.md
Normal file
42
julia/elyses-enchantments/HINTS.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# Hints
|
||||
|
||||
## 1. Retrieve a card from a stack
|
||||
|
||||
- `Vector` indices start at `1`.
|
||||
- You can retrieve the `n`th value in a vector `v` with `v[n]`.
|
||||
|
||||
## 2. Exchange a card in the stack
|
||||
|
||||
- `Vector`s are mutable, you can change their contents at any time by assigning a new value.
|
||||
|
||||
## 3. Insert a card at the of top of the stack
|
||||
|
||||
- There is a method [`push!`][push] in Base to add a new value to the end of a collection.
|
||||
|
||||
## 4. Remove a card from the stack
|
||||
|
||||
- There is a method [`deleteat!`][deleteat] in Base to delete an element from a `Vector` at a given index.
|
||||
|
||||
## 5. Remove the top card from the stack
|
||||
|
||||
- There is a method [`pop!`][pop] in Base to remove an element at the end of a collection.
|
||||
|
||||
## 6. Insert a card at the bottom of the stack
|
||||
|
||||
- There is a method [`pushfirst!`][pushfirst] in Base to insert a new value to start of a collection.
|
||||
|
||||
## 7. Remove a card from the bottom of the stack
|
||||
|
||||
- There is a method [`popfirst!`][popfirst] in Base to remove an element at the start of a collection.
|
||||
|
||||
## 8. Check the size of the stack
|
||||
|
||||
- There is a method [`length`][length] in Base to retrieve the length of a collection.
|
||||
|
||||
[push]: https://docs.julialang.org/en/v1/base/collections/#Base.push!
|
||||
[pop]: https://docs.julialang.org/en/v1/base/collections/#Base.pop!
|
||||
[pushfirst]: https://docs.julialang.org/en/v1/base/collections/#Base.pushfirst!
|
||||
[popfirst]: https://docs.julialang.org/en/v1/base/collections/#Base.popfirst!
|
||||
[insert]: https://docs.julialang.org/en/v1/base/collections/#Base.insert!
|
||||
[deleteat]: https://docs.julialang.org/en/v1/base/collections/#Base.deleteat!
|
||||
[length]: https://docs.julialang.org/en/v1/base/collections/#Base.length
|
||||
243
julia/elyses-enchantments/README.md
Normal file
243
julia/elyses-enchantments/README.md
Normal file
@@ -0,0 +1,243 @@
|
||||
# Elyses Enchantments
|
||||
|
||||
Welcome to Elyses Enchantments 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
|
||||
|
||||
A central aim of Julia is to be able to perform calculations on numerical data, quickly and efficiently.
|
||||
_Lots_ of numerical data.
|
||||
|
||||
Typically, the data will be stored in arrays (or some related type), so it is reasonable to say that arrays are at the heart of the Julia language.
|
||||
|
||||
Arrays can be of arbitrary size (subject only to memory constraints in your hardware), and can have arbitrarily many dimensions.
|
||||
|
||||
This introductory Concept concentrates on the basics of 1-dimensional arrays, which are called Vectors.
|
||||
|
||||
## Creating Vectors
|
||||
|
||||
In Julia, a Vector is an ordered sequence of values that can be accessed by index number.
|
||||
|
||||
The simplest way to create a Vector is to list the values in square brackets:
|
||||
|
||||
```julia-repl
|
||||
julia> num_vec = [1, 4, 9]
|
||||
3-element Vector{Int64}:
|
||||
1
|
||||
4
|
||||
9
|
||||
|
||||
julia> str_vec = ["arrays", "are", "important"]
|
||||
3-element Vector{String}:
|
||||
"arrays"
|
||||
"are"
|
||||
"important"
|
||||
```
|
||||
|
||||
The Vector type matches the type of each element.
|
||||
|
||||
_So Vectors need to be homogeneous (all elements the same type)?_
|
||||
|
||||
At some level, yes — and it is recommended to aim for this if you want the best performance.
|
||||
However, Julia can work round this limitation (when necessary) by using the `Any` type:
|
||||
|
||||
```julia-repl
|
||||
julia> mixed_vector = [1, "str", 'c']
|
||||
3-element Vector{Any}:
|
||||
1
|
||||
"str"
|
||||
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
|
||||
```
|
||||
|
||||
Please read and remember this rule:
|
||||
|
||||
- ***By default, indexing in Julia starts at 1, not 0***.
|
||||
|
||||
This is familiar to anyone who has used other data science languages (R, Matlab, Fortran...), but may seem shocking to computer scientists with a background in C-like languages.
|
||||
|
||||
Otherwise, indexes work as you might guess: just put them in square brackets:
|
||||
|
||||
```julia
|
||||
squares = [0, 1, 4, 9, 16]
|
||||
squares[1] # 0
|
||||
squares[3] # 4
|
||||
squares[begin] # 0 ("begin" is synonym for 1)
|
||||
squares[end] # 16 ("end" is synonym for length(squares))
|
||||
```
|
||||
|
||||
Note the convenience of indexing with `end` (which is very useful) and `begin` (which is probably not).
|
||||
|
||||
Python programmers may be wondering about negative indices.
|
||||
Don't: these are not part of Julia, and will raise a `BoundsError`, as will any index smaller than 1 or bigger than `length(squares)`.
|
||||
|
||||
## Vector operations
|
||||
|
||||
Vectors in Julia are _mutable_: we can change the contents of individual cells.
|
||||
|
||||
```julia-repl
|
||||
julia> vals = [1, 3, 5, 7]
|
||||
4-element Vector{Int64}:
|
||||
1
|
||||
3
|
||||
5
|
||||
7
|
||||
|
||||
julia> vals[2] = 4 # reassign the value of this element
|
||||
4
|
||||
|
||||
# Only the value at position 2 changes:
|
||||
julia> vals
|
||||
4-element Vector{Int64}:
|
||||
1
|
||||
4
|
||||
5
|
||||
7
|
||||
```
|
||||
|
||||
There are many functions available for manipulating vectors, though the Julia documentation generalizes them to "collections" rather than vectors.
|
||||
|
||||
Note that, by convention, functions that mutate their input have `!` in the name.
|
||||
The compiler will not enforce this, but it is a very _strong_ convention in the Julia world.
|
||||
|
||||
These are a few of the useful functions:
|
||||
|
||||
- To add values to the end of the vector, use `push!()`.
|
||||
- To remove the last value, use `pop!()`.
|
||||
- To operate on the start of the vector, the corresponding functions are `pushfirst!()` and `popfirst!()`.
|
||||
- To insert or remove an element at any position, there is `insert!()` and `deleteat!()`.
|
||||
|
||||
```julia-repl
|
||||
julia> vals = [1, 3]
|
||||
2-element Vector{Int64}:
|
||||
1
|
||||
3
|
||||
|
||||
julia> push!(vals, 5, 6) # can add multiple values
|
||||
4-element Vector{Int64}:
|
||||
1
|
||||
3
|
||||
5
|
||||
6
|
||||
|
||||
julia> pop!(vals) # mutates vals, return popped value
|
||||
6
|
||||
|
||||
julia> vals
|
||||
3-element Vector{Int64}:
|
||||
1
|
||||
3
|
||||
5
|
||||
```
|
||||
|
||||
## Instructions
|
||||
|
||||
As a magician-to-be, Elyse needs to practice some basics.
|
||||
She has a stack of cards that she wants to manipulate.
|
||||
|
||||
To make things a bit easier she only uses the cards 1 to 10 so her stack of cards can be represented by a vector of numbers.
|
||||
The position of a certain card corresponds to the index in the vector.
|
||||
|
||||
## 1. Retrieve a card from a stack
|
||||
|
||||
To pick a card, return the card at index `position` from the given stack.
|
||||
|
||||
```julia-repl
|
||||
julia> stack = [1, 2, 4, 1];
|
||||
julia> position = 3;
|
||||
julia> get_item(stack, position)
|
||||
4
|
||||
```
|
||||
|
||||
## 2. Exchange a card in the stack
|
||||
|
||||
Perform some sleight of hand and exchange the card at index `position` with the replacement card provided.
|
||||
Return the adjusted stack.
|
||||
|
||||
```julia-repl
|
||||
julia> stack = [1, 2, 4, 1];
|
||||
julia> position = 3;
|
||||
julia> replacement_card = 6;
|
||||
julia> set_item!(stack, position, replacement_card)
|
||||
[1, 2, 6, 1]
|
||||
```
|
||||
|
||||
## 3. Insert a card at the top of the stack
|
||||
|
||||
Make a card appear by inserting a new card at the top of the stack.
|
||||
Return the adjusted stack.
|
||||
|
||||
```julia-repl
|
||||
julia> stack = [5, 9, 7, 1];
|
||||
julia> new_card = 8;
|
||||
julia> insert_item_at_top!(stack, new_card)
|
||||
[5, 9, 7, 1, 8]
|
||||
```
|
||||
|
||||
## 4. Remove a card from the stack
|
||||
|
||||
Make a card disappear by removing the card at the given `position` from the stack.
|
||||
Return the adjusted stack.
|
||||
|
||||
```julia-repl
|
||||
julia> stack = [3, 2, 6, 4, 8];
|
||||
julia> position = 3;
|
||||
julia> remove_item!(stack, position)
|
||||
[3, 2, 4, 8]
|
||||
```
|
||||
|
||||
## 5. Remove the top card from the stack
|
||||
|
||||
Make a card disappear by removing the card at the top of the stack.
|
||||
Return the adjusted stack.
|
||||
|
||||
```julia-repl
|
||||
julia> stack = [3, 2, 6, 4, 8];
|
||||
julia> remove_item_from_top!(stack)
|
||||
[3, 2, 6, 4]
|
||||
```
|
||||
|
||||
## 6. Insert a card at the bottom of the stack
|
||||
|
||||
Make a card appear by inserting a new card at the bottom of the stack.
|
||||
Return the adjusted stack.
|
||||
|
||||
```julia-repl
|
||||
julia> stack = [5, 9, 7, 1];
|
||||
julia> new_card = 8;
|
||||
julia> insert_item_at_bottom!(stack, new_card)
|
||||
[8, 5, 9, 7, 1]
|
||||
```
|
||||
|
||||
## 7. Remove a card from the bottom of the stack
|
||||
|
||||
Make a card disappear by removing the card at the bottom of the stack.
|
||||
Return the adjusted stack.
|
||||
|
||||
```julia-repl
|
||||
julia> stack = [8, 5, 9, 7, 1];
|
||||
julia> remove_item_at_bottom!(stack)
|
||||
[5, 9, 7, 1]
|
||||
```
|
||||
|
||||
## 8. Check the size of the stack
|
||||
|
||||
Check whether the size of the stack is equal to `stack_size` or not.
|
||||
|
||||
```julia-repl
|
||||
julia> stack = [3, 2, 6, 4, 8];
|
||||
julia> stack_size = 4;
|
||||
julia> check_size_of_stack(stack, stack_size)
|
||||
false
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @colinleach
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @cmcaine
|
||||
- @SaschaMann
|
||||
50
julia/elyses-enchantments/elyses-enchantments.jl
Normal file
50
julia/elyses-enchantments/elyses-enchantments.jl
Normal file
@@ -0,0 +1,50 @@
|
||||
function get_item(stack, position)
|
||||
if position < 1 || position > length(stack)
|
||||
error("Position out of bounds")
|
||||
end
|
||||
return stack[position]
|
||||
end
|
||||
|
||||
function set_item!(stack, position, replacement_card)
|
||||
if position < 1 || position > length(stack)
|
||||
error("Position out of bounds")
|
||||
end
|
||||
stack[position] = replacement_card
|
||||
return stack
|
||||
end
|
||||
|
||||
function insert_item_at_top!(stack, new_card)
|
||||
push!(stack, new_card)
|
||||
return stack
|
||||
end
|
||||
|
||||
function remove_item!(stack, position)
|
||||
if position < 1 || position > length(stack)
|
||||
error("Position out of bounds")
|
||||
end
|
||||
deleteat!(stack, position)
|
||||
end
|
||||
|
||||
function remove_item_from_top!(stack)
|
||||
if isempty(stack)
|
||||
error("Stack is empty")
|
||||
end
|
||||
return deleteat!(stack, length(stack))
|
||||
end
|
||||
|
||||
function insert_item_at_bottom!(stack, new_card)
|
||||
pushfirst!(stack, new_card)
|
||||
return stack
|
||||
end
|
||||
|
||||
function remove_item_at_bottom!(stack)
|
||||
if isempty(stack)
|
||||
error("Stack is empty")
|
||||
end
|
||||
popfirst!(stack)
|
||||
return stack
|
||||
end
|
||||
|
||||
function check_size_of_stack(stack, stack_size)
|
||||
return length(stack) == stack_size
|
||||
end
|
||||
229
julia/elyses-enchantments/runtests.jl
Normal file
229
julia/elyses-enchantments/runtests.jl
Normal file
@@ -0,0 +1,229 @@
|
||||
using Test
|
||||
|
||||
include("elyses-enchantments.jl")
|
||||
|
||||
@testset verbose = true "tests" begin
|
||||
@testset "Retrieve a card from a deck" begin
|
||||
|
||||
@testset "get the first card" begin
|
||||
stack = [1, 2, 3]
|
||||
position = 1
|
||||
@test get_item(stack, position) == 1
|
||||
end
|
||||
|
||||
@testset "get the middle card" begin
|
||||
stack = [4, 5, 6]
|
||||
position = 2
|
||||
@test get_item(stack, position) == 5
|
||||
end
|
||||
|
||||
@testset "get the last card" begin
|
||||
stack = [9, 8, 7]
|
||||
position = 3
|
||||
@test get_item(stack, position) == 7
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@testset "Replace a card in a deck" begin
|
||||
|
||||
@testset "replace the first card with a 7" begin
|
||||
stack = [1, 2, 3]
|
||||
position = 1
|
||||
replacement_card = 7
|
||||
@test set_item!(stack, position, replacement_card) == [7, 2, 3]
|
||||
end
|
||||
|
||||
@testset "replace the middle card with a 5" begin
|
||||
stack = [2, 2, 2]
|
||||
position = 2
|
||||
replacement_card = 5
|
||||
@test set_item!(stack, position, replacement_card) == [2, 5, 2]
|
||||
end
|
||||
|
||||
@testset "replace the last card with a 7" begin
|
||||
stack = [7, 7, 6]
|
||||
position = 3
|
||||
replacement_card = 7
|
||||
@test set_item!(stack, position, replacement_card) == [7, 7, 7]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@testset "Add a card at the top" begin
|
||||
|
||||
@testset "adding a second card at the top" begin
|
||||
stack = [1]
|
||||
new_card = 5
|
||||
@test insert_item_at_top!(stack, new_card) == [1, 5]
|
||||
end
|
||||
|
||||
@testset "adding a third card at the top" begin
|
||||
stack = [1, 5]
|
||||
new_card = 9
|
||||
@test insert_item_at_top!(stack, new_card) == [1, 5, 9]
|
||||
end
|
||||
|
||||
@testset "adding a fourth card at the top" begin
|
||||
stack = [1, 5, 9]
|
||||
new_card = 2
|
||||
@test insert_item_at_top!(stack, new_card) == [1, 5, 9, 2]
|
||||
end
|
||||
|
||||
@testset "adding a different fourth card at the top" begin
|
||||
stack = [1, 5, 9]
|
||||
new_card = 8
|
||||
@test insert_item_at_top!(stack, new_card) == [1, 5, 9, 8]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@testset "Removing a card" begin
|
||||
|
||||
@testset "remove the card at the bottom" begin
|
||||
stack = [1, 2, 3, 4]
|
||||
position = 1
|
||||
@test remove_item!(stack, position) == [2, 3, 4]
|
||||
end
|
||||
|
||||
@testset "remove the card at the top" begin
|
||||
stack = [1, 2, 3, 4]
|
||||
position = 4
|
||||
@test remove_item!(stack, position) == [1, 2, 3]
|
||||
end
|
||||
|
||||
@testset "remove the second card" begin
|
||||
stack = [1, 2, 3, 4]
|
||||
position = 2
|
||||
@test remove_item!(stack, position) == [1, 3, 4]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@testset "Removing a card from the top" begin
|
||||
|
||||
@testset "remove the only card from the top" begin
|
||||
stack = [1]
|
||||
@test remove_item_from_top!(stack) == []
|
||||
end
|
||||
|
||||
@testset "remove the card from the top" begin
|
||||
stack = [1, 2, 3]
|
||||
@test remove_item_from_top!(stack) == [1, 2]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@testset "Add a card at the bottom" begin
|
||||
|
||||
@testset "adding a second card to the bottom" begin
|
||||
stack = [1]
|
||||
new_card = 5
|
||||
@test insert_item_at_bottom!(stack, new_card) == [5, 1]
|
||||
end
|
||||
|
||||
@testset "adding a third card to the bottom" begin
|
||||
stack = [5, 1]
|
||||
new_card = 9
|
||||
@test insert_item_at_bottom!(stack, new_card) == [9, 5, 1]
|
||||
end
|
||||
|
||||
@testset "adding a fourth card to the bottom" begin
|
||||
stack = [9, 5, 1]
|
||||
new_card = 2
|
||||
@test insert_item_at_bottom!(stack, new_card) == [2, 9, 5, 1]
|
||||
end
|
||||
|
||||
@testset "adding a different fourth card to the bottom" begin
|
||||
stack = [9, 5, 1]
|
||||
new_card = 8
|
||||
@test insert_item_at_bottom!(stack, new_card) == [8, 9, 5, 1]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@testset "Remove a card from the bottom" begin
|
||||
|
||||
@testset "remove the only card from the bottom" begin
|
||||
stack = [1]
|
||||
@test remove_item_at_bottom!(stack) == []
|
||||
end
|
||||
|
||||
@testset "remove the card from the bottom" begin
|
||||
stack = [1, 2, 3]
|
||||
@test remove_item_at_bottom!(stack) == [2, 3]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@testset "Check size of stack" begin
|
||||
|
||||
@testset "an empty stack of cards" begin
|
||||
stack = []
|
||||
stack_size = 0
|
||||
@test check_size_of_stack(stack, stack_size) == true
|
||||
end
|
||||
|
||||
@testset "an empty stack of cards" begin
|
||||
stack = []
|
||||
stack_size = 1
|
||||
@test check_size_of_stack(stack, stack_size) == false
|
||||
end
|
||||
|
||||
@testset "has exactly 1 card" begin
|
||||
stack = [7]
|
||||
stack_size = 0
|
||||
@test check_size_of_stack(stack, stack_size) == false
|
||||
end
|
||||
|
||||
@testset "has exactly 1 card" begin
|
||||
stack = [7]
|
||||
stack_size = 1
|
||||
@test check_size_of_stack(stack, stack_size) == true
|
||||
end
|
||||
|
||||
@testset "has exactly 1 card" begin
|
||||
stack = [7]
|
||||
stack_size = 2
|
||||
@test check_size_of_stack(stack, stack_size) == false
|
||||
end
|
||||
|
||||
@testset "has exactly 4 cards" begin
|
||||
stack = [2, 4, 6, 8]
|
||||
stack_size = 3
|
||||
@test check_size_of_stack(stack, stack_size) == false
|
||||
end
|
||||
|
||||
@testset "has exactly 4 cards" begin
|
||||
stack = [2, 4, 6, 8]
|
||||
stack_size = 4
|
||||
@test check_size_of_stack(stack, stack_size) == true
|
||||
end
|
||||
|
||||
@testset "has exactly 4 cards" begin
|
||||
stack = [2, 4, 6, 8]
|
||||
stack_size = 15
|
||||
@test check_size_of_stack(stack, stack_size) == false
|
||||
end
|
||||
|
||||
@testset "has exactly 5 cards" begin
|
||||
stack = [1, 3, 5, 7, 9]
|
||||
stack_size = 3
|
||||
@test check_size_of_stack(stack, stack_size) == false
|
||||
end
|
||||
|
||||
@testset "has exactly 5 cards" begin
|
||||
stack = [1, 3, 5, 7, 9]
|
||||
stack_size = 4
|
||||
@test check_size_of_stack(stack, stack_size) == false
|
||||
end
|
||||
|
||||
@testset "has exactly 5 cards" begin
|
||||
stack = [1, 3, 5, 7, 9]
|
||||
stack_size = 5
|
||||
@test check_size_of_stack(stack, stack_size) == true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user