Skip to content

Commit 9550d96

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

File tree

12 files changed

+684
-15
lines changed

12 files changed

+684
-15
lines changed

concepts/booleans/introduction.md

+42-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
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`.
89

910
```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
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+
1631
```
1732

18-
Unlike some other languages, `0` is not `false` and non-zero is not `true`.
33+
### Basic Operators
34+
Rust supports AND (&&), OR (||), NOT (!) and EQALITY (==) operators on Booleans
1935

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`.
36+
```rust
37+
let a = true;
38+
let b = false;
39+
40+
!a // => false
41+
42+
a == b // => false
43+
44+
a == true // => true
45+
46+
a && b // => false
47+
48+
a || b // => true
49+
50+
```

config.json

+14-1
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",
@@ -43,7 +54,9 @@
4354
"concepts": [
4455
"functions"
4556
],
46-
"prerequisites": [],
57+
"prerequisites": [
58+
"booleans"
59+
],
4760
"status": "active"
4861
},
4962
{
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,49 @@
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+
```rust
10+
let t = true;
11+
12+
let f: bool = false; // with explicit type annotation
13+
```
14+
15+
The main way to use Boolean values is through conditionals, such as an if
16+
expression.
17+
We’ll cover how if expressions work in Rust in later concepts.
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+
### Basic Operators
33+
Rust supports AND (&&), OR (||), NOT (!) and EQALITY (==) operators on Booleans
34+
35+
```rust
36+
let a = true;
37+
let b = false;
38+
39+
!a // => false
40+
41+
a == b // => false
42+
43+
a == true // => true
44+
45+
a && b // => false
46+
47+
a || b // => true
48+
49+
```
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Design
2+
3+
## Learning objectives
4+
5+
- Know of the existence of the `bool` type and its two values.
6+
- Know about boolean operators and how to build logical expressions with them.
7+
- Know of the boolean operator precedence rules.
8+
9+
## Out of scope
10+
11+
- Pattern matching on booleans.
12+
13+
## Concepts
14+
15+
- `booleans`: know of the existence of the `bool` type and its two values; know about boolean operators and how to build logical expressions with them; know of the boolean operator precedence rules.
16+
17+
## Prerequisites
18+
19+
This exercise's prerequisites Concepts are:
20+
21+
- none
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub fn can_fast_attack(knight_is_awake: bool) -> bool {
2+
return !knight_is_awake;
3+
}
4+
5+
pub fn can_spy(knight_is_awake: bool, archer_is_awake: bool, prisoner_is_awake: bool) -> bool {
6+
return knight_is_awake || archer_is_awake || prisoner_is_awake;
7+
}
8+
9+
pub fn can_signal_prisoner(archer_is_awake: bool, prisoner_is_awake: bool) -> bool {
10+
return !archer_is_awake && prisoner_is_awake;
11+
}
12+
13+
pub fn can_free_prisoner(
14+
knight_is_awake: bool,
15+
archer_is_awake: bool,
16+
prisoner_is_awake: bool,
17+
pet_dog_is_present: bool,
18+
) -> bool {
19+
return !knight_is_awake && !archer_is_awake && prisoner_is_awake
20+
|| !archer_is_awake && pet_dog_is_present;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
edition = "2021"
3+
name = "annalyns_infiltration"
4+
version = "1.0.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
pub fn can_fast_attack(_knight_is_awake: bool) -> bool {
2+
unimplemented!("Implement can_fast_attack");
3+
}
4+
5+
pub fn can_spy(_knight_is_awake: bool, _archer_is_awake: bool, _prisoner_is_awake: bool) -> bool {
6+
unimplemented!("Implement can_spy");
7+
}
8+
9+
pub fn can_signal_prisoner(_archer_is_awake: bool, _prisoner_is_awake: bool) -> bool {
10+
unimplemented!("Implement can_signal_prisoner");
11+
}
12+
13+
pub fn can_free_prisoner(
14+
_knight_is_awake: bool,
15+
_archer_is_awake: bool,
16+
_prisoner_is_awake: bool,
17+
_pet_dog_is_present: bool,
18+
) -> bool {
19+
unimplemented!("Implement can_free_prisoner");
20+
}

0 commit comments

Comments
 (0)