Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (concepts) Add concept exercise annalyns-infiltration #1668

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.
Copy link
Contributor

@senekor senekor May 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A student may wonder, what happens if a number value is used as a boolean? Is that a runtime panic?

Might be good to note here that it is a compile time error and the programmer is helpfully prevented from making this mistake.


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"binary operator" usually means "an operator that takes two operators". (as opposed to unary and ternary operators).

I suggest "bitwise operator" as a less ambiguous term here.

(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 `&`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should omit any mention of the bitwise operators in the concept about booleans. I've only ever seen bitwise operators used with unsigned integer types, where they operate on bit patterns.

For booleans, as is correctly described here, the difference between | and || etc. is very small - it's ony about the lazy evaluation. And a program which relies on lazy / eager evaluation of it's boolean expressions because the operands are side-effectful... is a terrible program in my opinion.

In other languages, it may be common to use the lazy boolean operators for control flow, but not in Rust.

With all that in mind, talking about the bitwise operators for booleans at all seems confusing at best, and suggestive of bad practices at worst.

Opinions? @MatthijsBlom

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fully agree that some Operations on Bits concept node would be a better place for introducing the bitwise operators.


And a program which relies on lazy / eager evaluation of it's boolean expressions because the operands are side-effectful... is a terrible program in my opinion.

Not exactly a counterexample – because it does not involve side effects – but: e.g. if some_list and predicate(some_list[0]) is a fairly common construct in Python. The some_list checks that it is not empty; only if it nonempty is some_list[0] looked up, avoiding a runtime error.

More generally, successful evaluation of subsequent propositions might depend on prior propositions holding.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the exact purpose of about.md and introduction.md on a concept? There is a lot of duplicated text between the two files, is that normal? I guess would be that some exaplantion should go in one of the two files - not in both.

@ErikSchierboom

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are documented here: https://exercism.org/docs/building/tracks/concepts

introduction.md is shown to students who have not yet solved the concept exercise(s?), and about.md is shown to those who have.

It is entirely normal for the two to share a lot of text. It also happens that they are identical.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's entirely correct.

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",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mostly copied csharp track and generated a uuid v4. Is there anything else that needs to be done to enable this concept?

Copy link
Member

@dem4ron dem4ron Apr 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately concept exercises just got disabled for Rust. The syllabus will need a complete rework, so it might take some time to enable this concept exercise. (CC @ErikSchierboom)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, this is exactly why I'm implementing simpler concept exercises. I only wanted to know if there are any other config files to touch to prepare it for enabling.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will show in-progress exercise for track maintainers though.

"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
devkabiir marked this conversation as resolved.
Show resolved Hide resolved

- 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is even more duplication here. The text explaining booleans seems to be copied three times? in the concept's about, the concept's introduction and here in the exercise's introduction.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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