From bcbcfae26a5a095ab01e984212929743fd4cce32 Mon Sep 17 00:00:00 2001 From: colinleach Date: Sat, 8 Jul 2023 15:42:00 -0700 Subject: [PATCH 1/2] bird-watcher concept exercise --- config.json | 12 ++++ exercises/concept/bird-watcher/.docs/hints.md | 1 + .../bird-watcher/.docs/instructions.md | 56 +++++++++++++++ .../bird-watcher/.docs/introduction.md | 45 ++++++++++++ .../concept/bird-watcher/.meta/config.json | 11 +++ .../concept/bird-watcher/.meta/design.md | 24 +++++++ .../concept/bird-watcher/.meta/exemplar.R | 22 ++++++ exercises/concept/bird-watcher/bird-watcher.R | 14 ++++ .../concept/bird-watcher/test_bird-watcher.R | 70 +++++++++++++++++++ 9 files changed, 255 insertions(+) create mode 100644 exercises/concept/bird-watcher/.docs/hints.md create mode 100644 exercises/concept/bird-watcher/.docs/instructions.md create mode 100644 exercises/concept/bird-watcher/.docs/introduction.md create mode 100644 exercises/concept/bird-watcher/.meta/config.json create mode 100644 exercises/concept/bird-watcher/.meta/design.md create mode 100644 exercises/concept/bird-watcher/.meta/exemplar.R create mode 100644 exercises/concept/bird-watcher/bird-watcher.R create mode 100644 exercises/concept/bird-watcher/test_bird-watcher.R diff --git a/config.json b/config.json index 312e341d..afb4a380 100644 --- a/config.json +++ b/config.json @@ -41,6 +41,18 @@ "concepts": ["basics"], "prerequisites": [], "status": "wip" + }, + { + "slug": "bird-watcher", + "name": "Bird Watcher", + "uuid": "ceac4ca0-3bfe-4ca7-9b83-3c9d465ef67f", + "concepts": [ + "vector-filtering" + ], + "prerequisites": [ + "vectors" + ], + "status": "wip" } ], "practice": [ diff --git a/exercises/concept/bird-watcher/.docs/hints.md b/exercises/concept/bird-watcher/.docs/hints.md new file mode 100644 index 00000000..b5296c36 --- /dev/null +++ b/exercises/concept/bird-watcher/.docs/hints.md @@ -0,0 +1 @@ +# Hints diff --git a/exercises/concept/bird-watcher/.docs/instructions.md b/exercises/concept/bird-watcher/.docs/instructions.md new file mode 100644 index 00000000..2c499af7 --- /dev/null +++ b/exercises/concept/bird-watcher/.docs/instructions.md @@ -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 +``` \ No newline at end of file diff --git a/exercises/concept/bird-watcher/.docs/introduction.md b/exercises/concept/bird-watcher/.docs/introduction.md new file mode 100644 index 00000000..ca26ea79 --- /dev/null +++ b/exercises/concept/bird-watcher/.docs/introduction.md @@ -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 +``` diff --git a/exercises/concept/bird-watcher/.meta/config.json b/exercises/concept/bird-watcher/.meta/config.json new file mode 100644 index 00000000..9420dccf --- /dev/null +++ b/exercises/concept/bird-watcher/.meta/config.json @@ -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." +} diff --git a/exercises/concept/bird-watcher/.meta/design.md b/exercises/concept/bird-watcher/.meta/design.md new file mode 100644 index 00000000..8b7a4999 --- /dev/null +++ b/exercises/concept/bird-watcher/.meta/design.md @@ -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` diff --git a/exercises/concept/bird-watcher/.meta/exemplar.R b/exercises/concept/bird-watcher/.meta/exemplar.R new file mode 100644 index 00000000..ebfe6bbf --- /dev/null +++ b/exercises/concept/bird-watcher/.meta/exemplar.R @@ -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]) +} diff --git a/exercises/concept/bird-watcher/bird-watcher.R b/exercises/concept/bird-watcher/bird-watcher.R new file mode 100644 index 00000000..f9ff778c --- /dev/null +++ b/exercises/concept/bird-watcher/bird-watcher.R @@ -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) { +} diff --git a/exercises/concept/bird-watcher/test_bird-watcher.R b/exercises/concept/bird-watcher/test_bird-watcher.R new file mode 100644 index 00000000..7b258a79 --- /dev/null +++ b/exercises/concept/bird-watcher/test_bird-watcher.R @@ -0,0 +1,70 @@ +source("./bird-watcher.R") +library(testthat) + +context("bird-watcher") + +# 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) +}) From c7e97f3f48fd19615ced2612d80195b708a7bee1 Mon Sep 17 00:00:00 2001 From: colinleach Date: Sun, 9 Jul 2023 13:52:24 -0700 Subject: [PATCH 2/2] removed context() line --- exercises/concept/bird-watcher/test_bird-watcher.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/exercises/concept/bird-watcher/test_bird-watcher.R b/exercises/concept/bird-watcher/test_bird-watcher.R index 7b258a79..e253c155 100644 --- a/exercises/concept/bird-watcher/test_bird-watcher.R +++ b/exercises/concept/bird-watcher/test_bird-watcher.R @@ -1,8 +1,6 @@ source("./bird-watcher.R") library(testthat) -context("bird-watcher") - # 1) today test_that("Today for disappointing day", {