Skip to content

[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
wants to merge 14 commits into
base: main
Choose a base branch
from
9 changes: 9 additions & 0 deletions concepts/ranges/.meta/config.json
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"
]
}
77 changes: 77 additions & 0 deletions concepts/ranges/about.md
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(_:)
77 changes: 77 additions & 0 deletions concepts/ranges/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Ranges
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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]
```

## 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(_:)
6 changes: 6 additions & 0 deletions concepts/ranges/links.json
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"
}
]
17 changes: 17 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@
],
"status": "active"
},
{
"slug": "chessboard",
"name": "ChessBoard",
"uuid": "162bf709-ed48-438d-9a32-f572099cf42e",
"concepts": [
"ranges"
],
"prerequisites": [
"arrays"
],
"status": "active"
},
{
"slug": "santas-helper",
"name": "Santa's Swifty Helper",
Expand Down Expand Up @@ -1503,6 +1515,11 @@
"slug": "repeat-while",
"name": "repeat while"
},
{
"uuid": "cb0d8166-672c-4bdf-820b-d60531574d73",
"slug": "ranges",
"name": "Ranges"
},
{
"uuid": "a4b7ebd1-28f4-4270-910f-cf511ec4b45a",
"slug": "self",
Expand Down
18 changes: 18 additions & 0 deletions exercises/concept/chessboard/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Hints

## 1. Define rank & file range
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## 1. Define rank & file range
## 1. Define rank and file range


- 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
55 changes: 55 additions & 0 deletions exercises/concept/chessboard/.docs/instructions.md
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## 1. Define rank & file range
## 1. Define rank and file range


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"]
```
77 changes: 77 additions & 0 deletions exercises/concept/chessboard/.docs/introduction.md
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(_:)
5 changes: 5 additions & 0 deletions exercises/concept/chessboard/.gitignore
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])
}
18 changes: 18 additions & 0 deletions exercises/concept/chessboard/.meta/config.json
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."
}
Loading