Skip to content

Commit 79ba19e

Browse files
committed
✨ (concepts) Add concept exercise annalyns-infiltration
This concept is inspired from the same in csharp track
1 parent 399488b commit 79ba19e

File tree

13 files changed

+786
-24
lines changed

13 files changed

+786
-24
lines changed

concepts/booleans/about.md

+93-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,68 @@
11
# About
22

3-
Booleans in Rust are represented by the `bool` type, which values can be either `true` or `false`.
3+
Booleans in Rust are represented by the `bool` type, with values either
4+
`true` or `false`.
45

5-
Rust supports one [boolean negation operator][negation operators]: `!` (Not).
6+
## Declaration
67

7-
Rust supports six [comparison operators][comparison operators]: `==` (Equal), `!=` (Not equal), `>` (Greater than), `<` (Less than), `>=`
8-
(Greater than or equal to), `<=` (Less than or equal to). They can be used to evaluate the relationship of non-boolean
9-
values into a boolean value.
8+
The Boolean type in Rust is specified using `bool`.
9+
10+
```rust
11+
let t = true;
12+
13+
let f: bool = false; // with explicit type annotation
14+
```
15+
16+
The main way to use Boolean values is through conditionals, such as an if
17+
expression.
18+
19+
### Use as a type
20+
Booleans can be used as a type for a variable, function parameter or return
21+
type.
22+
23+
```rust
24+
let my_variable: bool;
25+
26+
fn my_function(input: bool) -> bool {
27+
// Do something
28+
}
29+
30+
```
31+
32+
### Operators
33+
34+
Rust supports various operators on `bool` values.
35+
36+
- AND (`&&`) operator computes the logical AND of its operands.
37+
The result of `x && y` is `true` if both `x` and `y` evaluate to `true`.
38+
Otherwise, the result is `false`.
39+
- OR (`||`) operator computes the logical OR of its operands.
40+
The result of `x || y` is `true` if either `x` or `y` evaluates to `true`. Otherwise, the result is `false`.
41+
- NOT (`!`) operator computes logical negation of its operand.
42+
That is, it produces `true`, if the operand evaluates to `false`, and `false`,
43+
if the operand evaluates to `true`.
44+
45+
The following example demonstrates the effect of each operator on bool values:
46+
47+
```rust
48+
let x = true;
49+
let y = false;
50+
51+
!x // => false
52+
53+
x && y // => false
54+
55+
x || y // => true
56+
57+
```
58+
59+
The [comparison operators][comparison operators] can be used to evaluate the relationship of non-boolean values into a boolean value.
60+
- `==` (Equal)
61+
- `!=` (Not equal)
62+
- `>` (Greater than)
63+
- `<` (Less than)
64+
- `>=` (Greater than or equal to)
65+
- `<=` (Less than or equal to).
1066

1167
```rust
1268
1 > 0 // true
@@ -17,20 +73,47 @@ values into a boolean value.
1773
1 <= 0 // false
1874
```
1975

