Skip to content

Commit 83a3cc9

Browse files
colinleachColin Leachdepial
authored
Chessboard (exercism#835)
* Chessboard concept exercise * moved solution to exemplar.jl * Update chessboard.jl Added new line to function skeletons * Update introduction.md Syncing with concept introduction.md * Update instructions.md Good catch! That was sloppy copy-paste from another language track. * Update design.md Nothing comes to mind at this stage, so I've removed that subsection. We may want to add it back later, when we get a better view of the advanced topics. Luckily, the rules are non-prescriptive about the contends of this file. It's just a note to guide future maintainers. * Update config.json --------- Co-authored-by: Colin Leach <[email protected]> Co-authored-by: depial <[email protected]>
1 parent 6fef8a4 commit 83a3cc9

File tree

9 files changed

+260
-0
lines changed

9 files changed

+260
-0
lines changed

config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@
5656
],
5757
"status": "wip"
5858
},
59+
{
60+
"slug": "chessboard",
61+
"name": "chessboard",
62+
"uuid": "48977bc8-36e7-41be-8fd5-99c49de00f20",
63+
"concepts": [
64+
"ranges"
65+
],
66+
"prerequisites": [
67+
"vectors"
68+
],
69+
"status": "wip"
70+
},
5971
{
6072
"slug": "currency-exchange",
6173
"name": "currency-exchange",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Hints
2+
3+
This is a short and simple exercise, so it is best to avoid complicating it.
4+
5+
## 1. Define the rank range
6+
7+
- You need to return a range of integers
8+
9+
## 2. Define the file range
10+
11+
- You need to return a range of uppercase characters.
12+
- Characters use single-quotes, such as `'W'`,
13+
14+
## 3. Transform the rank range into a list of ranks
15+
16+
- The [`collect()`][collect] function is your friend here.
17+
18+
## 4. Transform the file range into a list of files
19+
20+
- See task 3.
21+
22+
[collect]: https://docs.julialang.org/en/v1/base/collections/#Base.collect-Tuple{Any}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Instructions
2+
3+
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!
4+
5+
But before you can let your imagination run wild, you need to take care of the basics. Let's start by generating the board.
6+
7+
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.
8+
9+
## 1. Define the rank range
10+
11+
Implement the `rank_range()` function. It should return a range of integers, from 1 to 8.
12+
13+
```julia-repl
14+
julia> rank_range()
15+
# output omitted
16+
```
17+
18+
## 2. Define the file range
19+
20+
Implement the `file_range()` function.
21+
It should return a range of integers, from the uppercase letter A, to the uppercase letter H.
22+
23+
```julia-repl
24+
julia> file_range()
25+
# output omitted
26+
```
27+
28+
## 3. Transform the rank range into a vector of ranks
29+
30+
Implement the `ranks()` function. It should return a vector of integers, from 1 to 8.
31+
Do not write the vector by hand, generate it from the range returned by the `rank_range()` function.
32+
33+
```julia-repl
34+
julia> ranks()
35+
[1, 2, 3, 4, 5, 6, 7, 8]
36+
```
37+
38+
## 4. Transform the file range into a vector of files
39+
40+
Implement the `files` function. It should return a vector of characters, from 'A' to 'H'.
41+
Do not write the vector by hand, generate it from the range returned by the `file_range()` function.
42+
43+
```julia-repl
44+
julia> files()
45+
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
46+
```
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Introduction
2+
3+
Suppose you want all the non-negative integers up to 1000.
4+
It would be ridiculous if you had to type these into an array.
5+
6+
For this we have the `range` type:
7+
8+
```julia-repl
9+
julia> 0:1000
10+
0:1000
11+
12+
julia> typeof(0:1000)
13+
UnitRange{Int64}
14+
```
15+
16+
Ranges are very common: not just to save you typing, but also as return types from functions.
17+
18+
Note that ranges are _not_ vectors.
19+
They are just a set of instructions to generate a sequence ("lazy" evaluation, or an "iterator").
20+
21+
If you need a range as a vector, use the `collect()` function for conversion:
22+
23+
```julia-repl
24+
julia> collect(0:5)
25+
6-element Vector{Int64}:
26+
0
27+
1
28+
2
29+
3
30+
4
31+
5
32+
```
33+
34+
The step size can be specified, in this case 0.3:
35+
36+
```julia-repl
37+
julia> collect(1.0:0.3:2.0)
38+
4-element Vector{Float64}:
39+
1.0
40+
1.3
41+
1.6
42+
1.9
43+
```
44+
45+
So the syntax is `start:stepsize:stop`.
46+
Both end limits are _inclusive_, as seen in the integer example.
47+
If the step size does not divide exactly into `stop - start`, the last element will avoid exceeding `stop`.
48+
49+
## Letter ranges
50+
51+
Non-numeric sequences can also be used in ranges.
52+
The simplest example is ASCII letters:
53+
54+
```julia-repl
55+
julia> 'a':'d'
56+
'a':1:'d'
57+
58+
julia> typeof('a':'d')
59+
StepRange{Char, Int64}
60+
61+
julia> collect('a':'d')
62+
4-element Vector{Char}:
63+
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
64+
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
65+
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
66+
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
67+
```
68+
69+
The `Char` type will be covered in more detail in another Concept.
70+
For now, just treat these as single characters in single-quotes.
71+
72+
## Functions and operators for ranges
73+
74+
Check the limits of a range with `first()` and `last()`.
75+
76+
```julia
77+
r = 1:10 # => 1:10
78+
first(r) # => 1
79+
last(r) # => 10
80+
```
81+
82+
## More on vector indexing
83+
84+
Integer ranges and vectors can be used in vector indexing:
85+
86+
```julia
87+
nums = collect(10.0:50.0)
88+
nums[3:2:7] # gives [12.0, 14.0, 16.0]
89+
nums[ [3, 5, 7] ] # also gives [12.0, 14.0, 16.0]
90+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": [
3+
"colinleach"
4+
],
5+
"files": {
6+
"solution": [
7+
"chessboard.jl"
8+
],
9+
"test": [
10+
"runtests.jl"
11+
],
12+
"exemplar": [
13+
".meta/exemplar.jl"
14+
]
15+
},
16+
"forked_from": [
17+
"elixir/chessboard"
18+
],
19+
"blurb": "Learn about ranges by building a chessboard."
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Design
2+
3+
## Learning objectives
4+
5+
- Know about ranges.
6+
- They are structs.
7+
- They are enumerable.
8+
- How to transform them to a list.
9+
- How to generate a list of letters from a range.
10+
11+
## Prerequisites
12+
13+
- `vectors`
14+
15+
## Concepts
16+
17+
- `ranges`
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
rank_range() = 1:8
2+
3+
file_range() = 'A':'H'
4+
5+
ranks() = collect(rank_range())
6+
7+
files() = collect(file_range())
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function rank_range()
2+
3+
end
4+
5+
function file_range()
6+
7+
end
8+
9+
function ranks()
10+
11+
end
12+
13+
function files()
14+
15+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Test
2+
3+
include("chessboard.jl")
4+
5+
@testset verbose = true "tests" begin
6+
7+
@testset "rank_range is a range from 1 to 8" begin
8+
result = rank_range()
9+
@test first(result) == 1
10+
@test last(result) == 8
11+
@test length(result) == 8
12+
@test typeof(result) == UnitRange{Int}
13+
end
14+
15+
@testset "file_range is a range from 'A' to 'H'" begin
16+
result = file_range()
17+
@test first(result) == 'A'
18+
@test last(result) == 'H'
19+
@test length(result) == 8
20+
@test typeof(result) == StepRange{Char, Int}
21+
end
22+
23+
@testset "ranks is a vector of integers from 1 to 8" begin
24+
@test ranks() == [1, 2, 3, 4, 5, 6, 7, 8]
25+
end
26+
27+
@testset "files is a vector of characters from 'A' to 'H'" begin
28+
@test ranks() == [1, 2, 3, 4, 5, 6, 7, 8]
29+
end
30+
31+
end

0 commit comments

Comments
 (0)