Skip to content

Commit a1354e0

Browse files
authored
Add concept exercise named-resistor-color (#446)
* add concept exercise named-resistor-color * add prereqs for resistor-color series * fix typo * fix typos * add general heading to hints.md
1 parent 6ca95e1 commit a1354e0

9 files changed

Lines changed: 314 additions & 4 deletions

File tree

config.json

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@
115115
"switch"
116116
],
117117
"status": "wip"
118+
},
119+
{
120+
"slug": "named-resistor-color",
121+
"name": "Named Resistor Color",
122+
"uuid": "f0c4e105-5db7-4e35-bfe1-694e9a1c199c",
123+
"concepts": [
124+
"names-attribute"
125+
],
126+
"prerequisites": [
127+
"vector-filtering"
128+
],
129+
"status": "wip"
118130
}
119131
],
120132
"practice": [
@@ -344,16 +356,24 @@
344356
"slug": "resistor-color",
345357
"name": "Resistor Color",
346358
"uuid": "8af70f6c-85e2-41de-b5a3-20aaacb32a26",
347-
"practices": [],
348-
"prerequisites": [],
359+
"practices": [
360+
"vector-filtering"
361+
],
362+
"prerequisites": [
363+
"vector-filtering"
364+
],
349365
"difficulty": 1
350366
},
351367
{
352368
"slug": "resistor-color-duo",
353369
"name": "Resistor Color Duo",
354370
"uuid": "a118a447-f41d-4904-8dfa-15f45ba3a990",
355-
"practices": [],
356-
"prerequisites": [],
371+
"practices": [
372+
"vector-filtering"
373+
],
374+
"prerequisites": [
375+
"vector-filtering"
376+
],
357377
"difficulty": 1
358378
},
359379
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Hints
2+
3+
## General
4+
5+
Return values for the functions are of a specific type, so pay attention.
6+
7+
## 1. Define a named vector
8+
9+
- The necessary information for the names and values is in the instructions.
10+
- The way to construct a named vector is covered in detail in the introduction.
11+
12+
## 2. Return the value associated with a band
13+
14+
- You can use your `resistor_bands` named vector to accomplish this easily.
15+
- You need to return a numeric value alone.
16+
17+
## 3. Return the value associated with two bands
18+
19+
- Your `band_value` function can come in handy here.
20+
- Indexing hints for named vectors are in the introduction.
21+
- There are several ways to construct the number, most commonly via string manipulation or numerically.
22+
- The returned number must be a numeric value alone.
23+
24+
## 4. Return the ohms associated with all three bands
25+
26+
- Both `band_value` and `two_band_value` are useful here.
27+
- The first two values in the vector are for the two-band value, while the third is the multiplier.
28+
- Again, string manipulation or numerical means are possible to get the number.
29+
- The returned number must be a numeric value alone.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Instructions
2+
3+
If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
4+
For this exercise, you need to know two things about them:
5+
6+
- Each resistor has a resistance value.
7+
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
8+
9+
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
10+
Each band has a position and a numeric value.
11+
12+
The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
13+
14+
The third band is a multiplier.
15+
16+
In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.
17+
18+
More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article][e-color-code].
19+
20+
## 1. Define a named vector
21+
22+
The colors are encoded as follows:
23+
24+
- black: 0
25+
- brown: 1
26+
- red: 2
27+
- orange: 3
28+
- yellow: 4
29+
- green: 5
30+
- blue: 6
31+
- violet: 7
32+
- grey: 8
33+
- white: 9
34+
35+
Create the named vector, `resistor_bands`, which should have the colors as the names and the numbers as the values.
36+
37+
## 2. Return the value associated with a band
38+
39+
First, you'll want to be able to find the single value for any colored band.
40+
41+
Define a function `band_value(band)` which takes a `string` of a band color.
42+
The function returns the associated value for that color.
43+
44+
```R
45+
band_value("green")
46+
# => [1] 5
47+
48+
band_value("violet")
49+
# => [1] 7
50+
```
51+
52+
## 3. Return the value associated with two bands
53+
54+
The first two bands on a resistor combine to form a single number.
55+
If the first two bands have respective numbers `m` and `n`, they combine to make the single number `mn`.
56+
57+
Define a function `two_band_value(bands)` which takes a `vector` of `string`s with two band colors.
58+
The function returns the associated value for the combination of those two colors.
59+
60+
```R
61+
two_band_value(c("yellow", "red"))
62+
# => [1] 42
63+
64+
two_band_value(c("green", "violet"))
65+
# => [1] 57
66+
```
67+
68+
## 4. Return the ohms associated with all three bands
69+
70+
Finally, you'll need to find the full ohm rating using the first two bands in concert with the third.
71+
To do that you'll need to find the two-band value and a multiplier.
72+
The multiplier can be thought of as how many zeros are tacked onto the two-band value.
73+
74+
Define a function `ohms(bands)` which takes a `vector` of `string`s with three band colors.
75+
The function returns the associated ohm rating for the combination of the three colors.
76+
77+
```R
78+
ohms(c("yellow", "red", "orange"))
79+
# => [1] 42000
80+
81+
ohms(c("green", "violet", "yellow"))
82+
# => [1] 570000
83+
```
84+
85+
[e-color-code]: https://en.wikipedia.org/wiki/Electronic_color_code
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Introduction
2+
3+
Vectors can have named elements, which sometimes makes them more convenient to work with.
4+
5+
## Creation
6+
7+
There are three ways to add names to a vector.
8+
9+
1) At vector creation time
10+
11+
```R
12+
> work_days <- c(Mon = TRUE, Tue = TRUE, Wed = TRUE, Thu = TRUE, Fri = TRUE, Sat = FALSE, Sun = FALSE)
13+
> work_days
14+
Mon Tue Wed Thu Fri Sat Sun
15+
TRUE TRUE TRUE TRUE TRUE FALSE FALSE
16+
```
17+
18+
2) By assigning a character vector to `names()`
19+
20+
```R
21+
> months <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
22+
> names(months) <- month.abb
23+
> months
24+
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
25+
31 28 31 30 31 30 31 31 30 31 30 31
26+
```
27+
28+
3) With `setNames()`
29+
30+
```R
31+
> months <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
32+
> setNames(months, month.name)
33+
January February March April May June July August September October November December
34+
31 28 31 30 31 30 31 31 30 31 30 31
35+
```
36+
37+
## Working with names
38+
39+
The `names()` function can retrieve names as well as set them.
40+
41+
```R
42+
> names(months) <- month.abb
43+
> names(months)[1:3]
44+
[1] "Jan" "Feb" "Mar"
45+
```
46+
47+
A name can be used in place of the position index, with quotes required in this case.
48+
49+
```R
50+
> months[c("Jul", "Aug")]
51+
Jul Aug
52+
31 31
53+
```
54+
55+
For such indexing to work correctly, it is best to ensure that names are unique and non-missing.
56+
However, R does not enforce uniqueness.
57+
58+
The usual vector operations still work, and names usually will be preserved if that makes sense.
59+
60+
```R
61+
> months[months == 30]
62+
Apr Jun Sep Nov
63+
30 30 30 30
64+
65+
> sum(months)
66+
[1] 365 # no meaningful names possible
67+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"depial"
4+
],
5+
"files": {
6+
"solution": [
7+
"named-resistor-color.R"
8+
],
9+
"test": [
10+
"test_named-resistor-color.R"
11+
],
12+
"exemplar": [
13+
".meta/exemplar.R"
14+
]
15+
},
16+
"blurb": "Use named vectors to reason about resistor colors"
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Design
2+
3+
## Goal
4+
5+
The goal of this exercise is to teach the student the use of `names-attribute` in R.
6+
7+
## Learning objectives
8+
9+
- Know the ways to create a named vector.
10+
- Know the how to access elements in a named vector via name.
11+
- Know how to unname an object.
12+
13+
## Out of scope
14+
15+
- Removing named objects from a named vector.
16+
- Lists, tibbles, dataframes, etc.
17+
18+
## Concepts
19+
20+
- `names-attribute`
21+
22+
## Prerequisites
23+
24+
- `vector-filtering`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
colors <- c("black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white")
2+
resistor_bands <- 0:9
3+
names(resistor_bands) <- colors
4+
5+
band_value <- function(band) {
6+
unname(resistor_bands[band])
7+
}
8+
9+
two_band_value <- function(bands) {
10+
sum(c(10, 1) * resistor_bands[bands])
11+
}
12+
13+
ohms <- function(bands) {
14+
two_band_value(bands[1:2]) * 10^band_value(bands[3])
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resistor_bands <- NULL
2+
3+
band_value <- function(band) {
4+
}
5+
6+
two_band_value <- function(bands) {
7+
}
8+
9+
ohms <- function(bands) {
10+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
source("./named-resistor-color.R")
2+
library(testthat)
3+
4+
# 1. Define a named vector
5+
6+
test_that("1. Define the named vector resistor_values", {
7+
expect_length(resistor_bands, 10)
8+
expected_names <- c("black", "brown",
9+
"red", "orange",
10+
"yellow", "green",
11+
"blue", "violet",
12+
"grey", "white")
13+
expect_named(resistor_bands, expected_names)
14+
expect_equal(order(resistor_bands), 1:10)
15+
})
16+
17+
# 2. The `band_value` function
18+
19+
test_that("2. band_value returns the correct value", {
20+
expect_equal(band_value("black"), 0)
21+
expect_equal(band_value("red"), 2)
22+
expect_equal(band_value("green"), 5)
23+
expect_equal(band_value("white"), 9)
24+
})
25+
26+
# 3. The `two_band_value` function
27+
28+
test_that("3. two_band_value returns the correct value", {
29+
expect_equal(two_band_value(c("yellow", "red")), 42)
30+
expect_equal(two_band_value(c("orange", "brown")), 31)
31+
expect_equal(two_band_value(c("grey", "blue")), 86)
32+
expect_equal(two_band_value(c("violet", "green")), 75)
33+
expect_equal(two_band_value(c("black", "white")), 9)
34+
})
35+
36+
# 4. The `ohms` function
37+
38+
test_that("4. ohms returns the correct value", {
39+
expect_equal(ohms(c("yellow", "red", "black")), 42)
40+
expect_equal(ohms(c("green", "white", "red")), 5900)
41+
expect_equal(ohms(c("red", "brown", "orange")), 21000)
42+
expect_equal(ohms(c("blue", "orange", "green")), 6300000)
43+
})

0 commit comments

Comments
 (0)