76+
~~~~exercism/note
2077
Unlike some other languages, `0` is not `false` and non-zero is not `true`.
78+
~~~~
79+
80+
Rust supports three [logical binary operators][logical binary operators]: `&`
81+
(Logical And), `|` (Logical Or), `^` (Logical Xor).
82+
83+
- `|` (Logical Or)
84+
| `a` | `b` | <code>a &#124; b</code> |
85+
|- | - | - |
86+
| `true` | `true` | `true` |
87+
| `true` | `false` | `true` |
88+
| `false` | `true` | `true` |
89+
| `false` | `false` | `false` |
90+
91+
- `&` (Logical And)
92+
| `a` | `b` | [`a & b` |
93+
|- | - | - |
94+
| `true` | `true` | `true` |
95+
| `true` | `false` | `false` |
96+
| `false` | `true` | `false` |
97+
| `false` | `false` | `false` |
2198

22-
Rust supports three [logical binary operators][logical binary operators]: `&` (Logical And), `|` (Logical Or), `^` (Logical Xor).
99+
- `^` (Logical Xor)
100+
| `a` | `b` | `a ^ b` |
101+
|- | - | - |
102+
| `true` | `true` | `false` |
103+
| `true` | `false` | `true` |
104+
| `false` | `true` | `true` |
105+
| `false` | `false` | `false` |
23106

24-
Rust supports two [boolean operators][lazy boolean operators]: `||` (Or), `&&` (And). They differ from `|` and `&` in that the right-hand operand
25-
is only evaluated when the left-hand operand does not already determine the result of the expression. That is, `||` only evaluates
26-
its right-hand operand when the left-hand operand evaluates to `false`, and `&&` only when it evaluates to `true`.
107+
The operators `||` (OR), `&&` (AND) differ from `|` and `&`.
108+
The `||` only evaluates its right-hand operand when the left-hand operand
109+
evaluates to `false`, and `&&` only when it evaluates to `true`.
110+
They are also called [lazy boolean operators] for this reason.
27111

28112
```rust
29113
true || false // == true; the right-hand side was not evaluated
30114
true && false // == false
31115
```
32116

33-
[negation operators]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#negation-operators
34117
[comparison operators]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#comparison-operators
35118
[logical binary operators]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#arithmetic-and-logical-binary-operators
36119
[lazy boolean operators]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#lazy-boolean-operators

concepts/booleans/introduction.md

+47-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,55 @@
11
# Introduction
22

3-
Booleans in Rust are represented by the `bool` type, which values can be either `true` or `false`.
3+
## Booleans
44

5-
Rust supports six comparison operators: `==` (Equal), `!=` (Not equal), `>` (Greater than), `<` (Less than), `>=`
6-
(Greater than or equal to), `<=` (Less than or equal to). They can be used to evaluate the relationship of non-boolean
7-
values into a boolean value.
5+
As in most other programming languages, a Boolean type in Rust has two possible values: `true` and `false`.
6+
7+
### Declaration
8+
The Boolean type in Rust is specified using `bool`.
9+
10+
```rust
11+
let t = true;
12+
13+
let f: bool = false; // with explicit type annotation
14+
```
15+
16+
The main way to use Boolean values is through conditionals, such as an if
17+
expression.
18+
We’ll cover how if expressions work in Rust in later concepts.
19+
20+
### Use as a type
21+
Booleans can be used as a type for a variable, function parameter or return
22+
type.
823

924
```rust
10-
1 > 0 // true
11-
1 < 0 // false
12-
1 == 0 // false
13-
1 != 0 // true
14-
1 >= 0 // true
15-
1 <= 0 // false
25+
let my_variable: bool;
26+
27+
fn my_function(input: bool) -> bool {
28+
// Do something
29+
}
30+
1631
```
1732

18-
Unlike some other languages, `0` is not `false` and non-zero is not `true`.
33+
### Basic Operators
34+
Rust supports following operators on `bool`:
35+
- AND (`&&`) operator computes the logical AND of its operands.
36+
The result of `x && y` is `true` if both `x` and `y` evaluate to `true`.
37+
Otherwise, the result is `false`.
38+
- OR (`||`) operator computes the logical OR of its operands.
39+
The result of `x || y` is `true` if either `x` or `y` evaluates to `true`. Otherwise, the result is `false`.
40+
- NOT (`!`) operator computes logical negation of its operand.
41+
That is, it produces `true`, if the operand evaluates to `false`, and `false`, if the operand evaluates to `true`.
42+
43+
The following example demonstrates the effect of each operator on bool values:
44+
45+
```rust
46+
let x = true;
47+
let y = false;
48+
49+
!x // => false
50+
51+
x && y // => false
1952

20-
Rust supports two boolean operators: `||` (Or), `&&` (And). They are only evaluated when the left-hand operand does not already
21-
determine the result of the expression. That is, `||` only evaluates its right-hand operand when the left-hand operand evaluates
22-
to `false`, and `&&` only when it evaluates to `true`.
53+
x || y // => true
54+
55+
```

config.json

+11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@
3535
},
3636
"exercises": {
3737
"concept": [
38+
{
39+
"slug": "annalyns-infiltration",
40+
"name": "Annalyn's Infiltration",
41+
"uuid": "897fa2ea-c29c-4d4e-9469-ff661a9b838a",
42+
"difficulty": 1,
43+
"concepts": [
44+
"booleans"
45+
],
46+
"prerequisites": [],
47+
"status": "wip"
48+
},
3849
{
3950
"slug": "lucians-luscious-lasagna",
4051
"uuid": "29a2d3bd-eec8-454d-9dba-4b2d7d071925",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Hints
2+
3+
## General
4+
5+
- There are three boolean operators<sup>[1] [2]</sup> to work with boolean values.
6+
- Multiple operators can be combined in a single expression.
7+
8+
## 2. Check if a fast attack can be made
9+
10+
- The boolean operators<sup>[1] [2]</sup> can also be applied to boolean parameters.
11+
12+
[1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#lazy-boolean-operators
13+
[2]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#negation-operators
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Instructions
2+
3+
In this exercise, you'll be implementing the quest logic for a new RPG game a friend is developing.
4+
5+
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.
6+
7+
Annalyn will try to find and free her best friend, optionally taking her dog with her on this quest.
8+
9+
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.
10+
11+
Having found the kidnappers, Annalyn considers which of the following actions she can engage in:
12+
13+
- _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.
14+
- _Spy_: the group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time.
15+
- _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.
16+
- _Free prisoner_: Annalyn can try sneaking into the camp to free the prisoner.
17+
This is a risky thing to do and can only succeed in one of two ways:
18+
- If Annalyn has her pet dog with her she can rescue the prisoner if the archer is asleep.
19+
The knight is scared of the dog and the archer will not have time to get ready before Annalyn and the prisoner can escape.
20+
- If Annalyn does not have her dog then she and the prisoner must be very sneaky!
21+
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.
22+
23+
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.
24+
25+
## 1. Check if a fast attack can be made
26+
27+
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`:
28+
29+
```rust
30+
let knight_is_awake = true;
31+
can_fast_attack(knight_is_awake);
32+
// => false
33+
```
34+
35+
## 2. Check if the group can be spied upon
36+
37+
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`:
38+
39+
```rust
40+
let knight_is_awake = false;
41+
let archer_is_awake = true;
42+
let prisoner_is_awake = false;
43+
can_spy(knight_is_awake, archer_is_awake, prisoner_is_awake);
44+
// => true
45+
```
46+
47+
## 3. Check if the prisoner can be signalled
48+
49+
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`:
50+
51+
```rust
52+
let archer_is_awake = false;
53+
let prisoner_is_awake = true;
54+
can_signal_prisoner(archer_is_awake, prisoner_is_awake);
55+
// => true
56+
```
57+
58+
## 4. Check if the prisoner can be freed
59+
60+
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`:
61+
62+
```rust
63+
let knight_is_awake = false;
64+
let archer_is_awake = true;
65+
let prisoner_is_awake = false;
66+
let pet_dog_is_present = false;
67+
can_free_prisoner(knight_is_awake, archer_is_awake, prisoner_is_awake, pet_dog_is_present);
68+
// => false
69+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Introduction
2+
3+
## Booleans
4+
5+
As in most other programming languages, a Boolean type in Rust has two possible values: `true` and `false`.
6+
7+
### Declaration
8+
The Boolean type in Rust is specified using `bool`.
9+
10+
```rust
11+
let t = true;
12+
13+
let f: bool = false; // with explicit type annotation
14+
```
15+
16+
The main way to use Boolean values is through conditionals, such as an if
17+
expression.
18+
We’ll cover how if expressions work in Rust in later concepts.
19+
20+
### Use as a type
21+
Booleans can be used as a type for a variable, function parameter or return
22+
type.
23+
24+
```rust
25+
let my_variable: bool;
26+
27+
fn my_function(input: bool) -> bool {
28+
// Do something
29+
}
30+
31+
```
32+
33+
### Basic Operators
34+
Rust supports following operators on `bool`:
35+
- AND (`&&`) operator computes the logical AND of its operands.
36+
The result of `x && y` is `true` if both `x` and `y` evaluate to `true`.
37+
Otherwise, the result is `false`.
38+
- OR (`||`) operator computes the logical OR of its operands.
39+
The result of `x || y` is `true` if either `x` or `y` evaluates to `true`.
40+
Otherwise, the result is `false`.
41+
- NOT (`!`) operator computes logical negation of its operand.
42+
That is, it produces `true`, if the operand evaluates to `false`, and `false`, if the operand evaluates to `true`.
43+
44+
The following example demonstrates the effect of each operator on bool values:
45+
46+
```rust
47+
let x = true;
48+
let y = false;
49+
50+
!x // => false
51+
52+
x && y // => false
53+
54+
x || y // => true
55+
56+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
/target/
4+
**/*.rs.bk
5+
6+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7+
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
8+
Cargo.lock
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"forked_from": [
3+
"fsharp/annalyns-infiltration"
4+
],
5+
"blurb": "Learn about booleans while helping Annalyn rescue her friend.",
6+
"authors": [
7+
"devkabiir"
8+
],
9+
"contributors": [],
10+
"files": {
11+
"solution": [
12+
"src/lib.rs",
13+
"Cargo.toml"
14+
],
15+
"test": [
16+
"tests/annalyns-infiltration.rs"
17+
],
18+
"exemplar": [
19+
".meta/exemplar.rs"
20+
]
21+
}
22+
}

0 commit comments

Comments
 (0)