-
-
Notifications
You must be signed in to change notification settings - Fork 44
Add concept exercise named-resistor-color #446
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,29 @@ | ||
| # Hints | ||
|
|
||
| ## General | ||
|
|
||
| Return values for the functions are of a specific type, so pay attention. | ||
|
|
||
| ## 1. Define a named vector | ||
|
|
||
| - The necessary information for the names and values is in the instructions. | ||
| - The way to construct a named vector is covered in detail in the introduction. | ||
|
|
||
| ## 2. Return the value associated with a band | ||
|
|
||
| - You can use your `resistor_bands` named vector to accomplish this easily. | ||
| - You need to return a numeric value alone. | ||
|
|
||
| ## 3. Return the value associated with two bands | ||
|
|
||
| - Your `band_value` function can come in handy here. | ||
| - Indexing hints for named vectors are in the introduction. | ||
| - There are several ways to construct the number, most commonly via string manipulation or numerically. | ||
| - The returned number must be a numeric value alone. | ||
|
|
||
| ## 4. Return the ohms associated with all three bands | ||
|
|
||
| - Both `band_value` and `two_band_value` are useful here. | ||
| - The first two values in the vector are for the two-band value, while the third is the multiplier. | ||
| - Again, string manipulation or numerical means are possible to get the number. | ||
| - The returned number must be a numeric value alone. | ||
85 changes: 85 additions & 0 deletions
85
exercises/concept/named-resistor-color/.docs/instructions.md
This file contains hidden or 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,85 @@ | ||
| # Instructions | ||
|
|
||
| If you want to build something using a Raspberry Pi, you'll probably use _resistors_. | ||
| For this exercise, you need to know two things about them: | ||
|
|
||
| - Each resistor has a resistance value. | ||
| - Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read. | ||
|
|
||
| To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values. | ||
| Each band has a position and a numeric value. | ||
|
|
||
| The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number. | ||
|
|
||
| The third band is a multiplier. | ||
|
|
||
| In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands. | ||
|
|
||
| More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article][e-color-code]. | ||
|
|
||
| ## 1. Define a named vector | ||
|
|
||
| The colors are encoded as follows: | ||
|
|
||
| - black: 0 | ||
| - brown: 1 | ||
| - red: 2 | ||
| - orange: 3 | ||
| - yellow: 4 | ||
| - green: 5 | ||
| - blue: 6 | ||
| - violet: 7 | ||
| - grey: 8 | ||
| - white: 9 | ||
|
|
||
| Create the named vector, `resistor_bands`, which should have the colors as the names and the numbers as the values. | ||
|
|
||
| ## 2. Return the value associated with a band | ||
|
|
||
| First, you'll want to be able to find the single value for any colored band. | ||
|
|
||
| Define a function `band_value(band)` which takes a `string` of a band color. | ||
| The function returns the associated value for that color. | ||
|
|
||
| ```R | ||
| band_value("green") | ||
| # => [1] 5 | ||
|
|
||
| band_value("violet") | ||
| # => [1] 7 | ||
| ``` | ||
|
|
||
| ## 3. Return the value associated with two bands | ||
|
|
||
| The first two bands on a resistor combine to form a single number. | ||
| If the first two bands have respective numbers `m` and `n`, they combine to make the single number `mn`. | ||
|
|
||
| Define a function `two_band_value(bands)` which takes a `vector` of `string`s with two band colors. | ||
| The function returns the associated value for the combination of those two colors. | ||
|
|
||
| ```R | ||
| two_band_value(c("yellow", "red")) | ||
| # => [1] 42 | ||
|
|
||
| two_band_value(c("green", "violet")) | ||
| # => [1] 57 | ||
| ``` | ||
|
|
||
| ## 4. Return the ohms associated with all three bands | ||
|
|
||
| Finally, you'll need to find the full ohm rating using the first two bands in concert with the third. | ||
| To do that you'll need to find the two-band value and a multiplier. | ||
| The multiplier can be thought of as how many zeros are tacked onto the two-band value. | ||
|
|
||
| Define a function `ohms(bands)` which takes a `vector` of `string`s with three band colors. | ||
| The function returns the associated ohm rating for the combination of the three colors. | ||
|
|
||
| ```R | ||
| ohms(c("yellow", "red", "orange")) | ||
| # => [1] 42000 | ||
|
|
||
| ohms(c("green", "violet", "yellow")) | ||
| # => [1] 570000 | ||
| ``` | ||
|
|
||
| [e-color-code]: https://en.wikipedia.org/wiki/Electronic_color_code |
67 changes: 67 additions & 0 deletions
67
exercises/concept/named-resistor-color/.docs/introduction.md
This file contains hidden or 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,67 @@ | ||
| # Introduction | ||
|
|
||
| Vectors can have named elements, which sometimes makes them more convenient to work with. | ||
|
|
||
| ## Creation | ||
|
|
||
| There are three ways to add names to a vector. | ||
|
|
||
| 1) At vector creation time | ||
|
|
||
| ```R | ||
| > work_days <- c(Mon = TRUE, Tue = TRUE, Wed = TRUE, Thu = TRUE, Fri = TRUE, Sat = FALSE, Sun = FALSE) | ||
| > work_days | ||
| Mon Tue Wed Thu Fri Sat Sun | ||
| TRUE TRUE TRUE TRUE TRUE FALSE FALSE | ||
| ``` | ||
|
|
||
| 2) By assigning a character vector to `names()` | ||
|
|
||
| ```R | ||
| > months <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) | ||
| > names(months) <- month.abb | ||
| > months | ||
| Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec | ||
| 31 28 31 30 31 30 31 31 30 31 30 31 | ||
| ``` | ||
|
|
||
| 3) With `setNames()` | ||
|
|
||
| ```R | ||
| > months <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) | ||
| > setNames(months, month.name) | ||
| January February March April May June July August September October November December | ||
| 31 28 31 30 31 30 31 31 30 31 30 31 | ||
| ``` | ||
|
|
||
| ## Working with names | ||
|
|
||
| The `names()` function can retrieve names as well as set them. | ||
|
|
||
| ```R | ||
| > names(months) <- month.abb | ||
| > names(months)[1:3] | ||
| [1] "Jan" "Feb" "Mar" | ||
| ``` | ||
|
|
||
| A name can be used in place of the position index, with quotes required in this case. | ||
|
|
||
| ```R | ||
| > months[c("Jul", "Aug")] | ||
| Jul Aug | ||
| 31 31 | ||
| ``` | ||
|
|
||
| For such indexing to work correctly, it is best to ensure that names are unique and non-missing. | ||
| However, R does not enforce uniqueness. | ||
|
|
||
| The usual vector operations still work, and names usually will be preserved if that makes sense. | ||
|
|
||
| ```R | ||
| > months[months == 30] | ||
| Apr Jun Sep Nov | ||
| 30 30 30 30 | ||
|
|
||
| > sum(months) | ||
| [1] 365 # no meaningful names possible | ||
| ``` |
This file contains hidden or 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,17 @@ | ||
| { | ||
| "authors": [ | ||
| "depial" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "named-resistor-color.R" | ||
| ], | ||
| "test": [ | ||
| "test_named-resistor-color.R" | ||
| ], | ||
| "exemplar": [ | ||
| ".meta/exemplar.R" | ||
| ] | ||
| }, | ||
| "blurb": "Use named vectors to reason about resistor colors" | ||
| } |
This file contains hidden or 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,24 @@ | ||
| # Design | ||
|
|
||
| ## Goal | ||
|
|
||
| The goal of this exercise is to teach the student the use of `names-attribute` in R. | ||
|
|
||
| ## Learning objectives | ||
|
|
||
| - Know the ways to create a named vector. | ||
| - Know the how to access elements in a named vector via name. | ||
| - Know how to unname an object. | ||
|
|
||
| ## Out of scope | ||
|
|
||
| - Removing named objects from a named vector. | ||
| - Lists, tibbles, dataframes, etc. | ||
|
|
||
| ## Concepts | ||
|
|
||
| - `names-attribute` | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - `vector-filtering` |
This file contains hidden or 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,15 @@ | ||
| colors <- c("black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white") | ||
| resistor_bands <- 0:9 | ||
| names(resistor_bands) <- colors | ||
|
|
||
| band_value <- function(band) { | ||
| unname(resistor_bands[band]) | ||
| } | ||
|
|
||
| two_band_value <- function(bands) { | ||
| sum(c(10, 1) * resistor_bands[bands]) | ||
| } | ||
|
|
||
| ohms <- function(bands) { | ||
| two_band_value(bands[1:2]) * 10^band_value(bands[3]) | ||
| } |
10 changes: 10 additions & 0 deletions
10
exercises/concept/named-resistor-color/named-resistor-color.R
This file contains hidden or 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,10 @@ | ||
| resistor_bands <- NULL | ||
|
|
||
| band_value <- function(band) { | ||
| } | ||
|
|
||
| two_band_value <- function(bands) { | ||
| } | ||
|
|
||
| ohms <- function(bands) { | ||
| } |
43 changes: 43 additions & 0 deletions
43
exercises/concept/named-resistor-color/test_named-resistor-color.R
This file contains hidden or 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,43 @@ | ||
| source("./named-resistor-color.R") | ||
| library(testthat) | ||
|
|
||
| # 1. Define a named vector | ||
|
|
||
| test_that("1. Define the named vector resistor_values", { | ||
| expect_length(resistor_bands, 10) | ||
| expected_names <- c("black", "brown", | ||
| "red", "orange", | ||
| "yellow", "green", | ||
| "blue", "violet", | ||
| "grey", "white") | ||
| expect_named(resistor_bands, expected_names) | ||
| expect_equal(order(resistor_bands), 1:10) | ||
| }) | ||
|
|
||
| # 2. The `band_value` function | ||
|
|
||
| test_that("2. band_value returns the correct value", { | ||
| expect_equal(band_value("black"), 0) | ||
| expect_equal(band_value("red"), 2) | ||
| expect_equal(band_value("green"), 5) | ||
| expect_equal(band_value("white"), 9) | ||
| }) | ||
|
|
||
| # 3. The `two_band_value` function | ||
|
|
||
| test_that("3. two_band_value returns the correct value", { | ||
| expect_equal(two_band_value(c("yellow", "red")), 42) | ||
| expect_equal(two_band_value(c("orange", "brown")), 31) | ||
| expect_equal(two_band_value(c("grey", "blue")), 86) | ||
| expect_equal(two_band_value(c("violet", "green")), 75) | ||
| expect_equal(two_band_value(c("black", "white")), 9) | ||
| }) | ||
|
|
||
| # 4. The `ohms` function | ||
|
|
||
| test_that("4. ohms returns the correct value", { | ||
| expect_equal(ohms(c("yellow", "red", "black")), 42) | ||
| expect_equal(ohms(c("green", "white", "red")), 5900) | ||
| expect_equal(ohms(c("red", "brown", "orange")), 21000) | ||
| expect_equal(ohms(c("blue", "orange", "green")), 6300000) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.