Skip to content

Commit

Permalink
✨ (concepts) Add concept exercise bird-watcher
Browse files Browse the repository at this point in the history
This is inspired by the same in csharp track. Provides introduction to for loops, arrays and a bit of iterators.
  • Loading branch information
devkabiir committed Apr 10, 2023
1 parent 796f2ca commit 78f358c
Show file tree
Hide file tree
Showing 11 changed files with 600 additions and 0 deletions.
16 changes: 16 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@
},
"exercises": {
"concept": [
{
"slug": "bird-watcher",
"uuid": "ef6bdeea-56d5-44d9-8971-1ac3b3a8f624",
"name": "Bird Watcher",
"difficulty": 1,
"concepts": [
"for-loops",
"ranges",
"arrays"
],
"prerequisites": [
"if-statements",
"assignment"
],
"status": "wip"
},
{
"slug": "lucians-luscious-lasagna",
"uuid": "29a2d3bd-eec8-454d-9dba-4b2d7d071925",
Expand Down
44 changes: 44 additions & 0 deletions exercises/concept/bird-watcher/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Hints

## General

- The bird count per day is an array that contains exactly 7 integers.

## 1. Check what the counts were last week

- Define an array that contains last week's log.
- Return the array as a result.

## 2. Check how many birds visited today

- Remember that the counts are ordered by day from oldest to most recent, with the last element representing today.
- Accessing the last element can be done by using its (fixed) index (remember to start counting from zero).

## 3. Increment today's count

- Set the element representing today's count to today's count plus 1.

## 4. Check if there was a day with no visiting birds

- Use if statements for performing comparisions with array elements.
- Use `for in` construct to loop over an array.

## 5. Calculate the number of visiting birds for the first x number of days

- A variable can be used to hold the count for the number of visiting birds.
- The array can be _enumerated_ over using [`.iter()`][.iter()] and [`.enumerate()`][.enumerate()] methods.
- The variable can be updated inside the loop.
- Remember: arrays are indexed from `0`.
- Use `break` statement to stop a loop.

## 6. Calculate the number of busy days

- A variable can be used to hold the number of busy days.
- The array can be _iterated_ over using [`.iter()`][.iter()] method.
- The variable can be updated inside the loop.
- A [conditional statement][if-statement] can be used inside the loop.

[if-statement]: https://doc.rust-lang.org/rust-by-example/flow_control/if_else.html
[.iter()]: https://doc.rust-lang.org/rust-by-example/flow_control/for.html?highlight=iter()#for-and-iterators
[.enumerate()]:
https://doc.rust-lang.org/core/iter/trait.Iterator.html#method.enumerate
68 changes: 68 additions & 0 deletions exercises/concept/bird-watcher/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Instructions

You're an avid bird watcher that keeps track of how many birds have visited your garden in the last seven days.

You have six tasks, all dealing with the numbers of birds that visited your garden.

## 1. Check what the counts were last week

For comparison purposes, you always keep a copy of last week's log nearby,
which were: 0, 2, 5, 3, 7, 8 and 4. Implement the `last_week_log()` function
that returns last week's log

```rust
last_week_log();
// => [0, 2, 5, 3, 7, 8, 4]
```

## 2. Check how many birds visited today

Implement the `count_today()` function to return how many birds visited your garden today. The bird counts are ordered by day, with the first element being the count of the oldest day, and the last element being today's count.

```rust
let watch_log = [ 2, 5, 0, 7, 4, 1 ];
count_today(watch_log);
// => 1
```

## 3. Increment today's count

Implement the `log_today()` function to increment today's count:

```rust
let watch_log = [ 2, 5, 0, 7, 4, 1 ];
log_today(watch_log);
count_today(watch_log);
// => 2
```

## 4. Check if there was a day with no visiting birds

Implement the `has_day_without_birds()` method that returns `true` if there was a day at which zero birds visited the garden; otherwise, return `false`:

```rust
let watch_log = [ 2, 5, 0, 7, 4, 1 ];
has_day_without_birds(watch_log);
// => true
```

## 5. Calculate the number of visiting birds for the first x number of days

Implement the `tally_days()` function that returns the number of birds that have visited your garden from the start of the week, but limit the count to the specified number of days from the start of the week.

```rust
let watch_log = [ 2, 5, 0, 7, 4, 1 ];
tally_days(watch_log, 4);
// => 14
```

## 6. Calculate the number of busy days

Some days are busier that others. A busy day is one where five or more birds have visited your garden.
Implement the `calc_busy_days()` function to return the number of busy days:

```rust
let watch_log = [ 2, 5, 0, 7, 4, 1 ];
calc_busy_days(watch_log);
// => 2
```
Loading

0 comments on commit 78f358c

Please sign in to comment.