-
-
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
Open
meatball133
wants to merge
14
commits into
exercism:main
Choose a base branch
from
meatball133:add-ranges-concept
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6908429
Add range concept and exercise
meatball133 65daba2
Fix name of exercise
meatball133 9d6856f
Fix name again
meatball133 54c08e9
Fix name in test file
meatball133 92891b8
Add hints file
meatball133 7c339f0
format config file
meatball133 62d8ab1
Update concepts/ranges/about.md
meatball133 5f3d8c6
Update concepts/ranges/about.md
meatball133 a71fc6b
Update concepts/ranges/about.md
meatball133 321d529
Update exercises/concept/chessboard/.docs/instructions.md
meatball133 f6c41e3
Update exercises/concept/chessboard/.docs/instructions.md
meatball133 c65ae8a
Update exercises/concept/chessboard/.docs/instructions.md
meatball133 2609d3a
Make columns line up
meatball133 ffed7f4
Various fixes based on feedback
meatball133 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"blurb": "Ranges can be used to represent a sequence of values.", | ||
"authors": [ | ||
"meatball133" | ||
], | ||
"contributors": [ | ||
"IsaacG" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# 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] | ||
// Returns [1, 2, 3, 4, 5] | ||
let slice = array[1...3] | ||
// Returns [2, 3, 4] | ||
``` | ||
|
||
## Range methods | ||
|
||
Ranges 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 and beginless ranges | ||
|
||
A range can be endless and beginless. | ||
|
||
Using endless and beginless 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(_:) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# 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] | ||
// Returns [1, 2, 3, 4, 5] | ||
let slice = array[1...3] | ||
// Returns [2, 3, 4] | ||
``` | ||
|
||
## Range methods | ||
|
||
Ranges 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 and beginless ranges | ||
|
||
A range can be endless and beginless. | ||
|
||
Using endless and beginless 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(_:) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 strings 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,55 @@ | ||||||
# Instructions | ||||||
|
||||||
As a chess enthusiast, you want to write your own version of the game. | ||||||
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 | ||||||
|
||||||
files | ||||||
// Returns "A".."H" | ||||||
``` | ||||||
|
||||||
## 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 `String` of the file. | ||||||
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 | ||||||
``` | ||||||
|
||||||
## 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. | ||||||
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"] | ||||||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# 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] | ||
// Returns [1, 2, 3, 4, 5] | ||
let slice = array[1...3] | ||
// Returns [2, 3, 4] | ||
``` | ||
|
||
## Range methods | ||
|
||
Ranges 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 and beginless ranges | ||
|
||
A range can be endless and beginless. | ||
|
||
Using endless and beginless 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(_:) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ |
12 changes: 12 additions & 0 deletions
12
exercises/concept/chessboard/.meta/Sources/Chessboard/ChessboardExemplar.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this file supposed to match the about file?
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.
Yes forgot to sync