Some solutions for the julia path
This commit is contained in:
20
julia/chessboard/.exercism/config.json
Normal file
20
julia/chessboard/.exercism/config.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"authors": [
|
||||
"colinleach"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"chessboard.jl"
|
||||
],
|
||||
"test": [
|
||||
"runtests.jl"
|
||||
],
|
||||
"exemplar": [
|
||||
".meta/exemplar.jl"
|
||||
]
|
||||
},
|
||||
"forked_from": [
|
||||
"elixir/chessboard"
|
||||
],
|
||||
"blurb": "Learn about ranges by building a chessboard."
|
||||
}
|
||||
1
julia/chessboard/.exercism/metadata.json
Normal file
1
julia/chessboard/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"julia","exercise":"chessboard","id":"dc930c6af16b45df91a8634905880861","url":"https://exercism.org/tracks/julia/exercises/chessboard","handle":"Kimawari","is_requester":true,"auto_approve":false}
|
||||
34
julia/chessboard/HELP.md
Normal file
34
julia/chessboard/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 chessboard.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/).
|
||||
22
julia/chessboard/HINTS.md
Normal file
22
julia/chessboard/HINTS.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# Hints
|
||||
|
||||
This is a short and simple exercise, so it is best to avoid complicating it.
|
||||
|
||||
## 1. Define the rank range
|
||||
|
||||
- You need to return a range of integers
|
||||
|
||||
## 2. Define the file range
|
||||
|
||||
- You need to return a range of uppercase characters.
|
||||
- Characters use single-quotes, such as `'W'`,
|
||||
|
||||
## 3. Transform the rank range into a list of ranks
|
||||
|
||||
- The [`collect()`][collect] function is your friend here.
|
||||
|
||||
## 4. Transform the file range into a list of files
|
||||
|
||||
- See task 3.
|
||||
|
||||
[collect]: https://docs.julialang.org/en/v1/base/collections/#Base.collect-Tuple{Any}
|
||||
149
julia/chessboard/README.md
Normal file
149
julia/chessboard/README.md
Normal file
@@ -0,0 +1,149 @@
|
||||
# Chessboard
|
||||
|
||||
Welcome to Chessboard 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
|
||||
|
||||
Suppose you want all the non-negative integers up to 1000.
|
||||
It would be ridiculous if you had to type these into an array.
|
||||
|
||||
For this we have the `range` type:
|
||||
|
||||
```julia-repl
|
||||
julia> 0:1000
|
||||
0:1000
|
||||
|
||||
julia> typeof(0:1000)
|
||||
UnitRange{Int64}
|
||||
```
|
||||
|
||||
Ranges are very common: not just to save you typing, but also as return types from functions.
|
||||
|
||||
Note that ranges are _not_ vectors.
|
||||
They are just a set of instructions to generate a sequence ("lazy" evaluation, or an "iterator").
|
||||
|
||||
If you need a range as a vector, use the `collect()` function for conversion:
|
||||
|
||||
```julia-repl
|
||||
julia> collect(0:5)
|
||||
6-element Vector{Int64}:
|
||||
0
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
```
|
||||
|
||||
The step size can be specified, in this case 0.3:
|
||||
|
||||
```julia-repl
|
||||
julia> collect(1.0:0.3:2.0)
|
||||
4-element Vector{Float64}:
|
||||
1.0
|
||||
1.3
|
||||
1.6
|
||||
1.9
|
||||
```
|
||||
|
||||
So the syntax is `start:stepsize:stop`.
|
||||
Both end limits are _inclusive_, as seen in the integer example.
|
||||
If the step size does not divide exactly into `stop - start`, the last element will avoid exceeding `stop`.
|
||||
|
||||
## Letter ranges
|
||||
|
||||
Non-numeric sequences can also be used in ranges.
|
||||
The simplest example is ASCII letters:
|
||||
|
||||
```julia-repl
|
||||
julia> 'a':'d'
|
||||
'a':1:'d'
|
||||
|
||||
julia> typeof('a':'d')
|
||||
StepRange{Char, Int64}
|
||||
|
||||
julia> collect('a':'d')
|
||||
4-element Vector{Char}:
|
||||
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
|
||||
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
|
||||
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
|
||||
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
|
||||
```
|
||||
|
||||
The `Char` type will be covered in more detail in another Concept.
|
||||
For now, just treat these as single characters in single-quotes.
|
||||
|
||||
## Functions and operators for ranges
|
||||
|
||||
Check the limits of a range with `first()` and `last()`.
|
||||
|
||||
```julia
|
||||
r = 1:10 # => 1:10
|
||||
first(r) # => 1
|
||||
last(r) # => 10
|
||||
```
|
||||
|
||||
## More on vector indexing
|
||||
|
||||
Integer ranges and vectors can be used in vector indexing:
|
||||
|
||||
```julia
|
||||
nums = collect(10.0:50.0)
|
||||
nums[3:2:7] # gives [12.0, 14.0, 16.0]
|
||||
nums[ [3, 5, 7] ] # also gives [12.0, 14.0, 16.0]
|
||||
```
|
||||
|
||||
## Instructions
|
||||
|
||||
As a chess enthusiast, you would like to write your own version of the game. Yes, there maybe plenty of implementations of chess available online already, but yours will be unique!
|
||||
|
||||
But before you can let your imagination run wild, you need to take care of the basics. Let's start by generating the board.
|
||||
|
||||
Each square of the chessboard is identified by a letter-number pair. The vertical columns of squares, called files, are labeled A through H. The horizontal rows of squares, called ranks, are numbered 1 to 8.
|
||||
|
||||
## 1. Define the rank range
|
||||
|
||||
Implement the `rank_range()` function. It should return a range of integers, from 1 to 8.
|
||||
|
||||
```julia-repl
|
||||
julia> rank_range()
|
||||
# output omitted
|
||||
```
|
||||
|
||||
## 2. Define the file range
|
||||
|
||||
Implement the `file_range()` function.
|
||||
It should return a range of integers, from the uppercase letter A, to the uppercase letter H.
|
||||
|
||||
```julia-repl
|
||||
julia> file_range()
|
||||
# output omitted
|
||||
```
|
||||
|
||||
## 3. Transform the rank range into a vector of ranks
|
||||
|
||||
Implement the `ranks()` function. It should return a vector of integers, from 1 to 8.
|
||||
Do not write the vector by hand, generate it from the range returned by the `rank_range()` function.
|
||||
|
||||
```julia-repl
|
||||
julia> ranks()
|
||||
[1, 2, 3, 4, 5, 6, 7, 8]
|
||||
```
|
||||
|
||||
## 4. Transform the file range into a vector of files
|
||||
|
||||
Implement the `files` function. It should return a vector of characters, from 'A' to 'H'.
|
||||
Do not write the vector by hand, generate it from the range returned by the `file_range()` function.
|
||||
|
||||
```julia-repl
|
||||
julia> files()
|
||||
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @colinleach
|
||||
15
julia/chessboard/chessboard.jl
Normal file
15
julia/chessboard/chessboard.jl
Normal file
@@ -0,0 +1,15 @@
|
||||
function rank_range()
|
||||
return 1:8
|
||||
end
|
||||
|
||||
function file_range()
|
||||
return 'A':'H'
|
||||
end
|
||||
|
||||
function ranks()
|
||||
return collect(rank_range())
|
||||
end
|
||||
|
||||
function files()
|
||||
return collect(file_range())
|
||||
end
|
||||
31
julia/chessboard/runtests.jl
Normal file
31
julia/chessboard/runtests.jl
Normal file
@@ -0,0 +1,31 @@
|
||||
using Test
|
||||
|
||||
include("chessboard.jl")
|
||||
|
||||
@testset verbose = true "tests" begin
|
||||
|
||||
@testset "rank_range is a range from 1 to 8" begin
|
||||
result = rank_range()
|
||||
@test first(result) == 1
|
||||
@test last(result) == 8
|
||||
@test length(result) == 8
|
||||
@test typeof(result) == UnitRange{Int}
|
||||
end
|
||||
|
||||
@testset "file_range is a range from 'A' to 'H'" begin
|
||||
result = file_range()
|
||||
@test first(result) == 'A'
|
||||
@test last(result) == 'H'
|
||||
@test length(result) == 8
|
||||
@test typeof(result) == StepRange{Char, Int}
|
||||
end
|
||||
|
||||
@testset "ranks is a vector of integers from 1 to 8" begin
|
||||
@test ranks() == [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
end
|
||||
|
||||
@testset "files is a vector of characters from 'A' to 'H'" begin
|
||||
@test ranks() == [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user