diff --git a/config.json b/config.json index b183ee9..3ca691c 100644 --- a/config.json +++ b/config.json @@ -225,6 +225,14 @@ "prerequisites": [], "difficulty": 3 }, + { + "slug": "resistor-color-trio", + "name": "Resistor Color Trio", + "uuid": "d7cb1f1e-0db4-4f50-ac36-8b5ec07ab04a", + "practices": [], + "prerequisites": [], + "difficulty": 3 + }, { "slug": "rotational-cipher", "name": "Rotational Cipher", diff --git a/exercises/practice/resistor-color-trio/.docs/instructions.append.md b/exercises/practice/resistor-color-trio/.docs/instructions.append.md new file mode 100644 index 0000000..edf6895 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.docs/instructions.append.md @@ -0,0 +1,7 @@ +## Futhark specific notes + +Any spaces should appear to the left of the value, or to the right of the unit: + +``` +{ value = " 33", unit = "ohms " } +``` 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..b41b04e --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "resistor_color_trio.fut" + ], + "test": [ + "test.fut" + ], + "example": [ + ".meta/example.fut" + ] + }, + "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.fut b/exercises/practice/resistor-color-trio/.meta/example.fut new file mode 100644 index 0000000..47b315a --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/example.fut @@ -0,0 +1,44 @@ +type resistance = { value: [3]u8, unit: [8]u8 } + +def equal [m] [n] (first: [m]u8) (second: [n]u8): bool = + if m != n then false else + let same = loop same = true for i < n do + same && (first[i] == second[i]) + in + same + +def colorCode (color: []u8): i32 = + if equal color "black" then 0 else + if equal color "brown" then 1 else + if equal color "red" then 2 else + if equal color "orange" then 3 else + if equal color "yellow" then 4 else + if equal color "green" then 5 else + if equal color "blue" then 6 else + if equal color "violet" then 7 else + if equal color "grey" then 8 else + if equal color "white" then 9 else + assert false 0 + +def label_value (first: u8) (second: u8) (remainder: i32): [3]u8 = + if remainder == 0 then ( + if second == '0' then [ ' ', ' ', first ] else [ first, '.', second ] + ) else if remainder == 1 then ( + if first == '0' then [ ' ', ' ', second ] else [ ' ', first, second ] + ) else if remainder == 2 then ( + [ first, second, '0' ] + ) else assert false " " + +def label_unit (quotient: i32): [8]u8 = + if quotient == 0 then "ohms " else + if quotient == 1 then "kiloohms" else + if quotient == 2 then "megaohms" else + if quotient == 3 then "gigaohms" else + assert false " " + +def label (first: []u8) (second: []u8) (third: []u8): resistance = + let first = u8.i32 ((colorCode first) + '0') + let second = u8.i32 ((colorCode second) + '0') + let third = (colorCode third) + 1 + in + { value = label_value first second (third % 3), unit = label_unit (third / 3) } 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.fut b/exercises/practice/resistor-color-trio/resistor_color_trio.fut new file mode 100644 index 0000000..c4b2417 --- /dev/null +++ b/exercises/practice/resistor-color-trio/resistor_color_trio.fut @@ -0,0 +1,3 @@ +type resistance = { value: [3]u8, unit: [8]u8 } + +def label (first: []u8) (second: []u8) (third: []u8): resistance = ??? diff --git a/exercises/practice/resistor-color-trio/test.fut b/exercises/practice/resistor-color-trio/test.fut new file mode 100644 index 0000000..88e9bb9 --- /dev/null +++ b/exercises/practice/resistor-color-trio/test.fut @@ -0,0 +1,76 @@ +import "resistor_color_trio" + +-- Orange and orange and black +-- == +-- input { "orange" "orange" "black" } +-- output { " 33 ohms " } + +-- Blue and grey and brown +-- == +-- input { "blue" "grey" "brown" } +-- output { "680 ohms " } + +-- Red and black and red +-- == +-- input { "red" "black" "red" } +-- output { " 2 kiloohms" } + +-- Green and brown and orange +-- == +-- input { "green" "brown" "orange" } +-- output { " 51 kiloohms" } + +-- Yellow and violet and yellow +-- == +-- input { "yellow" "violet" "yellow" } +-- output { "470 kiloohms" } + +-- Blue and violet and blue +-- == +-- input { "blue" "violet" "blue" } +-- output { " 67 megaohms" } + +-- Minimum possible value +-- == +-- input { "black" "black" "black" } +-- output { " 0 ohms " } + +-- Maximum possible value +-- == +-- input { "white" "white" "white" } +-- output { " 99 gigaohms" } + +-- First two colors make an invalid octal number +-- == +-- input { "black" "grey" "black" } +-- output { " 8 ohms " } + +-- Ignore extra colors +-- == +-- input { "blue" "green" "yellow" } +-- output { "650 kiloohms" } + +-- Orange and orange and red +-- == +-- input { "orange" "orange" "red" } +-- output { "3.3 kiloohms" } + +-- Orange and orange and green +-- == +-- input { "orange" "orange" "green" } +-- output { "3.3 megaohms" } + +-- White and white and violet +-- == +-- input { "white" "white" "violet" } +-- output { "990 megaohms" } + +-- White and white and grey +-- == +-- input { "white" "white" "grey" } +-- output { "9.9 gigaohms" } + +def main (first: []u8) (second: []u8) (third: []u8): []u8 = + let result = label first second third + in + result.value ++ " " ++ result.unit diff --git a/generators/exercises/resistor_color_trio.py b/generators/exercises/resistor_color_trio.py new file mode 100644 index 0000000..458152b --- /dev/null +++ b/generators/exercises/resistor_color_trio.py @@ -0,0 +1,51 @@ +MAIN = """\ +def main (first: []u8) (second: []u8) (third: []u8): []u8 = + let result = label first second third + in + result.value ++ " " ++ result.unit +""" + + +def extra_cases(): + # Test cases from the Julia track. + return [ + { + "description": "Orange and orange and red", + "property": "label", + "input": {"colors": ["orange", "orange", "red"]}, + "expected": {"value": "3.3", "unit": "kiloohms"}, + }, + { + "description": "Orange and orange and green", + "property": "label", + "input": {"colors": ["orange", "orange", "green"]}, + "expected": {"value": "3.3", "unit": "megaohms"}, + }, + { + "description": "White and white and violet", + "property": "label", + "input": {"colors": ["white", "white", "violet"]}, + "expected": {"value": "990", "unit": "megaohms"}, + }, + { + "description": "White and white and grey", + "property": "label", + "input": {"colors": ["white", "white", "grey"]}, + "expected": {"value": "9.9", "unit": "gigaohms"}, + }, + ] + + +def gen_test_case(prop, description, inp, expected, f): + colors = inp["colors"] + value = expected["value"] + unit = expected["unit"] + + f.write(f"-- {description}\n") + f.write("-- ==\n") + f.write(f'-- input {{ "{colors[0]}" "{colors[1]}" "{colors[2]}" }}\n') + f.write(f'-- output {{ "{value:>3} {unit:<8}" }}\n\n') + + +def gen_main(f): + f.write(MAIN)