initial commit
This commit is contained in:
91
bash/resistor-color-trio/README.md
Normal file
91
bash/resistor-color-trio/README.md
Normal file
@@ -0,0 +1,91 @@
|
||||
# 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:
|
||||
|
||||
* Black: 0
|
||||
* Brown: 1
|
||||
* Red: 2
|
||||
* Orange: 3
|
||||
* Yellow: 4
|
||||
* Green: 5
|
||||
* Blue: 6
|
||||
* Violet: 7
|
||||
* Grey: 8
|
||||
* White: 9
|
||||
|
||||
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:
|
||||
|
||||
- orange-orange-black would be 33 and no zeros, which becomes 33 ohms.
|
||||
- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms.
|
||||
- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms.
|
||||
|
||||
(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:
|
||||
|
||||
> "... ohms"
|
||||
|
||||
So an input of `"orange", "orange", "black"` should return:
|
||||
|
||||
> "33 ohms"
|
||||
|
||||
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:
|
||||
|
||||
> "33 kiloohms"
|
||||
|
||||
|
||||
Run the tests with:
|
||||
|
||||
```bash
|
||||
bats resistor_color_trio_test.sh
|
||||
```
|
||||
|
||||
After the first test(s) pass, continue by commenting out or removing the
|
||||
`[[ $BATS_RUN_SKIPPED == true ]] || skip`
|
||||
annotations prepending other tests.
|
||||
|
||||
To run all tests, including the ones with `skip` annotations, run:
|
||||
|
||||
```bash
|
||||
BATS_RUN_SKIPPED=true bats resistor_color_trio_test.sh
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1549](https://github.com/exercism/problem-specifications/issues/1549)
|
||||
|
||||
|
||||
## External utilities
|
||||
`Bash` is a language to write "scripts" -- programs that can call
|
||||
external tools, such as
|
||||
[`sed`](https://www.gnu.org/software/sed/),
|
||||
[`awk`](https://www.gnu.org/software/gawk/),
|
||||
[`date`](https://www.gnu.org/software/coreutils/manual/html_node/date-invocation.html)
|
||||
and even programs written in other programming languages,
|
||||
like [`Python`](https://www.python.org/).
|
||||
This track does not restrict the usage of these utilities, and as long
|
||||
as your solution is portable between systems and does not require
|
||||
installation of third party applications, feel free to use them to solve
|
||||
the exercise.
|
||||
|
||||
For an extra challenge, if you would like to have a better understanding
|
||||
of the language, try to re-implement the solution in pure `Bash`,
|
||||
without using any external tools. Note that there are some types of
|
||||
problems that bash cannot solve, such as performing floating point
|
||||
arithmetic and manipulating dates: for those, you must call out to an
|
||||
external tool.
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others
|
||||
have completed the exercise.
|
93
bash/resistor-color-trio/resistor_color_trio.sh
Executable file
93
bash/resistor-color-trio/resistor_color_trio.sh
Executable file
@@ -0,0 +1,93 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# V1: initial version
|
||||
# V2: merged power tables, add index one, improved unit str calculation
|
||||
|
||||
# set to mask to enable logs. 0: none, 255: all
|
||||
((debug=2#00000000))
|
||||
#((debug=2#00011111))
|
||||
# $1: log level (mask), then strings to display.
|
||||
debug () {
|
||||
(( debug & $1 )) && shift && echo Line ${BASH_LINENO[0]}: "${@}" >&2
|
||||
}
|
||||
|
||||
die () {
|
||||
echo "${@}" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
usage() {
|
||||
die "usage: resistor_color_trio.sh color1 [color2 [color3]]"
|
||||
}
|
||||
|
||||
# not sure here if 1 or 2 colors only should be accepted (not in test cases).
|
||||
# I will assume yes, it looks more coherent, even if out of scope:
|
||||
# "resistor_color_trio.sh Black" will return 0
|
||||
# "resistor_color_trio.sh yellow violet" will return 47
|
||||
# also: (not in test cases), no args will return an error.
|
||||
# (in test case), extra colors are ignored, I don't like it, personally
|
||||
(( $# == 0 )) && usage
|
||||
|
||||
# value/power of different colors
|
||||
declare -A colors=([black]=0 [brown]=1 [red]=2 [orange]=3 [yellow]=4
|
||||
[green]=5 [blue]=6 [violet]=7 [grey]=8 [white]=9)
|
||||
|
||||
# available multiplicators (powers of 10).
|
||||
declare -a powers=([9]=giga [6]=mega [3]=kilo)
|
||||
idx=("${!powers[@]}") # keys copy for reverse loop
|
||||
|
||||
params=( ${@,,} ) # converting to lower case
|
||||
|
||||
(( result=0 )) # final number to display
|
||||
|
||||
# I would have preferred to throw an error if $# > 3, instead of ignoring
|
||||
# extra args...
|
||||
for ((i=0; i<3 && i<${#params[@]}; ++i)); do
|
||||
color="${params[$i]}" # color name
|
||||
val="${colors[$color]}" # color value
|
||||
|
||||
[[ -z "$val" ]] && die "$color: invalid color."
|
||||
case $i in
|
||||
0) (( result=val ))
|
||||
debug 2 "new color 1 $color/$val. result=$result"
|
||||
;;
|
||||
# probably case 0 and 1 could be merged. Easier to read for me
|
||||
# by separating them.
|
||||
1) (( result*=10 ))
|
||||
(( result+=val ))
|
||||
debug 2 "new color 2 $color/$val. result=$result"
|
||||
;;
|
||||
2) (( result *= 10 ** val ))
|
||||
debug 2 "new color 3 $color/$val. result=$result"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# we have final number, scaling to corresponding strings.
|
||||
scalestr="" # "giga", etc...
|
||||
|
||||
if (( result )); then
|
||||
for (( i = ${#idx[@]} - 1; i >= 0; i-- )); do # reverse loop by value
|
||||
j=${idx[$i]} # also actual power (9, 6, 3)
|
||||
(( power = 10 ** $j )) # actual value
|
||||
debug 8 $i $j ${powers[$j]} $power $result
|
||||
if ! (( result % power )); then # found power
|
||||
scalestr=${powers[$j]}
|
||||
(( result /= power ))
|
||||
break
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
echo "$result ${scalestr}ohms"
|
||||
|
||||
exit 0
|
||||
|
||||
# emacs/vim settings below.
|
||||
# Local Variables:
|
||||
# sh-basic-offset: 4
|
||||
# indent-tabs-mode: nil
|
||||
# comment-column: 50
|
||||
# fill-column: 80
|
||||
# End:
|
||||
# vim: set tabstop=4 expandtab:
|
98
bash/resistor-color-trio/resistor_color_trio.sh.1
Executable file
98
bash/resistor-color-trio/resistor_color_trio.sh.1
Executable file
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# set to mask to enable logs. 0: none, 255: all
|
||||
# ((debug=2#00011111))
|
||||
((debug=2#00000000))
|
||||
|
||||
# $1: log level (mask), then strings to display.
|
||||
debug () {
|
||||
(( debug & $1 )) && shift && echo "${@}" >&2
|
||||
}
|
||||
|
||||
die () {
|
||||
echo "${@}" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
usage() {
|
||||
die "usage: resistor_color_trio.sh color1 [color2 [color3]]"
|
||||
}
|
||||
|
||||
# value/power of different colors
|
||||
declare -A colors=([black]=0 [brown]=1 [red]=2 [orange]=3 [yellow]=4
|
||||
[green]=5 [blue]=6 [violet]=7 [grey]=8 [white]=9)
|
||||
# available multiplicators (powers of 10)
|
||||
declare -a powers=(9 6 3)
|
||||
# corresponding strings
|
||||
declare -a powernames=(giga mega kilo)
|
||||
|
||||
# not sure here if 1 or 2 colors only should be accepted (not in test cases).
|
||||
# I will assume yes, it looks more coherent, even if out of scope:
|
||||
# "resistor_color_trio.sh Black" will return 0
|
||||
# "resistor_color_trio.sh yellow violet" will return 47
|
||||
# also: (not in test cases), no args will return an error.
|
||||
# (in test case), extra colors are ignored, I don't like it, personally
|
||||
(( $# == 0 )) && usage
|
||||
|
||||
scalestr="" # "giga", etc...
|
||||
(( result=0 )) # number to display
|
||||
|
||||
debug 64 "params=${#params[@]} ${params[@]}"
|
||||
|
||||
# converting to lower case
|
||||
params=( ${@,,} )
|
||||
debug 2 "#params=${#params[@]} ${params[@]}"
|
||||
|
||||
# I would have preferred to throw an error if $# > 3, instead of ignoring
|
||||
# extra args...
|
||||
for ((i=0; i<3 && i<${#params[@]}; ++i)); do
|
||||
color="${params[$i]}" # color name
|
||||
val="${colors[$color]}" # color value
|
||||
|
||||
debug 8 "i=$i - color=$color - val=$val"
|
||||
|
||||
debug 4 val="$val" color="${color[$i]}"
|
||||
[[ -z "$val" ]] && die "$color: invalid color."
|
||||
case $i in
|
||||
0) (( result=val ))
|
||||
debug 2 "new color 1 $color/$val. result=$result"
|
||||
;;
|
||||
|
||||
# probably case 0 and 1 could be merged. Easier to read for me
|
||||
# separating them.
|
||||
1) (( result*=10 ))
|
||||
(( result+=val ))
|
||||
debug 2 "new color 2 $color/$val. result=$result"
|
||||
;;
|
||||
|
||||
2) (( result *= 10 ** val ))
|
||||
debug 2 "new color 3 $color/$val. result=$result"
|
||||
|
||||
# we have final number, scaling to multiple strings.
|
||||
for (( j=0; result && j<${#powers[@]}; ++j )); do
|
||||
(( pow = 10 ** ${powers[j]} ))
|
||||
debug 8 "val=$val result=$result j=$j pow=$pow"
|
||||
if ! (( result && result % pow )); then
|
||||
((result /= pow))
|
||||
scalestr=${powernames[$j]}
|
||||
break
|
||||
fi
|
||||
done
|
||||
;;
|
||||
esac
|
||||
done
|
||||
debug 4 res="$result" scalestr="$scalestr"
|
||||
|
||||
echo "$result ${scalestr}ohms"
|
||||
|
||||
exit 0
|
||||
|
||||
# emacs/vim settings below.
|
||||
|
||||
# Local Variables:
|
||||
# sh-basic-offset: 4
|
||||
# indent-tabs-mode: nil
|
||||
# comment-column: 60
|
||||
# fill-column: 80
|
||||
# End:
|
||||
# vim: set tabstop=4 expandtab:
|
99
bash/resistor-color-trio/resistor_color_trio.sh.sav
Executable file
99
bash/resistor-color-trio/resistor_color_trio.sh.sav
Executable file
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# V1: initial version
|
||||
# V2: merged the
|
||||
|
||||
# set to mask to enable logs. 0: none, 255: all
|
||||
# ((debug=2#00011111))
|
||||
((debug=2#00000000))
|
||||
|
||||
# $1: log level (mask), then strings to display.
|
||||
debug () {
|
||||
(( debug & $1 )) && shift && echo "${@}" >&2
|
||||
}
|
||||
|
||||
die () {
|
||||
echo "${@}" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
usage() {
|
||||
die "usage: resistor_color_trio.sh color1 [color2 [color3]]"
|
||||
}
|
||||
|
||||
# value/power of different colors
|
||||
declare -A colors=([black]=0 [brown]=1 [red]=2 [orange]=3 [yellow]=4
|
||||
[green]=5 [blue]=6 [violet]=7 [grey]=8 [white]=9)
|
||||
# available multiplicators (powers of 10)
|
||||
declare -a powers=(9 6 3)
|
||||
|
||||
scalestr="" # "giga", etc...
|
||||
(( result=0 )) # number to display
|
||||
# corresponding strings
|
||||
declare -a powernames=(giga mega kilo)
|
||||
|
||||
# not sure here if 1 or 2 colors only should be accepted (not in test cases).
|
||||
# I will assume yes, it looks more coherent, even if out of scope:
|
||||
# "resistor_color_trio.sh Black" will return 0
|
||||
# "resistor_color_trio.sh yellow violet" will return 47
|
||||
# also: (not in test cases), no args will return an error.
|
||||
# (in test case), extra colors are ignored, I don't like it, personally
|
||||
(( $# == 0 )) && usage
|
||||
|
||||
debug 64 "params=${#params[@]} ${params[@]}"
|
||||
|
||||
# converting to lower case, adding default values if args missing
|
||||
params=( ${@,,} black black black )
|
||||
debug 2 "#params=${#params[@]} ${params[@]}"
|
||||
|
||||
# I would have preferred to throw an error if $# > 3, instead of ignoring
|
||||
# extra args...
|
||||
for ((i=0; i<3 && i<${#params[@]}; ++i)); do
|
||||
color="${params[$i]}" # color name
|
||||
val="${colors[$color]}" # color value
|
||||
|
||||
debug 4 val="$val" color="${color[$i]}"
|
||||
[[ -z "$val" ]] && die "$color: invalid color."
|
||||
case $i in
|
||||
0) (( result=val ))
|
||||
debug 2 "new color 1 $color/$val. result=$result"
|
||||
;;
|
||||
# probably case 0 and 1 could be merged. Easier to read for me
|
||||
# by separating them.
|
||||
1) (( result*=10 ))
|
||||
(( result+=val ))
|
||||
debug 2 "new color 2 $color/$val. result=$result"
|
||||
;;
|
||||
2) (( result *= 10 ** val ))
|
||||
debug 2 "new color 3 $color/$val. result=$result"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# we have final number, scaling to corresponding strings.
|
||||
for (( j=0; result && j<${#powers[@]}; ++j )); do
|
||||
(( pow = 10 ** ${powers[j]} ))
|
||||
debug 8 "val=$val result=$result j=$j pow=$pow"
|
||||
if ! (( result && result % pow )); then
|
||||
((result /= pow))
|
||||
scalestr=${powernames[$j]}
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
debug 4 res="$result" scalestr="$scalestr"
|
||||
|
||||
echo "$result ${scalestr}ohms"
|
||||
|
||||
exit 0
|
||||
|
||||
# emacs/vim settings below.
|
||||
|
||||
# Local Variables:
|
||||
# sh-basic-offset: 4
|
||||
# indent-tabs-mode: nil
|
||||
# comment-column: 50
|
||||
# fill-column: 80
|
||||
# End:
|
||||
# vim: set tabstop=4 expandtab:
|
Reference in New Issue
Block a user