-
-
Notifications
You must be signed in to change notification settings - Fork 159
[Swift 6]: Add ranges concept #823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
6908429
65daba2
9d6856f
54c08e9
92891b8
7c339f0
62d8ab1
5f3d8c6
a71fc6b
321d529
f6c41e3
c65ae8a
2609d3a
ffed7f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"blurb": "Optionals can be used to represent the possible absence of a value.", | ||
"authors": [ | ||
"meatball133" | ||
], | ||
"contributors": [] | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,74 @@ | ||||||
# Ranges | ||||||
|
||||||
[Ranges][range] represent an interval between two values. | ||||||
The most common types that support ranges are `Int`, `String`, and `Date`. | ||||||
They can be used for many things, such as quickly creating a collection, slicing an array, checking if a value is in a range, and iteration. | ||||||
They are created using the range operator `...` or `..<` (inclusive and exclusive, respectively). | ||||||
|
||||||
```swift | ||||||
1...5 // A range containing 1, 2, 3, 4, 5 | ||||||
1..<5 // A range containing 1, 2, 3, 4 | ||||||
``` | ||||||
|
||||||
The reason for having two range operators is to create ranges that are inclusive or exclusive of the end value, which can be useful when, for example, working with zero-based indexes. | ||||||
|
||||||
~~~~exercism/note | ||||||
When creating a range in Swift using the range operators `...` or `..<`, and wanting to call a method on the range, you need to wrap the range in parentheses. | ||||||
This is because the otherwise will the method be called on the 2nd argument of the range operator. | ||||||
|
||||||
```swift | ||||||
(1...5).contains(3) // Returns true | ||||||
1...5.contains(3) // => Error: value of type 'Int' has no member 'contains' | ||||||
``` | ||||||
~~~~ | ||||||
|
||||||
## Convert a range to an array | ||||||
|
||||||
To convert a range to an array, you can use the `Array` initializer. | ||||||
This can be useful when you want to create a collection of values, without having to write them out. | ||||||
|
||||||
```swift | ||||||
let range = 1...5 | ||||||
let array = Array(range) // Returns [1, 2, 3, 4, 5] | ||||||
``` | ||||||
|
||||||
## Slice an array | ||||||
|
||||||
Ranges can be used to slice an array. | ||||||
|
||||||
```swift | ||||||
let array = [1, 2, 3, 4, 5] | ||||||
let slice = array[1...3] // Returns [2, 3, 4] | ||||||
meatball133 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
``` | ||||||
|
||||||
## Range methods | ||||||
|
||||||
Ranges do have a set of methods that can be used to work with them. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
For example, these methods can be used to get the sum of all the values in the range or check if the range includes a value. | ||||||
|
||||||
| Method | Description | Example | | ||||||
| ----------------------- | ----------------------------------------------------------------------- | ------------------------------------- | | ||||||
| `count` | Returns the size of the range | `(1...5).count // returns 5` | | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the columns line up? |
||||||
| [`contains`][contains] | Returns `true` if the range includes the given value, otherwise `false` | `(1...5).contains(3) // Returns true` | | ||||||
|
||||||
## Endless & Beginningless ranges | ||||||
meatball133 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
A range can be endless and beginless. | ||||||
|
||||||
Using beginless and endless ranges is useful when you want to, for example, slice an array from the beginning or to the end. | ||||||
meatball133 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
~~~~exercism/caution | ||||||
If not used on a collection, the endless range can cause an endless sequence, if not used with caution. | ||||||
~~~~ | ||||||
|
||||||
## String ranges | ||||||
|
||||||
String can be used in ranges and allow you to get an interval of Strings between two Strings. | ||||||
For example, this can be handy when you want to get the alphabet. | ||||||
|
||||||
```swift | ||||||
"a"..."z" // A range containing ["a", "b", "c", ..., "z"] | ||||||
``` | ||||||
|
||||||
[range]: https://developer.apple.com/documentation/swift/range | ||||||
[contains]: https://developer.apple.com/documentation/swift/range/contains(_:) |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,74 @@ | ||||||||
# Ranges | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this file supposed to match the about file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes forgot to sync |
||||||||
|
||||||||
[Ranges][range] represent an interval between two values. | ||||||||
The most common types that support ranges are `Int`, `String`, and `Date`. | ||||||||
They can be used for many things, such as quickly creating a collection, slicing an array, checking if a value is in a range, and iteration. | ||||||||
They are created using the range operator `...` or `..<` (inclusive and exclusive, respectively). | ||||||||
|
||||||||
```swift | ||||||||
1...5 // A range containing 1, 2, 3, 4, 5 | ||||||||
1..<5 // A range containing 1, 2, 3, 4 | ||||||||
``` | ||||||||
|
||||||||
The reason for having two range operators is to create ranges that are inclusive or exclusive of the end value, which can be useful when, for example, working with zero-based indexes. | ||||||||
|
||||||||
~~~~exercism/note | ||||||||
When creating a range in Swift using the range operators `...` or `..<`, and wanting to call a method on the range, you need to wrap the range in parentheses. | ||||||||
This is because the otherwise will the method be called on the 2nd argument of the range operator. | ||||||||
|
||||||||
```swift | ||||||||
(1...5).contains(3) // Returns true | ||||||||
1...5.contains(3) // => Error: value of type 'Int' has no member 'contains' | ||||||||
``` | ||||||||
~~~~ | ||||||||
|
||||||||
## Convert a range to an array | ||||||||
|
||||||||
To convert a range to an array, you can use the `Array` initializer. | ||||||||
This can be useful when you want to create a collection of values, without having to write them out. | ||||||||
|
||||||||
```swift | ||||||||
let range = 1...5 | ||||||||
let array = Array(range) // Returns [1, 2, 3, 4, 5] | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
``` | ||||||||
|
||||||||
## Slice an array | ||||||||
|
||||||||
Ranges can be used to slice an array. | ||||||||
|
||||||||
```swift | ||||||||
let array = [1, 2, 3, 4, 5] | ||||||||
let slice = array[1...3] // Returns [2, 3, 4] | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
``` | ||||||||
|
||||||||
## Range methods | ||||||||
|
||||||||
Ranges do have a set of methods that can be used to work with them. | ||||||||
For example, these methods can be used to get the sum of all the values in the range or check if the range includes a value. | ||||||||
|
||||||||
| Method | Description | Example | | ||||||||
| ----------------------- | ----------------------------------------------------------------------- | ------------------------------------- | | ||||||||
| `count` | Returns the size of the range | `(1...5).count // returns 5` | | ||||||||
| [`contains`][contains] | Returns `true` if the range includes the given value, otherwise `false` | `(1...5).contains(3) // Returns true` | | ||||||||
|
||||||||
## Endless & Beginningless ranges | ||||||||
|
||||||||
A range can be endless and beginless. | ||||||||
|
||||||||
Using beginless and endless ranges is useful when you want to, for example, slice an array from the beginning or to the end. | ||||||||
|
||||||||
~~~~exercism/caution | ||||||||
If not used on a collection, the endless range can cause an endless sequence, if not used with caution. | ||||||||
~~~~ | ||||||||
|
||||||||
## String ranges | ||||||||
|
||||||||
String can be used in ranges and allow you to get an interval of Strings between two Strings. | ||||||||
For example, this can be handy when you want to get the alphabet. | ||||||||
|
||||||||
```swift | ||||||||
"a"..."z" // A range containing ["a", "b", "c", ..., "z"] | ||||||||
``` | ||||||||
|
||||||||
[range]: https://developer.apple.com/documentation/swift/range | ||||||||
[contains]: https://developer.apple.com/documentation/swift/range/contains(_:) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ | ||
"url": "https://developer.apple.com/documentation/swift/range", | ||
"description": "Swift docs: Range" | ||
} | ||
] |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||||
# Hints | ||||||
|
||||||
## 1. Define rank & file range | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
- There is an [operator][swift-range-docs] for creating a range, which can be used for both characters and integers. | ||||||
|
||||||
## 2. Check if square is valid | ||||||
|
||||||
- There is an [method][contains-docs] for checking if a value is inside of a range. | ||||||
|
||||||
## 3. Get row | ||||||
|
||||||
- Each row should have the length of 8. | ||||||
- For every row, starting from 1, will you have to offset the start and end by 8. | ||||||
- You can use a range to get a subarray of the array. | ||||||
|
||||||
[swift-range-docs]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators/#Range-Operators | ||||||
[contains-docs]: https://developer.apple.com/documentation/swift/range/contains(_:)-76nb4 |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,55 @@ | ||||||
# Instructions | ||||||
|
||||||
As a chess enthusiast, you want to write your version of the game. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Yes, there may be plenty of implementations of chess available online already, but yours will be unique! | ||||||
|
||||||
You start with implementing a chess board. | ||||||
|
||||||
The chess game will be played on an eight-square wide and eight-square long board. | ||||||
The squares are identified by a letter and a number. | ||||||
|
||||||
## 1. Define rank & file range | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
The game will have to store the ranks of the board. | ||||||
The ranks are the rows of the board, and are numbered from 1 to 8. | ||||||
|
||||||
The game will also have to store the files on the board. | ||||||
The files are the board's columns and are identified by the letters A to H. | ||||||
|
||||||
Define the `ranks` and `files` constants that store the range of ranks and files respectively. | ||||||
|
||||||
```swift | ||||||
ranks | ||||||
// returns 1..8 | ||||||
meatball133 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
files | ||||||
// returns 'A'..'H' | ||||||
meatball133 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
``` | ||||||
|
||||||
## 2. Check if square is valid | ||||||
|
||||||
The game will have to check if a square is valid. | ||||||
A square is valid if the rank and file are within the ranges of the ranks and files. | ||||||
|
||||||
Define the `isValidSquare(rank:file:)` method that takes the arguments `rank` that holds an int of the rank and `file` that holds a char of the file. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String, I relized I wrote the exercise but while implementing I changed it without updating the docs |
||||||
The method should return `true` if the rank and file are within the range of ranks and files, and return `false` otherwise. | ||||||
|
||||||
```swift | ||||||
isValidSquare(rank: 1, file: "A") | ||||||
// returns true | ||||||
meatball133 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
``` | ||||||
|
||||||
## 3. Get row | ||||||
|
||||||
The game will store all the squares of the board in a single dimensional array. | ||||||
The squares are formed as a string of the rank and file, e.g. "1A", "8B", "4G", etc. | ||||||
To get the row of a square, the game will have to calculate the index of the first square of the row. | ||||||
|
||||||
Define the `getRow(rank:)` method that takes the argument `rank` that holds an int of the rank. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
The method should return an array of strings that represent the squares of the row. | ||||||
|
||||||
```swift | ||||||
let board = ["1A", "1B", "1C", "1D", "1E", "1F", "1G", "1H", "2A", ..., "8H"] | ||||||
getRow(board, rank: 1) | ||||||
// returns ["1A", "1B", "1C", "1D", "1E", "1F", "1G", "1H"] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Ranges | ||
|
||
[Ranges][range] represent an interval between two values. | ||
The most common types that support ranges are `Int`, `String`, and `Date`. | ||
They can be used for many things, such as quickly creating a collection, slicing an array, checking if a value is in a range, and iteration. | ||
They are created using the range operator `...` or `..<` (inclusive and exclusive, respectively). | ||
|
||
```swift | ||
1...5 // A range containing 1, 2, 3, 4, 5 | ||
1..<5 // A range containing 1, 2, 3, 4 | ||
``` | ||
|
||
The reason for having two range operators is to create ranges that are inclusive or exclusive of the end value, which can be useful when, for example, working with zero-based indexes. | ||
|
||
~~~~exercism/note | ||
When creating a range in Swift using the range operators `...` or `..<`, and wanting to call a method on the range, you need to wrap the range in parentheses. | ||
This is because the otherwise will the method be called on the 2nd argument of the range operator. | ||
|
||
```swift | ||
(1...5).contains(3) // Returns true | ||
1...5.contains(3) // => Error: value of type 'Int' has no member 'contains' | ||
``` | ||
~~~~ | ||
|
||
## Convert a range to an array | ||
|
||
To convert a range to an array, you can use the `Array` initializer. | ||
This can be useful when you want to create a collection of values, without having to write them out. | ||
|
||
```swift | ||
let range = 1...5 | ||
let array = Array(range) // Returns [1, 2, 3, 4, 5] | ||
``` | ||
|
||
## Slice an array | ||
|
||
Ranges can be used to slice an array. | ||
|
||
```swift | ||
let array = [1, 2, 3, 4, 5] | ||
let slice = array[1...3] // Returns [2, 3, 4] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't match the above intro doc |
||
``` | ||
|
||
## Range methods | ||
|
||
Ranges do have a set of methods that can be used to work with them. | ||
For example, these methods can be used to get the sum of all the values in the range or check if the range includes a value. | ||
|
||
| Method | Description | Example | | ||
| ----------------------- | ----------------------------------------------------------------------- | ------------------------------------- | | ||
| `count` | Returns the size of the range | `(1...5).count // returns 5` | | ||
| [`contains`][contains] | Returns `true` if the range includes the given value, otherwise `false` | `(1...5).contains(3) // Returns true` | | ||
|
||
## Endless & Beginningless ranges | ||
|
||
A range can be endless and beginless. | ||
|
||
Using beginless and endless ranges is useful when you want to, for example, slice an array from the beginning or to the end. | ||
|
||
~~~~exercism/caution | ||
If not used on a collection, the endless range can cause an endless sequence, if not used with caution. | ||
~~~~ | ||
|
||
## String ranges | ||
|
||
String can be used in ranges and allow you to get an interval of Strings between two Strings. | ||
For example, this can be handy when you want to get the alphabet. | ||
|
||
```swift | ||
"a"..."z" // A range containing ["a", "b", "c", ..., "z"] | ||
``` | ||
|
||
[range]: https://developer.apple.com/documentation/swift/range | ||
[contains]: https://developer.apple.com/documentation/swift/range/contains(_:) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
let ranks = 1...8 | ||
let files = "A"..."H" | ||
|
||
func isVaildSquare(rank: Int, file: String) -> Bool { | ||
return ranks.contains(rank) && files.contains(file) | ||
} | ||
|
||
func getRow(_ board : [String], rank: Int) -> [String] { | ||
let startIndex = (rank - 1) * 8 | ||
let endIndex = startIndex + 8 | ||
return Array(board[startIndex..<endIndex]) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"authors": [ | ||
"meatball133" | ||
], | ||
"files": { | ||
"solution": [ | ||
"Sources/Chessboard/Chessboard.swift" | ||
], | ||
"test": [ | ||
"Tests/ChessboardTests/ChessboardTests.swift" | ||
], | ||
"exemplar": [ | ||
".meta/Sources/Chessboard/ChessboardExemplar.swift" | ||
] | ||
}, | ||
"icon": "chessboard", | ||
"blurb": "Learn about ranges while making a chess board." | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look like a
ranges
blurb.