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

bird-watcher concept exercise #267

Open
wants to merge 3 commits 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
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@
"prerequisites": [],
"status": "wip"
},
{
"slug": "bird-watcher",
"name": "Bird Watcher",
"uuid": "ceac4ca0-3bfe-4ca7-9b83-3c9d465ef67f",
"concepts": [
"vector-filtering"
],
"prerequisites": [
"vectors"
],
"status": "wip"
},
{
"slug": "cars-assemble",
"name": "Cars, Assemble",
Expand Down
1 change: 1 addition & 0 deletions exercises/concept/bird-watcher/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hints
56 changes: 56 additions & 0 deletions exercises/concept/bird-watcher/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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 how many birds visited today

Implement the `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.

```R
birds_per_day <- c(2, 5, 0, 7, 4, 1)
today(birds_per_day)
# => 1
```

## 2. Increment today's count

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

```R
birds_per_day <- c(2, 5, 0, 7, 4, 1)
increment_todays_count(birds_per_day)
# => c(2, 5, 0, 7, 4, 2)
```

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

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

```R
birds_per_day <- c(2, 5, 0, 7, 4, 1)
has_day_without_birds(birds_per_day)
# => true
```

## 4. Calculate the number of visiting birds for the first number of days

Implement the `count_for_first_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.

```R
birds_per_day <- c(2, 5, 0, 7, 4, 1)
count_for_first_days(birds_per_day, 4)
# => 14
```

## 5. 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 `busy_days()` function to return the number of busy days:

```R
birds_per_day <- c(2, 5, 0, 7, 4, 1)
busy_days(birds_per_day)
# => 2
```
45 changes: 45 additions & 0 deletions exercises/concept/bird-watcher/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Introduction

We saw in the `vectors` concept that a vector can be used in a conditional expression, giving a vector of booleans. This in turn can be used in functions such as `all()` and `any()`.

```R
> v <- c(4, 7, 10)
> v >= 6
[1] FALSE TRUE TRUE
> all(v > 6)
[1] FALSE # not all elements match this condition
> any(v > 6)
[1] TRUE # at least one element matches
```

The technique is much more powerful than this.

## Array subsets

Selected elements of an array can be pulled out with an index number or a vector of indices:

```R
> v <- 5:10
> v
[1] 5 6 7 8 9 10
> v[3]
[1] 7
> v[c(2, 4)]
[1] 6 8
```

Alternatively, use a vector of booleans to filter the original vector, returning a subset of entries matched to a `TRUE` value:

```R
> v <- 1:3
> bools <- c(FALSE, TRUE, TRUE)
> v[bools]
[1] 2 3
```

It is a small step from there to generating the boolean vector with a conditional expression:

```R
> v[v >= 2]
[1] 2 3
```
11 changes: 11 additions & 0 deletions exercises/concept/bird-watcher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"authors": ["colinleach"],
"contributors": [],
"files": {
"solution": ["bird-watcher.R"],
"test": ["test_bird-watcher.R"],
"exemplar": [".meta/exemplar.R"]
},
"forked_from": ["Csharp/bird-watcher"],
"blurb": "Learn about R vectors by keeping track of how many birds visit your garden."
}
24 changes: 24 additions & 0 deletions exercises/concept/bird-watcher/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Design

## Goal

The goal of this exercise is to build on the `vectors` concept by teaching the student more about vector filtering/subsetting in R. It is the second exercise on this concept, after `elyses-analytic-enchantments`.

## Learning objectives

- Practice the use of conditional expressions to get a subset of the original vector.

## Out of scope

- Recycling is not mentioned within this exercise, though it is explained in `vector-filtering/about.md` which unlocks on completing this exercise.

## Concepts

The Concepts this exercise unlocks are:

- `vector-functions`
- `nothingness`

## Prerequisites

- `vectors`
22 changes: 22 additions & 0 deletions exercises/concept/bird-watcher/.meta/exemplar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
today <- function(birds_per_day) {
tail(birds_per_day, 1)
}

increment_todays_count <- function(birds_per_day) {
# clunky!
birds_per_day[length(birds_per_day)] <-
birds_per_day[length(birds_per_day)] + 1
birds_per_day
}

has_day_without_birds <- function(birds_per_day) {
any(birds_per_day == 0)
}

count_for_first_days <- function(birds_per_day, num_days) {
sum(birds_per_day[1:num_days])
}

busy_days <- function(birds_per_day) {
length(birds_per_day[birds_per_day >= 5])
}
14 changes: 14 additions & 0 deletions exercises/concept/bird-watcher/bird-watcher.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
today <- function(birds_per_day) {
}

increment_todays_count <- function(birds_per_day) {
}

has_day_without_birds <- function(birds_per_day) {
}

count_for_first_days <- function(birds_per_day, num_days) {
}

busy_days <- function(birds_per_day) {
}
68 changes: 68 additions & 0 deletions exercises/concept/bird-watcher/test_bird-watcher.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
source("./bird-watcher.R")
library(testthat)

# 1) today

test_that("Today for disappointing day", {
birds_per_day <- c(0, 0, 1, 0, 0, 1, 0)
expect_equal(today(birds_per_day), 0)
})

test_that("Today for busy day", {
birds_per_day <- c(8, 8, 9, 5, 4, 7, 10)
expect_equal(today(birds_per_day), 10)
})

# 2) increment_todays_count

test_that("Increment todays count with no previous visits", {
birds_per_day <- c(0, 0, 0, 4, 2, 3, 0)
expect_equal(
increment_todays_count(birds_per_day),
c(0, 0, 0, 4, 2, 3, 1)
)
})

test_that("Increment todays count with multiple previous visits", {
birds_per_day <- c(8, 8, 9, 2, 1, 6, 4)
expect_equal(
increment_todays_count(birds_per_day),
c(8, 8, 9, 2, 1, 6, 5)
)
})

# 3) has_day_without_birds

test_that("Has day without birds with day without birds", {
birds_per_day <- c(5, 5, 4, 0, 7, 6, 7)
expect_equal(has_day_without_birds(birds_per_day), TRUE)
})

test_that("Has day without birds with no day without birds", {
birds_per_day <- c(4, 5, 9, 10, 9, 4, 3)
expect_equal(has_day_without_birds(birds_per_day), FALSE)
})

# 4) count_for_first_days

test_that("Count for first three days of disappointing week", {
birds_per_day <- c(0, 0, 1, 0, 0, 1, 0)
expect_equal(count_for_first_days(birds_per_day, 3), 1)
})

test_that("Count for first six days of busy week", {
birds_per_day <- c(5, 9, 12, 6, 8, 8, 17)
expect_equal(count_for_first_days(birds_per_day, 6), 48)
})

# 5) busy_days

test_that("Busy days for disappointing week", {
birds_per_day <- c(1, 1, 1, 0, 0, 0, 0)
expect_equal(busy_days(birds_per_day), 0)
})

test_that("Busy days for busy week", {
birds_per_day <- c(4, 9, 5, 7, 8, 8, 2)
expect_equal(busy_days(birds_per_day), 5)
})