-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add resistor-color * Remove colors from stub
- Loading branch information
Showing
9 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Instructions | ||
|
||
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 has a position and a numeric value. | ||
|
||
The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number. | ||
|
||
In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands. | ||
|
||
These 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 | ||
|
||
The goal of this exercise is to create a way: | ||
|
||
- to look up the numerical value associated with a particular color band | ||
- to list the different band colors | ||
|
||
Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array: | ||
Better Be Right Or Your Great Big Values Go Wrong. | ||
|
||
More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article][e-color-code]. | ||
|
||
[e-color-code]: https://en.wikipedia.org/wiki/Electronic_color_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"BNAndras" | ||
], | ||
"files": { | ||
"solution": [ | ||
"resistor_color.bal" | ||
], | ||
"test": [ | ||
"tests/resistor_color_test.bal" | ||
], | ||
"example": [ | ||
".meta/reference/resistor_color.bal" | ||
] | ||
}, | ||
"blurb": "Convert a resistor band's color to its numeric representation.", | ||
"source": "Maud de Vries, Erik Schierboom", | ||
"source_url": "https://github.com/exercism/problem-specifications/issues/1458" | ||
} |
26 changes: 26 additions & 0 deletions
26
exercises/practice/resistor-color/.meta/reference/resistor_color.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
final string[] colorList = [ | ||
"black", | ||
"brown", | ||
"red", | ||
"orange", | ||
"yellow", | ||
"green", | ||
"blue", | ||
"violet", | ||
"grey", | ||
"white" | ||
]; | ||
|
||
# Calculates the resistor value for the passed band color | ||
# | ||
# + color - The color of the resistor band | ||
# + return - The value of the resistor band | ||
function colorCode(string color) returns int { | ||
return colorList.indexOf(color) ?: 0; | ||
} | ||
|
||
# Returns the list of colors in the resistor color code | ||
# + return - The list of colors | ||
function colors() returns string[] { | ||
return colorList; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 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. | ||
|
||
[49eb31c5-10a8-4180-9f7f-fea632ab87ef] | ||
description = "Color codes -> Black" | ||
|
||
[0a4df94b-92da-4579-a907-65040ce0b3fc] | ||
description = "Color codes -> White" | ||
|
||
[5f81608d-f36f-4190-8084-f45116b6f380] | ||
description = "Color codes -> Orange" | ||
|
||
[581d68fa-f968-4be2-9f9d-880f2fb73cf7] | ||
description = "Colors" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
org = "ballerina_exercism" | ||
name = "resistor_color" | ||
version = "0.1.0" | ||
distribution = "2201.5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# AUTO-GENERATED FILE. DO NOT MODIFY. | ||
|
||
# This file is auto-generated by Ballerina for managing dependency versions. | ||
# It should not be modified by hand. | ||
|
||
[ballerina] | ||
dependencies-toml-version = "2" | ||
distribution-version = "2201.5.0" | ||
|
||
[[package]] | ||
org = "ballerina" | ||
name = "jballerina.java" | ||
version = "0.0.0" | ||
scope = "testOnly" | ||
|
||
[[package]] | ||
org = "ballerina" | ||
name = "test" | ||
version = "0.0.0" | ||
scope = "testOnly" | ||
dependencies = [ | ||
{org = "ballerina", name = "jballerina.java"} | ||
] | ||
modules = [ | ||
{org = "ballerina", packageName = "test", moduleName = "test"} | ||
] | ||
|
||
[[package]] | ||
org = "ballerina_exercism" | ||
name = "resistor_color" | ||
version = "0.1.0" | ||
dependencies = [ | ||
{org = "ballerina", name = "test"} | ||
] | ||
modules = [ | ||
{org = "ballerina_exercism", packageName = "resistor_color", moduleName = "resistor_color"} | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Calculates the resistor value for the passed band color | ||
# | ||
# + color - The color of the resistor band | ||
# + return - The value of the resistor band | ||
function colorCode(string color) returns int { | ||
// TODO: implement this function | ||
} | ||
|
||
# Returns the list of colors in the resistor color code | ||
# + return - The list of colors | ||
function colors() returns string[] { | ||
// TODO: implement this function | ||
} |
39 changes: 39 additions & 0 deletions
39
exercises/practice/resistor-color/tests/resistor_color_test.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import ballerina/test; | ||
|
||
@test:Config {} | ||
function testBlack() { | ||
test:assertEquals(0, colorCode("black")); | ||
} | ||
|
||
@test:Config { | ||
enable: false | ||
} | ||
function testWhite() { | ||
test:assertEquals(9, colorCode("white")); | ||
} | ||
|
||
@test:Config { | ||
enable: false | ||
} | ||
function testOrange() { | ||
test:assertEquals(3, colorCode("orange")); | ||
} | ||
|
||
@test:Config { | ||
enable: false | ||
} | ||
function testColors() { | ||
string[] expected = [ | ||
"black", | ||
"brown", | ||
"red", | ||
"orange", | ||
"yellow", | ||
"green", | ||
"blue", | ||
"violet", | ||
"grey", | ||
"white" | ||
]; | ||
test:assertEquals(expected, colors()); | ||
} |