Skip to content

Commit

Permalink
Update ranges (#675) [no important files changed]
Browse files Browse the repository at this point in the history
* [no important files changed]
  • Loading branch information
meatball133 authored Aug 22, 2024
1 parent 01e1c04 commit 057da3d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 57 deletions.
44 changes: 22 additions & 22 deletions concepts/ranges/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

[Ranges][range] represent an interval between two values.
The most common types that support ranges are `Int`, `Char`, and `String`.
They can be used for many things like quickly creating a collection, slicing strings, checking if a value is in a range, and iteration.
They can be used for many things, such as quickly creating a collection, slicing strings, checking if a value is in a range, and iteration.
They are created using the range operator `..` or `...` (inclusive and exclusive, respectively).

```crystal
1..5 # A range containing 1..5
1..5  # A range containing 1..5
1...5 # A range containing 1...5
```

The reason for having two range operators is to allow to create ranges that are inclusive or exclusive of the end value, which can be useful when for example working with indexes that are zero based.
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.

Ranges can also be created using the `Range` initializer.

Expand Down Expand Up @@ -50,16 +50,16 @@ You can also use negative indexes to get the substring from the end of the strin
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 |
| Method                  | Description                                                             | Example                         |
| ----------------------- | ----------------------------------------------------------------------- | ------------------------------- |
| [`sum`][sum] | Returns the sum of all the values in the range | `(1..5).sum # => 15` |
| [`size`][size] | Returns the size of the range | `(1..5).size # => 5` |
| [`sum`][sum]            | Returns the sum of all the values in the range                          | `(1..5).sum # => 15` |
| [`size`][size]          | Returns the size of the range                                           | `(1..5).size # => 5` |
| [`includes?`][indludes] | Returns `true` if the range includes the given value, otherwise `false` | `(1..5).includes?(3) # => true` |

## Endless & Beginningless ranges

A range can be endless and beginless.
The endless or beginless range has their start or end value being `nil`, but when defining the range the `nil` can be omitted.
The endless or beginless range has its start or end value as `nil`, but `nil` can be omitted when defining the range.

Using beginless and endless ranges is useful when you want to, for example, slice a string from the beginning or to the end.

Expand All @@ -76,17 +76,17 @@ If not used on a collection, the endless range can cause an endless sequence, if
## Char ranges

Chars can be used in ranges and allow you to get an interval of chars between two chars.
This can be handy when you want to, for example, get the alphabet.
For example, this can be handy when you want to get the alphabet.

```crystal
'a'..'z' # A range containing ['a', 'b', 'c', ..., 'z']
```

## String ranges

Strings can also be used in ranges and allow one to get an interval of strings between two strings.
But its behavior is a bit different than with `Char` when using multiple characters in a string range.
Its behavior can become confusing when doing more complex string ranges, so use it with caution.
Strings can also be used in ranges, allowing one to get an interval of strings between two strings.
But its behavior is slightly different than that of `Char` when multiple characters are used in a string range.
Its behavior can become confusing when doing more complex string ranges, so use caution.

```crystal
("aa".."az") # A range containing ["aa", "ab", "ac", ..., "az"]
Expand All @@ -106,23 +106,23 @@ These methods make it so that the range can iterate over the object and compare
```crystal
class Foo
include Comparable(Foo)
  include Comparable(Foo)
getter value
  getter value
def initialize(@value : Int32)
end
  def initialize(@value : Int32)
  end
def succ
Foo.new(value + 1)
end
  def succ
    Foo.new(value + 1)
  end
def <=>(other : Foo)
value <=> other.value
end
  def <=>(other : Foo)
value <=> other.value
  end
end
p (Foo.new(1)..Foo.new(5))
Foo.new(1)..Foo.new(5)
# => #<Foo:0x7f3552bebe70 @value=1>, #<Foo:0x7f3552bebe50 @value=2>, #<Foo:0x7f3552bebe40 @value=3>, #<Foo:0x7f3552bebe30 @value=4>, #<Foo:0x7f3552bebe20 @value=5>
```
~~~~
Expand Down
22 changes: 11 additions & 11 deletions concepts/ranges/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

[Ranges][range] represent an interval between two values.
The most common types that support ranges are `Int`, `Char`, and `String`.
They can be used for many things like quickly creating a collection, slicing strings, checking if a value is in a range, and iteration.
They can be used for many things, such as quickly creating a collection, slicing strings, checking if a value is in a range, and iteration.
They are created using the range operator `..` or `...` (inclusive and exclusive, respectively).

```crystal
1..5 # A range containing 1..5
1..5  # A range containing 1..5
1...5 # A range containing 1...5
```

The reason for having two range operators is to allow to create ranges that are inclusive or exclusive of the end value, which can be useful when for example working with indexes that are zero based.
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.

Ranges can also be created using the `Range` initializer.

Expand Down Expand Up @@ -50,16 +50,16 @@ You can also use negative indexes to get the substring from the end of the strin
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 |
| Method                  | Description                                                             | Example                         |
| ----------------------- | ----------------------------------------------------------------------- | ------------------------------- |
| [`sum`][sum] | Returns the sum of all the values in the range | `(1..5).sum # => 15` |
| [`size`][size] | Returns the size of the range | `(1..5).size # => 5` |
| [`sum`][sum]            | Returns the sum of all the values in the range                          | `(1..5).sum # => 15` |
| [`size`][size]          | Returns the size of the range                                           | `(1..5).size # => 5` |
| [`includes?`][indludes] | Returns `true` if the range includes the given value, otherwise `false` | `(1..5).includes?(3) # => true` |

## Endless & Beginningless ranges

A range can be endless and beginless.
The endless or beginless range has their start or end value being `nil`, but when defining the range the `nil` can be omitted.
The endless or beginless range has its start or end value as `nil`, but `nil` can be omitted when defining the range.

Using beginless and endless ranges is useful when you want to, for example, slice a string from the beginning or to the end.

Expand All @@ -76,17 +76,17 @@ If not used on a collection, the endless range can cause an endless sequence, if
## Char ranges

Chars can be used in ranges and allow you to get an interval of chars between two chars.
This can be handy when you want to, for example, get the alphabet.
For example, this can be handy when you want to get the alphabet.

```crystal
'a'..'z' # A range containing ['a', 'b', 'c', ..., 'z']
```

## String ranges

Strings can also be used in ranges and allow one to get an interval of strings between two strings.
But its behavior is a bit different than with `Char` when using multiple characters in a string range.
Its behavior can become confusing when doing more complex string ranges, so use it with caution.
Strings can also be used in ranges, allowing one to get an interval of strings between two strings.
But its behavior is slightly different than that of `Char` when multiple characters are used in a string range.
Its behavior can become confusing when doing more complex string ranges, so use caution.

```crystal
("aa".."az") # A range containing ["aa", "ab", "ac", ..., "az"]
Expand Down
18 changes: 9 additions & 9 deletions exercises/concept/chess-game/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# 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!
As a chess enthusiast, you want to write your 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 basic movement system for the pieces.

The chess game will be played on a board that is 8 squares wide and 8 squares long.
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

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 of the board.
The files are the columns of the board and are identified by the letters A to H.
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 `Chess::RANKS` and `Chess::FILES` constants that store the range of ranks and files respectively.

Expand All @@ -41,12 +41,12 @@ Chess.valid_square?(1, 'A')

## 3. Get player's nickname

The game will have to get the nickname of the player.
The nickname is the first 2 characters of the player's first name and the last 2 characters of the player's last name.
The game will have to get the player's nickname.
The nickname is the first two characters of the player's first name and the last two characters of the player's last name.
The nickname should be capitalized.

Define the `Chess.nickname` method that takes the arguments `first_name` that holds a string of the player's first name and `last_name` that holds a string of the player's last name.
The method should return the nickname of the player as capitalized string.
The method should return the nickname of the player as an uppercased string.

```crystal
Chess.nickname("John", "Doe")
Expand All @@ -57,7 +57,7 @@ Chess.nickname("John", "Doe")

The game will have to create a message for a move to say which player moved to which square.
The message should use the player's nickname and the square they moved to.
The game also has to determine if the move is valid by checking if the file and rank of the square are within the ranges of the files and ranks.
The game must also determine if the move is valid by checking if the file and rank of the square are within the ranges of the files and ranks.

If the move is valid, the message should be: `"{nickname} moved to {square}"`
If the move is invalid, the message should be: `"{nickname} attempted to move to {square}, but that is not a valid square"`
Expand Down
22 changes: 11 additions & 11 deletions exercises/concept/chess-game/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

[Ranges][range] represent an interval between two values.
The most common types that support ranges are `Int`, `Char`, and `String`.
They can be used for many things like quickly creating a collection, slicing strings, checking if a value is in a range, and iteration.
They can be used for many things, such as quickly creating a collection, slicing strings, checking if a value is in a range, and iteration.
They are created using the range operator `..` or `...` (inclusive and exclusive, respectively).

```crystal
1..5 # A range containing 1..5
1..5  # A range containing 1..5
1...5 # A range containing 1...5
```

The reason for having two range operators is to allow to create ranges that are inclusive or exclusive of the end value, which can be useful when for example working with indexes that are zero based.
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.

Ranges can also be created using the `Range` initializer.

Expand Down Expand Up @@ -50,16 +50,16 @@ You can also use negative indexes to get the substring from the end of the strin
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 |
| Method                  | Description                                                             | Example                         |
| ----------------------- | ----------------------------------------------------------------------- | ------------------------------- |
| [`sum`][sum] | Returns the sum of all the values in the range | `(1..5).sum # => 15` |
| [`size`][size] | Returns the size of the range | `(1..5).size # => 5` |
| [`sum`][sum]            | Returns the sum of all the values in the range                          | `(1..5).sum # => 15` |
| [`size`][size]          | Returns the size of the range                                           | `(1..5).size # => 5` |
| [`includes?`][indludes] | Returns `true` if the range includes the given value, otherwise `false` | `(1..5).includes?(3) # => true` |

## Endless & Beginningless ranges

A range can be endless and beginless.
The endless or beginless range has their start or end value being `nil`, but when defining the range the `nil` can be omitted.
The endless or beginless range has its start or end value as `nil`, but `nil` can be omitted when defining the range.

Using beginless and endless ranges is useful when you want to, for example, slice a string from the beginning or to the end.

Expand All @@ -76,17 +76,17 @@ If not used on a collection, the endless range can cause an endless sequence, if
## Char ranges

Chars can be used in ranges and allow you to get an interval of chars between two chars.
This can be handy when you want to, for example, get the alphabet.
For example, this can be handy when you want to get the alphabet.

```crystal
'a'..'z' # A range containing ['a', 'b', 'c', ..., 'z']
```

## String ranges

Strings can also be used in ranges and allow one to get an interval of strings between two strings.
But its behavior is a bit different than with `Char` when using multiple characters in a string range.
Its behavior can become confusing when doing more complex string ranges, so use it with caution.
Strings can also be used in ranges, allowing one to get an interval of strings between two strings.
But its behavior is slightly different than that of `Char` when multiple characters are used in a string range.
Its behavior can become confusing when doing more complex string ranges, so use caution.

```crystal
("aa".."az") # A range containing ["aa", "ab", "ac", ..., "az"]
Expand Down
8 changes: 4 additions & 4 deletions exercises/concept/chess-game/spec/chess_game_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "spec"
require "../src/*"

describe Chess do
describe "Files & Ranks" do
describe "Files & Ranks", tags: "task_id=1" do
it "should have 8 files" do
Chess::FILES.should eq 'A'..'H'
end
Expand All @@ -12,7 +12,7 @@ describe Chess do
end
end

describe "valid_square?" do
describe "valid_square?", tags: "task_id=2" do
it "should return true when given a valid square" do
Chess.valid_square?(1, 'A').should be_true
end
Expand All @@ -34,7 +34,7 @@ describe Chess do
end
end

describe "nickname" do
describe "nickname", tags: "task_id=3" do
it "Should return correct player nickname" do
Chess.nickname("John", "Doe").should eq "JOOE"
end
Expand All @@ -48,7 +48,7 @@ describe Chess do
end
end

describe "move_message" do
describe "move_message", tags: "task_id=4" do
it "should return correct message for a move" do
Chess.move_message("John", "Doe", "A2").should eq "JOOE moved to A2"
end
Expand Down

0 comments on commit 057da3d

Please sign in to comment.