-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (concepts) Add concept exercise
annalyns-infiltration
This concept is inspired from the same in csharp track
- Loading branch information
Showing
13 changed files
with
784 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,22 +1,55 @@ | ||
# Introduction | ||
|
||
Booleans in Rust are represented by the `bool` type, which values can be either `true` or `false`. | ||
## Booleans | ||
|
||
Rust supports six comparison operators: `==` (Equal), `!=` (Not equal), `>` (Greater than), `<` (Less than), `>=` | ||
(Greater than or equal to), `<=` (Less than or equal to). They can be used to evaluate the relationship of non-boolean | ||
values into a boolean value. | ||
As in most other programming languages, a Boolean type in Rust has two possible values: `true` and `false`. | ||
|
||
### Declaration | ||
The Boolean type in Rust is specified using `bool`. | ||
|
||
```rust | ||
let t = true; | ||
|
||
let f: bool = false; // with explicit type annotation | ||
``` | ||
|
||
The main way to use Boolean values is through conditionals, such as an if | ||
expression. | ||
We’ll cover how if expressions work in Rust in later concepts. | ||
|
||
### Use as a type | ||
Booleans can be used as a type for a variable, function parameter or return | ||
type. | ||
|
||
```rust | ||
1 > 0 // true | ||
1 < 0 // false | ||
1 == 0 // false | ||
1 != 0 // true | ||
1 >= 0 // true | ||
1 <= 0 // false | ||
let my_variable: bool; | ||
|
||
fn my_function(input: bool) -> bool { | ||
// Do something | ||
} | ||
|
||
``` | ||
|
||
Unlike some other languages, `0` is not `false` and non-zero is not `true`. | ||
### Basic Operators | ||
Rust supports following operators on `bool`: | ||
- AND (`&&`) operator computes the logical AND of its operands. | ||
The result of `x && y` is `true` if both `x` and `y` evaluate to `true`. | ||
Otherwise, the result is `false`. | ||
- OR (`||`) operator computes the logical OR of its operands. | ||
The result of `x || y` is `true` if either `x` or `y` evaluates to `true`. Otherwise, the result is `false`. | ||
- NOT (`!`) operator computes logical negation of its operand. | ||
That is, it produces `true`, if the operand evaluates to `false`, and `false`, if the operand evaluates to `true`. | ||
|
||
The following example demonstrates the effect of each operator on bool values: | ||
|
||
```rust | ||
let x = true; | ||
let y = false; | ||
|
||
!x // => false | ||
|
||
x && y // => false | ||
|
||
Rust supports two boolean operators: `||` (Or), `&&` (And). They are only evaluated when the left-hand operand does not already | ||
determine the result of the expression. That is, `||` only evaluates its right-hand operand when the left-hand operand evaluates | ||
to `false`, and `&&` only when it evaluates to `true`. | ||
x || y // => true | ||
|
||
``` |
This file contains 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 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,13 @@ | ||
# Hints | ||
|
||
## General | ||
|
||
- There are three boolean operators<sup>[1] [2]</sup> to work with boolean values. | ||
- Multiple operators can be combined in a single expression. | ||
|
||
## 2. Check if a fast attack can be made | ||
|
||
- The boolean operators<sup>[1] [2]</sup> can also be applied to boolean parameters. | ||
|
||
[1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#lazy-boolean-operators | ||
[2]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#negation-operators |
69 changes: 69 additions & 0 deletions
69
exercises/concept/annalyns-infiltration/.docs/instructions.md
This file contains 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,69 @@ | ||
# Instructions | ||
|
||
In this exercise, you'll be implementing the quest logic for a new RPG game a friend is developing. | ||
|
||
The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog. Unfortunately, disaster strikes, as her best friend was kidnapped while searching for berries in the forest. | ||
|
||
Annalyn will try to find and free her best friend, optionally taking her dog with her on this quest. | ||
|
||
After some time spent following her best friend's trail, she finds the camp in which her best friend is imprisoned. It turns out there are two kidnappers: a mighty knight and a cunning archer. | ||
|
||
Having found the kidnappers, Annalyn considers which of the following actions she can engage in: | ||
|
||
- _Fast attack_: a fast attack can be made if the knight is sleeping, as it takes time for him to get his armor on, so he will be vulnerable. | ||
- _Spy_: the group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time. | ||
- _Signal prisoner_: the prisoner can be signalled using bird sounds if the prisoner is awake and the archer is sleeping, as archers are trained in bird signaling so they could intercept the message. | ||
- _Free prisoner_: Annalyn can try sneaking into the camp to free the prisoner. | ||
This is a risky thing to do and can only succeed in one of two ways: | ||
- If Annalyn has her pet dog with her she can rescue the prisoner if the archer is asleep. | ||
The knight is scared of the dog and the archer will not have time to get ready before Annalyn and the prisoner can escape. | ||
- If Annalyn does not have her dog then she and the prisoner must be very sneaky! | ||
Annalyn can free the prisoner if the prisoner is awake and the knight and archer are both sleeping, but if the prisoner is sleeping they can't be rescued: the prisoner would be startled by Annalyn's sudden appearance and wake up the knight and archer. | ||
|
||
You have four tasks: to implement the logic for determining if the above actions are available based on the state of the three characters found in the forest and whether Annalyn's pet dog is present or not. | ||
|
||
## 1. Check if a fast attack can be made | ||
|
||
Implement the `can_fast_attack()` function that takes a boolean value that indicates if the knight is awake. This function returns `true` if a fast attack can be made based on the state of the knight. Otherwise, returns `false`: | ||
|
||
```rust | ||
let knight_is_awake = true; | ||
can_fast_attack(knight_is_awake); | ||
// => false | ||
``` | ||
|
||
## 2. Check if the group can be spied upon | ||
|
||
Implement the `can_spy()` function that takes three boolean values, indicating if the knight, archer and the prisoner, respectively, are awake. The function returns `true` if the group can be spied upon, based on the state of the three characters. Otherwise, returns `false`: | ||
|
||
```rust | ||
let knight_is_awake = false; | ||
let archer_is_awake = true; | ||
let prisoner_is_awake = false; | ||
can_spy(knight_is_awake, archer_is_awake, prisoner_is_awake); | ||
// => true | ||
``` | ||
|
||
## 3. Check if the prisoner can be signalled | ||
|
||
Implement the `can_signal_prisoner()` function that takes two boolean values, indicating if the archer and the prisoner, respectively, are awake. The function returns `true` if the prisoner can be signalled, based on the state of the two characters. Otherwise, returns `false`: | ||
|
||
```rust | ||
let archer_is_awake = false; | ||
let prisoner_is_awake = true; | ||
can_signal_prisoner(archer_is_awake, prisoner_is_awake); | ||
// => true | ||
``` | ||
|
||
## 4. Check if the prisoner can be freed | ||
|
||
Implement the `can_free_prisoner()` function that takes four boolean values. The first three parameters indicate if the knight, archer and the prisoner, respectively, are awake. The last parameter indicates if Annalyn's pet dog is present. The function returns `true` if the prisoner can be freed based on the state of the three characters and Annalyn's pet dog presence. Otherwise, it returns `false`: | ||
|
||
```rust | ||
let knight_is_awake = false; | ||
let archer_is_awake = true; | ||
let prisoner_is_awake = false; | ||
let pet_dog_is_present = false; | ||
can_free_prisoner(knight_is_awake, archer_is_awake, prisoner_is_awake, pet_dog_is_present); | ||
// => false | ||
``` |
56 changes: 56 additions & 0 deletions
56
exercises/concept/annalyns-infiltration/.docs/introduction.md
This file contains 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,56 @@ | ||
# Introduction | ||
|
||
## Booleans | ||
|
||
As in most other programming languages, a Boolean type in Rust has two possible values: `true` and `false`. | ||
|
||
### Declaration | ||
The Boolean type in Rust is specified using `bool`. | ||
|
||
```rust | ||
let t = true; | ||
|
||
let f: bool = false; // with explicit type annotation | ||
``` | ||
|
||
The main way to use Boolean values is through conditionals, such as an if | ||
expression. | ||
We’ll cover how if expressions work in Rust in later concepts. | ||
|
||
### Use as a type | ||
Booleans can be used as a type for a variable, function parameter or return | ||
type. | ||
|
||
```rust | ||
let my_variable: bool; | ||
|
||
fn my_function(input: bool) -> bool { | ||
// Do something | ||
} | ||
|
||
``` | ||
|
||
### Basic Operators | ||
Rust supports following operators on `bool`: | ||
- AND (`&&`) operator computes the logical AND of its operands. | ||
The result of `x && y` is `true` if both `x` and `y` evaluate to `true`. | ||
Otherwise, the result is `false`. | ||
- OR (`||`) operator computes the logical OR of its operands. | ||
The result of `x || y` is `true` if either `x` or `y` evaluates to `true`. | ||
Otherwise, the result is `false`. | ||
- NOT (`!`) operator computes logical negation of its operand. | ||
That is, it produces `true`, if the operand evaluates to `false`, and `false`, if the operand evaluates to `true`. | ||
|
||
The following example demonstrates the effect of each operator on bool values: | ||
|
||
```rust | ||
let x = true; | ||
let y = false; | ||
|
||
!x // => false | ||
|
||
x && y // => false | ||
|
||
x || y // => true | ||
|
||
``` |
This file contains 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,8 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
**/*.rs.bk | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
Cargo.lock |
This file contains 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,22 @@ | ||
{ | ||
"forked_from": [ | ||
"fsharp/annalyns-infiltration" | ||
], | ||
"blurb": "Learn about booleans while helping Annalyn rescue her friend.", | ||
"authors": [ | ||
"devkabiir" | ||
], | ||
"contributors": [], | ||
"files": { | ||
"solution": [ | ||
"src/lib.rs", | ||
"Cargo.toml" | ||
], | ||
"test": [ | ||
"tests/annalyns-infiltration.rs" | ||
], | ||
"exemplar": [ | ||
".meta/exemplar.rs" | ||
] | ||
} | ||
} |
Oops, something went wrong.