Skip to content

Commit

Permalink
✨ (concepts) Add concept exercise annalyns-infiltration
Browse files Browse the repository at this point in the history
This concept is inspired from the same in csharp track
  • Loading branch information
devkabiir committed May 1, 2023
1 parent 399488b commit be86eca
Show file tree
Hide file tree
Showing 13 changed files with 784 additions and 24 deletions.
101 changes: 91 additions & 10 deletions concepts/booleans/about.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,68 @@
# About

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

Rust supports one [boolean negation operator][negation operators]: `!` (Not).
## Declaration

Rust supports six [comparison operators][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.
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.

### 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
}

```

### Operators

Rust supports various operators on `bool` values.

- 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

```

The [comparison operators][comparison operators] can be used to evaluate the relationship of non-boolean values into a boolean value.
- `==` (Equal)
- `!=` (Not equal)
- `>` (Greater than)
- `<` (Less than)
- `>=` (Greater than or equal to)
- `<=` (Less than or equal to).

```rust
1 > 0 // true
Expand All @@ -19,18 +75,43 @@ values into a boolean value.

Unlike some other languages, `0` is not `false` and non-zero is not `true`.

Rust supports three [logical binary operators][logical binary operators]: `&` (Logical And), `|` (Logical Or), `^` (Logical Xor).
Rust supports three [logical binary operators][logical binary operators]: `&`
(Logical And), `|` (Logical Or), `^` (Logical Xor).

- `|` (Logical Or)
| `a` | `b` | `a \| b` |
|- | - | - |
| `true` | `true` | `true` |
| `true` | `false` | `true` |
| `false` | `true` | `true` |
| `false` | `false` | `false` |

- `&` (Logical And)
| `a` | `b` | [`a & b` |
|- | - | - |
| `true` | `true` | `true` |
| `true` | `false` | `false` |
| `false` | `true` | `false` |
| `false` | `false` | `false` |

- `^` (Logical Xor)
| `a` | `b` | `a ^ b` |
|- | - | - |
| `true` | `true` | `false` |
| `true` | `false` | `true` |
| `false` | `true` | `true` |
| `false` | `false` | `false` |

Rust supports two [boolean operators][lazy boolean operators]: `||` (Or), `&&` (And). They differ from `|` and `&` in that the right-hand operand
is 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`.
The operators `||` (OR), `&&` (AND) differ from `|` and `&`.
The `||` only evaluates its right-hand operand when the left-hand operand
evaluates to `false`, and `&&` only when it evaluates to `true`.
They are also called [lazy boolean operators] for this reason.

```rust
true || false // == true; the right-hand side was not evaluated
true && false // == false
```

[negation operators]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#negation-operators
[comparison operators]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#comparison-operators
[logical binary operators]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#arithmetic-and-logical-binary-operators
[lazy boolean operators]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#lazy-boolean-operators
61 changes: 47 additions & 14 deletions concepts/booleans/introduction.md
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

```
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
},
"exercises": {
"concept": [
{
"slug": "annalyns-infiltration",
"name": "Annalyn's Infiltration",
"uuid": "897fa2ea-c29c-4d4e-9469-ff661a9b838a",
"difficulty": 1,
"concepts": [
"booleans"
],
"prerequisites": [],
"status": "wip"
},
{
"slug": "lucians-luscious-lasagna",
"uuid": "29a2d3bd-eec8-454d-9dba-4b2d7d071925",
Expand Down
13 changes: 13 additions & 0 deletions exercises/concept/annalyns-infiltration/.docs/hints.md
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 exercises/concept/annalyns-infiltration/.docs/instructions.md
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 exercises/concept/annalyns-infiltration/.docs/introduction.md
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

```
8 changes: 8 additions & 0 deletions exercises/concept/annalyns-infiltration/.gitignore
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
22 changes: 22 additions & 0 deletions exercises/concept/annalyns-infiltration/.meta/config.json
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"
]
}
}
Loading

0 comments on commit be86eca

Please sign in to comment.