initial commit
This commit is contained in:
68
bash/resistor-color-duo/README.md
Normal file
68
bash/resistor-color-duo/README.md
Normal file
@@ -0,0 +1,68 @@
|
||||
# Resistor Color Duo
|
||||
|
||||
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 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 two colors as input, and output the correct number.
|
||||
|
||||
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
|
||||
|
||||
|
||||
Run the tests with:
|
||||
|
||||
```bash
|
||||
bats resistor_color_duo_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_duo_test.sh
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1464](https://github.com/exercism/problem-specifications/issues/1464)
|
||||
|
||||
|
||||
## 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.
|
39
bash/resistor-color-duo/resistor_color_duo.sh
Executable file
39
bash/resistor-color-duo/resistor_color_duo.sh
Executable file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
usage () {
|
||||
if [[ $# == 1 ]]; then
|
||||
echo "$1: invalid color."
|
||||
else
|
||||
echo "usage: resistor_color.sh color1 color2"
|
||||
fi
|
||||
exit 1
|
||||
}
|
||||
|
||||
declare -A colors
|
||||
colors=(
|
||||
[black]=0
|
||||
[brown]=1
|
||||
[red]=2
|
||||
[orange]=3
|
||||
[yellow]=4
|
||||
[green]=5
|
||||
[blue]=6
|
||||
[violet]=7
|
||||
[grey]=8
|
||||
[white]=9
|
||||
)
|
||||
result=""
|
||||
|
||||
# not sure here if 1 color only should be accepted (case is not in test). I assume yes:
|
||||
# "resistor_color.sh Black" will return 0
|
||||
# also (not in test cases), no args will return an error.
|
||||
[[ $# == 0 ]] && usage
|
||||
|
||||
# we will accept capitalized colors: converting to lower case
|
||||
for i in ${1,,} ${2,,}
|
||||
do
|
||||
[[ ${colors[$i]} == "" ]] && usage "$i"
|
||||
result+=${colors[$i]}
|
||||
done
|
||||
echo $result
|
||||
exit 0
|
Reference in New Issue
Block a user