diff --git a/config.json b/config.json index 5ac049560..183378e56 100644 --- a/config.json +++ b/config.json @@ -54,6 +54,15 @@ "difficulty": 2, "topics": ["strings", "arrays"] }, + { + "slug": "resistor-color-trio", + "name": "Resistor Color Trio", + "uuid": "9316fc6a-42d2-4b12-85e1-e8abd9ce3ae2", + "practices": [], + "prerequisites": [], + "difficulty": 3, + "topics": ["strings", "arrays"] + }, { "slug": "leap", "name": "Leap", 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 000000000..47ca9f07b --- /dev/null +++ b/exercises/practice/resistor-color-trio/.docs/instructions.md @@ -0,0 +1,45 @@ +# Description + +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" diff --git a/exercises/practice/resistor-color-trio/.eslintignore b/exercises/practice/resistor-color-trio/.eslintignore new file mode 100644 index 000000000..e3e45c396 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.eslintignore @@ -0,0 +1,12 @@ +!.meta + +# Protected or generated +.git +.vscode + +# When using npm +node_modules/* + +# Configuration files +babel.config.js +jest.config.js \ No newline at end of file diff --git a/exercises/practice/resistor-color-trio/.eslintrc b/exercises/practice/resistor-color-trio/.eslintrc new file mode 100644 index 000000000..3fbb9032c --- /dev/null +++ b/exercises/practice/resistor-color-trio/.eslintrc @@ -0,0 +1,23 @@ +{ + "root": true, + "parser": "@typescript-eslint/parser", + "parserOptions": { + "project": "./tsconfig.json", + "ecmaFeatures": { + "jsx": true + }, + "ecmaVersion": 2020, + "sourceType": "module" + }, + "extends": "@exercism/eslint-config-typescript", + "env": { + "jest": true + }, + "overrides": [ + { + "files": [".meta/proof.ci.ts", ".meta/exemplar.ts", "*.test.ts"], + "excludedFiles": ["custom.test.ts"], + "extends": "@exercism/eslint-config-typescript/maintainers" + } + ] +} 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 000000000..1b098238e --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/config.json @@ -0,0 +1,12 @@ +{ + "blurb": "Convert color codes, as used on resistors, to a human-readable label.", + "authors": ["rodmagaldi"], + "contributors": ["SleeplessByte"], + "files": { + "solution": ["resistor-color-trio.ts"], + "test": ["resistor-color-trio.test.ts"], + "example": [".meta/proof.ci.ts"] + }, + "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/proof.ci.ts b/exercises/practice/resistor-color-trio/.meta/proof.ci.ts new file mode 100644 index 000000000..6510d874a --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/proof.ci.ts @@ -0,0 +1,63 @@ +// resistor-color solution START +const COLORS = [ + 'black', + 'brown', + 'red', + 'orange', + 'yellow', + 'green', + 'blue', + 'violet', + 'grey', + 'white', +] + +const colorCode = (color: string): number => COLORS.indexOf(color) +// resistor-color solution END + +interface IUnits { + name: string + numberOfZeros: number + representation: string +} + +const UNITS: IUnits[] = [ + { + name: 'giga', + numberOfZeros: 9, + representation: '000000000', + }, + { + name: 'mega', + numberOfZeros: 6, + representation: '000000', + }, + { + name: 'kilo', + numberOfZeros: 3, + representation: '000', + }, +] + +export const decodedResistorValue = ([ + tens, + ones, + exponent, +]: string[]): string => { + const numericValue = + (colorCode(tens) * 10 + colorCode(ones)) * 10 ** colorCode(exponent) + const stringifiedValue = numericValue.toString() + + let readableUnit = '' + let readableValue: string = stringifiedValue + + for (const unit of UNITS) { + if (stringifiedValue.endsWith(unit.representation)) { + readableUnit = unit.name + readableValue = readableValue.slice(0, -unit.numberOfZeros) + break + } + } + + return `${readableValue} ${readableUnit}ohms` +} 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 000000000..beabab3df --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/tests.toml @@ -0,0 +1,18 @@ +# This is an auto-generated file. Regular comments will be removed when this +# file is regenerated. Regenerating will not touch any manually added keys, +# so comments can be added in 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" diff --git a/exercises/practice/resistor-color-trio/babel.config.js b/exercises/practice/resistor-color-trio/babel.config.js new file mode 100644 index 000000000..eca257951 --- /dev/null +++ b/exercises/practice/resistor-color-trio/babel.config.js @@ -0,0 +1,20 @@ +module.exports = { + presets: [ + [ + '@babel/preset-env', + { + targets: { + node: 'current', + }, + useBuiltIns: 'entry', + corejs: '3.17', + }, + ], + '@babel/preset-typescript', + ], + plugins: [ + '@babel/proposal-class-properties', + '@babel/proposal-object-rest-spread', + '@babel/plugin-syntax-bigint', + ], +} diff --git a/exercises/practice/resistor-color-trio/jest.config.js b/exercises/practice/resistor-color-trio/jest.config.js new file mode 100644 index 000000000..d6b552072 --- /dev/null +++ b/exercises/practice/resistor-color-trio/jest.config.js @@ -0,0 +1,19 @@ +module.exports = { + verbose: true, + projects: [''], + testMatch: [ + '**/__tests__/**/*.[jt]s?(x)', + '**/test/**/*.[jt]s?(x)', + '**/?(*.)+(spec|test).[jt]s?(x)', + ], + testPathIgnorePatterns: [ + '/(?:production_)?node_modules/', + '.d.ts$', + '/test/fixtures', + '/test/helpers', + '__mocks__', + ], + transform: { + '^.+\\.[jt]sx?$': 'babel-jest', + }, +} diff --git a/exercises/practice/resistor-color-trio/package.json b/exercises/practice/resistor-color-trio/package.json new file mode 100644 index 000000000..7a5e14e8e --- /dev/null +++ b/exercises/practice/resistor-color-trio/package.json @@ -0,0 +1,37 @@ +{ + "name": "@exercism/typescript-practice-resistor-color-trio", + "version": "1.0.0", + "description": "Exercism practice exercise on resistor-color-trio", + "author": "Katrina Owen", + "contributors": [ + "Derk-Jan Karrenbeld (https://derk-jan.com)" + ], + "private": true, + "repository": { + "type": "git", + "url": "https://github.com/exercism/typescript" + }, + "devDependencies": { + "@babel/core": "^7.15.5", + "@babel/plugin-proposal-class-properties": "^7.14.5", + "@babel/plugin-proposal-object-rest-spread": "^7.14.7", + "@babel/plugin-syntax-bigint": "^7.8.3", + "@babel/preset-env": "^7.15.4", + "@babel/preset-typescript": "^7.15.0", + "@types/jest": "^26.0.24", + "@types/node": "^14.17.14", + "@exercism/eslint-config-typescript": "^0.3.0", + "babel-jest": "^26.6.3", + "core-js": "^3.17.2", + "eslint": "^7.32.0", + "eslint-plugin-import": "^2.24.2", + "jest": "^26.6.3", + "typescript": "^4.4.2" + }, + "scripts": { + "test": "yarn lint:types && jest --no-cache", + "lint": "yarn lint:types && yarn lint:ci", + "lint:types": "yarn tsc --noEmit -p .", + "lint:ci": "eslint . --ext .tsx,.ts" + } +} diff --git a/exercises/practice/resistor-color-trio/resistor-color-trio.test.ts b/exercises/practice/resistor-color-trio/resistor-color-trio.test.ts new file mode 100644 index 000000000..04a27ddeb --- /dev/null +++ b/exercises/practice/resistor-color-trio/resistor-color-trio.test.ts @@ -0,0 +1,29 @@ +import { decodedResistorValue } from './resistor-color-trio' + +describe('Resistor Colors', () => { + test('Orange and orange and black', () => { + expect(decodedResistorValue(['orange', 'orange', 'black'])).toEqual( + '33 ohms' + ) + }) + + xtest('Blue and grey and brown', () => { + expect(decodedResistorValue(['blue', 'grey', 'brown'])).toEqual('680 ohms') + }) + + xtest('Red and black and red', () => { + expect(decodedResistorValue(['red', 'black', 'red'])).toEqual('2 kiloohms') + }) + + xtest('Green and brown and orange', () => { + expect(decodedResistorValue(['green', 'brown', 'orange'])).toEqual( + '51 kiloohms' + ) + }) + + xtest('Yellow and violet and yellow', () => { + expect(decodedResistorValue(['yellow', 'violet', 'yellow'])).toEqual( + '470 kiloohms' + ) + }) +}) diff --git a/exercises/practice/resistor-color-trio/resistor-color-trio.ts b/exercises/practice/resistor-color-trio/resistor-color-trio.ts new file mode 100644 index 000000000..47498cfea --- /dev/null +++ b/exercises/practice/resistor-color-trio/resistor-color-trio.ts @@ -0,0 +1,3 @@ +export function decodedResistorValue() { + throw new Error('Remove this statement and implement this function') +} diff --git a/exercises/practice/resistor-color-trio/tsconfig.json b/exercises/practice/resistor-color-trio/tsconfig.json new file mode 100644 index 000000000..7314e3544 --- /dev/null +++ b/exercises/practice/resistor-color-trio/tsconfig.json @@ -0,0 +1,19 @@ +{ + "display": "Configuration for Node LTS", + "compilerOptions": { + "lib": ["es2020"], + "module": "commonjs", + "target": "es2020", + + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + + // Because we'll be using babel + // Ensure that Babel can safely transpile files in the TypeScript project + "isolatedModules": true + }, + "include": ["*", ".meta/*"], + "exclude": ["node_modules"] +}