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:
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.
The colors are encoded as follows:
Create the named vector, resistor_bands, which should have the colors as the names and the numbers as the values.
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.
band_value("green")
# => [1] 5
band_value("violet")
# => [1] 7The 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 strings with two band colors.
The function returns the associated value for the combination of those two colors.
two_band_value(c("yellow", "red"))
# => [1] 42
two_band_value(c("green", "violet"))
# => [1] 57Finally, 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 strings with three band colors.
The function returns the associated ohm rating for the combination of the three colors.
ohms(c("yellow", "red", "orange"))
# => [1] 42000
ohms(c("green", "violet", "yellow"))
# => [1] 570000If 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:
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.
The colors are encoded as follows:
Create the named vector, resistor_bands, which should have the colors as the names and the numbers as the values.
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.
band_value("green")
# => [1] 5
band_value("violet")
# => [1] 7The 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 strings with two band colors.
The function returns the associated value for the combination of those two colors.
two_band_value(c("yellow", "red"))
# => [1] 42
two_band_value(c("green", "violet"))
# => [1] 57Finally, 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 strings with three band colors.
The function returns the associated ohm rating for the combination of the three colors.
ohms(c("yellow", "red", "orange"))
# => [1] 42000
ohms(c("green", "violet", "yellow"))
# => [1] 570000