diff --git a/config.json b/config.json index e372a18..d4c8ea4 100644 --- a/config.json +++ b/config.json @@ -399,6 +399,14 @@ "prerequisites": [], "difficulty": 3 }, + { + "slug": "resistor-color-trio", + "name": "Resistor Color Trio", + "uuid": "88cce490-31c7-4734-8377-38a6035bfb64", + "practices": [], + "prerequisites": [], + "difficulty": 3 + }, { "slug": "reverse-string", "name": "Reverse String", diff --git a/exercises/practice/resistor-color-trio/.docs/instructions.md b/exercises/practice/resistor-color-trio/.docs/instructions.md new file mode 100644 index 0000000..1ac5cf5 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.docs/instructions.md @@ -0,0 +1,56 @@ +# Instructions + +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 to larger resistors, a [metric prefix][metric-prefix] is used to indicate a larger magnitude of ohms, such as "kiloohms". +That is similar to saying "2 kilometers" instead of "2000 meters", or "2 kilograms" for "2000 grams". + +For example, an input of `"orange", "orange", "orange"` should return: + +> "33 kiloohms" + +[metric-prefix]: https://en.wikipedia.org/wiki/Metric_prefix diff --git a/exercises/practice/resistor-color-trio/.meta/config.json b/exercises/practice/resistor-color-trio/.meta/config.json new file mode 100644 index 0000000..d9ea508 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "resistor_color_trio.vim" + ], + "test": [ + "resistor_color_trio.vader" + ], + "example": [ + ".meta/example.vim" + ] + }, + "blurb": "Convert color codes, as used on resistors, to a human-readable label.", + "source": "Maud de Vries, Erik Schierboom", + "source_url": "https://github.com/exercism/problem-specifications/issues/1549" +} diff --git a/exercises/practice/resistor-color-trio/.meta/example.vim b/exercises/practice/resistor-color-trio/.meta/example.vim new file mode 100644 index 0000000..f687b9b --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/example.vim @@ -0,0 +1,30 @@ +let s:COLORS = [ +\ 'black', +\ 'brown', +\ 'red', +\ 'orange', +\ 'yellow', +\ 'green', +\ 'blue', +\ 'violet', +\ 'grey', +\ 'white', +\ ] + +function! Label(colors) abort + let [l:first, l:second, l:third; l:rest] = a:colors + let l:tens = index(s:COLORS, l:first) + let l:ones = index(s:COLORS, l:second) + let l:multiplier = index(s:COLORS, l:third) + let l:value = float2nr((l:tens * 10 + l:ones) * pow(10, l:multiplier)) + + if l:value < 1000 + return l:value . ' ohms' + elseif l:value < 1000000 + return l:value / 1000 . ' kiloohms' + elseif l:value < 1000000000 + return l:value / 1000000 . ' megaohms' + else + return l:value / 1000000000 . ' gigaohms' + endif +endfunction diff --git a/exercises/practice/resistor-color-trio/.meta/tests.toml b/exercises/practice/resistor-color-trio/.meta/tests.toml new file mode 100644 index 0000000..b7d45fa --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/tests.toml @@ -0,0 +1,40 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[d6863355-15b7-40bb-abe0-bfb1a25512ed] +description = "Orange and orange and black" + +[1224a3a9-8c8e-4032-843a-5224e04647d6] +description = "Blue and grey and brown" + +[b8bda7dc-6b95-4539-abb2-2ad51d66a207] +description = "Red and black and red" + +[5b1e74bc-d838-4eda-bbb3-eaba988e733b] +description = "Green and brown and orange" + +[f5d37ef9-1919-4719-a90d-a33c5a6934c9] +description = "Yellow and violet and yellow" + +[5f6404a7-5bb3-4283-877d-3d39bcc33854] +description = "Blue and violet and blue" + +[7d3a6ab8-e40e-46c3-98b1-91639fff2344] +description = "Minimum possible value" + +[ca0aa0ac-3825-42de-9f07-dac68cc580fd] +description = "Maximum possible value" + +[0061a76c-903a-4714-8ce2-f26ce23b0e09] +description = "First two colors make an invalid octal number" + +[30872c92-f567-4b69-a105-8455611c10c4] +description = "Ignore extra colors" diff --git a/exercises/practice/resistor-color-trio/resistor_color_trio.vader b/exercises/practice/resistor-color-trio/resistor_color_trio.vader new file mode 100644 index 0000000..9400423 --- /dev/null +++ b/exercises/practice/resistor-color-trio/resistor_color_trio.vader @@ -0,0 +1,39 @@ +Execute (Orange and orange and black): + let g:colors = ['orange', 'orange', 'black'] + AssertEqual '33 ohms', Label(g:colors) + +Execute (Blue and grey and brown): + let g:colors = ['blue', 'grey', 'brown'] + AssertEqual '680 ohms', Label(g:colors) + +Execute (Red and black and red): + let g:colors = ['red', 'black', 'red'] + AssertEqual '2 kiloohms', Label(g:colors) + +Execute (Green and brown and orange): + let g:colors = ['green', 'brown', 'orange'] + AssertEqual '51 kiloohms', Label(g:colors) + +Execute (Yellow and violet and yellow): + let g:colors = ['yellow', 'violet', 'yellow'] + AssertEqual '470 kiloohms', Label(g:colors) + +Execute (Blue and violet and blue): + let g:colors = ['blue', 'violet', 'blue'] + AssertEqual '67 megaohms', Label(g:colors) + +Execute (Minimum possible value): + let g:colors = ['black', 'black', 'black'] + AssertEqual '0 ohms', Label(g:colors) + +Execute (Maximum possible value): + let g:colors = ['white', 'white', 'white'] + AssertEqual '99 gigaohms', Label(g:colors) + +Execute (First two colors make an invalid octal number): + let g:colors = ['black', 'grey', 'black'] + AssertEqual '8 ohms', Label(g:colors) + +Execute (Ignore extra colors): + let g:colors = ['blue', 'green', 'yellow', 'orange'] + AssertEqual '650 kiloohms', Label(g:colors) diff --git a/exercises/practice/resistor-color-trio/resistor_color_trio.vim b/exercises/practice/resistor-color-trio/resistor_color_trio.vim new file mode 100644 index 0000000..903695d --- /dev/null +++ b/exercises/practice/resistor-color-trio/resistor_color_trio.vim @@ -0,0 +1,12 @@ +" +" Returns a formatted label showing the encoded resistance +" value with units + +" Example: +" +" :echo Label(['green', 'brown', 'orange']) +" 51 kiloohms +" +function! Label(colors) abort + " your code goes here +endfunction