Skip to content
Merged
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
28 changes: 24 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@
"switch"
],
"status": "wip"
},
{
"slug": "named-resistor-color",
"name": "Named Resistor Color",
"uuid": "f0c4e105-5db7-4e35-bfe1-694e9a1c199c",
"concepts": [
"names-attribute"
],
"prerequisites": [
"vector-filtering"
],
"status": "wip"
}
],
"practice": [
Expand Down Expand Up @@ -344,16 +356,24 @@
"slug": "resistor-color",
"name": "Resistor Color",
"uuid": "8af70f6c-85e2-41de-b5a3-20aaacb32a26",
"practices": [],
"prerequisites": [],
"practices": [
"vector-filtering"
],
"prerequisites": [
"vector-filtering"
],
"difficulty": 1
},
{
"slug": "resistor-color-duo",
"name": "Resistor Color Duo",
"uuid": "a118a447-f41d-4904-8dfa-15f45ba3a990",
"practices": [],
"prerequisites": [],
"practices": [
"vector-filtering"
],
"prerequisites": [
"vector-filtering"
],
"difficulty": 1
},
{
Expand Down
29 changes: 29 additions & 0 deletions exercises/concept/named-resistor-color/.docs/hints.md
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 exercises/concept/named-resistor-color/.docs/instructions.md
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 exercises/concept/named-resistor-color/.docs/introduction.md
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
```
17 changes: 17 additions & 0 deletions exercises/concept/named-resistor-color/.meta/config.json
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"
}
24 changes: 24 additions & 0 deletions exercises/concept/named-resistor-color/.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 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`
15 changes: 15 additions & 0 deletions exercises/concept/named-resistor-color/.meta/exemplar.R
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 exercises/concept/named-resistor-color/named-resistor-color.R
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 exercises/concept/named-resistor-color/test_named-resistor-color.R
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)
})