|
| 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 |
0 commit comments