Skip to content

Commit bcc1e0f

Browse files
committed
✨ (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.
1 parent 796f2ca commit bcc1e0f

File tree

11 files changed

+600
-0
lines changed

11 files changed

+600
-0
lines changed

config.json

+16
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@
3535
},
3636
"exercises": {
3737
"concept": [
38+
{
39+
"slug": "bird-watchers",
40+
"uuid": "ef6bdeea-56d5-44d9-8971-1ac3b3a8f624",
41+
"name": "Bird Watcher",
42+
"difficulty": 1,
43+
"concepts": [
44+
"for-loops",
45+
"ranges",
46+
"arrays"
47+
],
48+
"prerequisites": [
49+
"if-statements",
50+
"assignment"
51+
],
52+
"status": "wip"
53+
},
3854
{
3955
"slug": "lucians-luscious-lasagna",
4056
"uuid": "29a2d3bd-eec8-454d-9dba-4b2d7d071925",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Hints
2+
3+
## General
4+
5+
- The bird count per day is an array that contains exactly 7 integers.
6+
7+
## 1. Check what the counts were last week
8+
9+
- Define an array that contains last week's log.
10+
- Return the array as a result.
11+
12+
## 2. Check how many birds visited today
13+
14+
- Remember that the counts are ordered by day from oldest to most recent, with the last element representing today.
15+
- Accessing the last element can be done by using its (fixed) index (remember to start counting from zero).
16+
17+
## 3. Increment today's count
18+
19+
- Set the element representing today's count to today's count plus 1.
20+
21+
## 4. Check if there was a day with no visiting birds
22+
23+
- Use if statements for performing comparisions with array elements.
24+
- Use `for in` construct to loop over an array.
25+
26+
## 5. Calculate the number of visiting birds for the first x number of days
27+
28+
- A variable can be used to hold the count for the number of visiting birds.
29+
- The array can be _enumerated_ over using [`.iter()`][.iter()] and [`.enumerate()`][.enumerate()] methods.
30+
- The variable can be updated inside the loop.
31+
- Remember: arrays are indexed from `0`.
32+
- Use `break` statement to stop a loop.
33+
34+
## 6. Calculate the number of busy days
35+
36+
- A variable can be used to hold the number of busy days.
37+
- The array can be _iterated_ over using [`.iter()`][.iter()] method.
38+
- The variable can be updated inside the loop.
39+
- A [conditional statement][if-statement] can be used inside the loop.
40+
41+
[if-statement]: https://doc.rust-lang.org/rust-by-example/flow_control/if_else.html
42+
[.iter()]: https://doc.rust-lang.org/rust-by-example/flow_control/for.html?highlight=iter()#for-and-iterators
43+
[.enumerate()]:
44+
https://doc.rust-lang.org/core/iter/trait.Iterator.html#method.enumerate
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Instructions
2+
3+
You're an avid bird watcher that keeps track of how many birds have visited your garden in the last seven days.
4+
5+
You have six tasks, all dealing with the numbers of birds that visited your garden.
6+
7+
## 1. Check what the counts were last week
8+
9+
For comparison purposes, you always keep a copy of last week's log nearby,
10+
which were: 0, 2, 5, 3, 7, 8 and 4. Implement the `last_week_log()` function
11+
that returns last week's log
12+
13+
```rust
14+
last_week_log();
15+
// => [0, 2, 5, 3, 7, 8, 4]
16+
```
17+
18+
## 2. Check how many birds visited today
19+
20+
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.
21+
22+
```rust
23+
let watch_log = [ 2, 5, 0, 7, 4, 1 ];
24+
count_today(watch_log);
25+
// => 1
26+
```
27+
28+
## 3. Increment today's count
29+
30+
Implement the `log_today()` function to increment today's count:
31+
32+
```rust
33+
let watch_log = [ 2, 5, 0, 7, 4, 1 ];
34+
log_today(watch_log);
35+
count_today(watch_log);
36+
// => 2
37+
```
38+
39+
## 4. Check if there was a day with no visiting birds
40+
41+
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`:
42+
43+
```rust
44+
let watch_log = [ 2, 5, 0, 7, 4, 1 ];
45+
has_day_without_birds(watch_log);
46+
// => true
47+
```
48+
49+
## 5. Calculate the number of visiting birds for the first x number of days
50+
51+
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.
52+
53+
```rust
54+
let watch_log = [ 2, 5, 0, 7, 4, 1 ];
55+
tally_days(watch_log, 4);
56+
// => 14
57+
```
58+
59+
## 6. Calculate the number of busy days
60+
61+
Some days are busier that others. A busy day is one where five or more birds have visited your garden.
62+
Implement the `calc_busy_days()` function to return the number of busy days:
63+
64+
```rust
65+
let watch_log = [ 2, 5, 0, 7, 4, 1 ];
66+
calc_busy_days(watch_log);
67+
// => 2
68+
```

0 commit comments

Comments
 (0)