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.
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.
The program will take color names as input and output a two digit number, even if the input is more than two colors!
The band 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
From the example above: brown-green should return 15 brown-green-violet should return 15 too, ignoring the third color.
** Solution
Remember, it only prints the first two values.
#+begin_src bash
declare -A colors
colors['black']=0
colors['brown']=1
colors['red']=2
colors['orange']=3
colors['yellow']=4
colors['green']=5
colors['blue']=6
colors['violet']=7
colors['grey']=8
colors['white']=9
function errorf {
printf "$@\n" && exit 1
}
output=""
for color in ${@:1}; do
test ${colors[$color]} || errorf "invalid color"
output="$output$(printf ${colors[$color]})"
done
echo ${output:0:2}
#+end_src
* Resistor color trio
If you want to build something using a Raspberry Pi, you'll probably use resistors. For this exercise, you need to know only three 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 acts as a digit of a number. For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15. In this exercise, you are going to create a helpful program so that you don't have to remember the values of the bands. The program will take 3 colors as input, and outputs the correct value, in ohms. The color bands are encoded as follows:
In resistor-color duo you decoded the first two colors. For instance: orange-orange got the main value 33. The third color stands for how many zeros need to be added to the main value. The main value plus the zeros gives us a value in ohms. For the exercise it doesn't matter what ohms really are. For example:
(If Math is your thing, you may want to think of the zeros as exponents of 10. If Math is not your thing, go with the zeros. It really is the same thing, just in plain English instead of Math lingo.)
This exercise is about translating the colors into a label:
#+begin_example
"... ohms"
#+end_example
So an input of "orange", "orange", "black" should return:
#+begin_example
"33 ohms"
#+end_example
When we get more than a thousand ohms, we say "kiloohms". That's similar to saying "kilometer" for 1000 meters, and "kilograms" for 1000 grams. So an input of "orange", "orange", "orange" should return:
#+begin_example
"33 kiloohms"
#+end_example
** Solution
Initially not happy with the solution as I feel it's too hacky.
Indeed, that feeling is verified when I saw [[https://exercism.io/tracks/bash/exercises/resistor-color-trio/solutions/56beec9e73814d84914335d9ff58d121][this solution]].
It's pretty nice and learnt a few tricks from that code, too — that other assignment operators exist (e.g., =/==, =*==), test if a variable is set with ~test -v~, and a simple separation of functions.
#+begin_src bash
declare -A colors
colors['black']=0
colors['brown']=1
colors['red']=2
colors['orange']=3
colors['yellow']=4
colors['green']=5
colors['blue']=6
colors['violet']=7
colors['grey']=8
colors['white']=9
function errorf {
printf "$@\n" && exit 1
}
output=""
for color in "$1" "$2"; do
test ${colors[$color]} || errorf "invalid color"
output="$output$(printf ${colors[$color]})"
done
# If there's a zero in front, it will be recognized as an octal so better be careful with that.
output=$(echo $output | sed --regexp-extended "s|^0||")
test ${colors[$3]} || errorf "invalid color"
multiplier=$((10 ** ${colors[$3]}))
resistance=$(($output * $multiplier))
# The arbitrary limits are based from the test suite.
Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the modulo operation.
The rules of raindrops are that if a given number:
- has 3 as a factor, add 'Pling' to the result.
- has 5 as a factor, add 'Plang' to the result.
- has 7 as a factor, add 'Plong' to the result.
- does not have any of 3, 5, or 7 as a factor, the result should be the digits of the number.
Examples:
- 28 has 7 as a factor, but not 3 or 5, so the result would be "Plong".
- 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang".
- 34 is not factored by 3, 5, or 7, so the result would be "34".
** Initial working solution
After finding out that Bash arithmetic expression exists, we'll have to use the modulo operator.