-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (concepts) Add concept exercise
bird-watcher
This is inspired by the same in csharp track. Provides introduction to for loops, arrays and a bit of iterators.
- Loading branch information
Showing
11 changed files
with
600 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
Oops, something went wrong.