Skip to content

Exercise/resistor color trio #814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 45 additions & 0 deletions exercises/practice/resistor-color-trio/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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"
12 changes: 12 additions & 0 deletions exercises/practice/resistor-color-trio/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
!.meta

# Protected or generated
.git
.vscode

# When using npm
node_modules/*

# Configuration files
babel.config.js
jest.config.js
23 changes: 23 additions & 0 deletions exercises/practice/resistor-color-trio/.eslintrc
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
12 changes: 12 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
63 changes: 63 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/proof.ci.ts
Original file line number Diff line number Diff line change
@@ -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`
}
18 changes: 18 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 20 additions & 0 deletions exercises/practice/resistor-color-trio/babel.config.js
Original file line number Diff line number Diff line change
@@ -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',
],
}
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color-trio/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
verbose: true,
projects: ['<rootDir>'],
testMatch: [
'**/__tests__/**/*.[jt]s?(x)',
'**/test/**/*.[jt]s?(x)',
'**/?(*.)+(spec|test).[jt]s?(x)',
],
testPathIgnorePatterns: [
'/(?:production_)?node_modules/',
'.d.ts$',
'<rootDir>/test/fixtures',
'<rootDir>/test/helpers',
'__mocks__',
],
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
},
}
37 changes: 37 additions & 0 deletions exercises/practice/resistor-color-trio/package.json
Original file line number Diff line number Diff line change
@@ -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 <derk-jan+git@karrenbeld.info> (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"
}
}
29 changes: 29 additions & 0 deletions exercises/practice/resistor-color-trio/resistor-color-trio.test.ts
Original file line number Diff line number Diff line change
@@ -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'
)
})
})
3 changes: 3 additions & 0 deletions exercises/practice/resistor-color-trio/resistor-color-trio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function decodedResistorValue() {
throw new Error('Remove this statement and implement this function')
}
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color-trio/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}