Skip to content

Resistor-Color: new exercise #371

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 1 commit into from
Feb 14, 2019
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
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@
"transforming"
]
},
{
"slug": "resistor-color",
"uuid": "cd21de38-d572-4cc1-9a6f-1c5aed511b32",
"core": false,
"unlocked_by": "two-fer",
"difficulty": 1,
"topics": [
"arrays"
]
},
{
"slug": "collatz-conjecture",
"uuid": "edfc2903-1165-4029-9901-0be72b32865f",
Expand Down
47 changes: 47 additions & 0 deletions exercises/resistor-color/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Resistor Color

Resistors have color coded bands, where each color maps to a number. The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.

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

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](https://en.wikipedia.org/wiki/Electronic_color_code)

## Testing

In order to run the tests for this track, you will need to install
DUnitX. Please see the [installation](http://www.exercism.io/languages/delphi/installation) instructions for more information.

### Loading Exercises into Delphi

If Delphi is properly installed, and `*.dpr` file types have been associated with Delphi, then double clicking the supplied `*.dpr` file will start Delphi and load the exercise/project. `control + F9` is the keyboard shortcut to compile the project or pressing `F9` will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the `File` drop down menu.

### When Questions Come Up
We monitor the [Pascal-Delphi](https://gitter.im/exercism/Pascal-Delphi) support room on [gitter.im](https://gitter.im) to help you with any questions that might arise.

### Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the `exercism/delphi/<exerciseName>` directory.

For example, if you're submitting `ubob.pas` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas`.

## Source

Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1458](https://github.com/exercism/problem-specifications/issues/1458)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you may receive assistance from a mentor.
60 changes: 60 additions & 0 deletions exercises/resistor-color/ResistorColor.dpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
program ResistorColor;

{$IFNDEF TESTINSIGHT}
{$APPTYPE CONSOLE}
{$ENDIF}{$STRONGLINKTYPES ON}
uses
System.SysUtils,
{$IFDEF TESTINSIGHT}
TestInsight.DUnitX,
{$ENDIF }
DUnitX.Loggers.Console,
DUnitX.Loggers.Xml.NUnit,
DUnitX.TestFramework,
uResistorColorTests in 'uResistorColorTests.pas',
uResistor in 'uResistor.pas';

var
runner : ITestRunner;
results : IRunResults;
logger : ITestLogger;
nunitLogger : ITestLogger;
begin
{$IFDEF TESTINSIGHT}
TestInsight.DUnitX.RunRegisteredTests;
exit;
{$ENDIF}
try
//Check command line options, will exit if invalid
TDUnitX.CheckCommandLine;
//Create the test runner
runner := TDUnitX.CreateRunner;
//Tell the runner to use RTTI to find Fixtures
runner.UseRTTI := True;
//tell the runner how we will log things
//Log to the console window
logger := TDUnitXConsoleLogger.Create(true);
runner.AddLogger(logger);
//Generate an NUnit compatible XML File
nunitLogger := TDUnitXXMLNUnitFileLogger.Create(TDUnitX.Options.XMLOutputFile);
runner.AddLogger(nunitLogger);
runner.FailsOnNoAsserts := False; //When true, Assertions must be made during tests;

//Run tests
results := runner.Execute;
if not results.AllPassed then
System.ExitCode := EXIT_ERRORS;

{$IFNDEF CI}
//We don't want this happening when running under CI.
if TDUnitX.Options.ExitBehavior = TDUnitXExitBehavior.Pause then
begin
System.Write('Done.. press <Enter> key to quit.');
System.Readln;
end;
{$ENDIF}
except
on E: Exception do
System.Writeln(E.ClassName, ': ', E.Message);
end;
end.
81 changes: 81 additions & 0 deletions exercises/resistor-color/uResistorColorTests.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
unit uResistorColorTests;

interface
uses
DUnitX.TestFramework, uResistor;

type

[TestFixture('Color Codes')]
TTestResistorColor = class(TObject)
public
[Test]
// [Ignore('Comment the "[Ignore]" statement to run the test')]
procedure Black;

[Test]
[Ignore]
procedure White;

[Test]
[Ignore]
procedure Orange;
end;

[TestFixture('Colors')]
TTestColors = class(TObject)
private
procedure CompareDynamicArrays(aExpected, aActual: TArray<TResistorColors>);
public
[Test]
[Ignore]
procedure Colors;
end;

implementation

{ TTestResistorColor }

procedure TTestResistorColor.Black;
begin
Assert.AreEqual(0, TResistor.colorCode(rcBlack));
end;

procedure TTestResistorColor.White;
begin
Assert.AreEqual(9, TResistor.colorCode(rcWhite));
end;

procedure TTestResistorColor.Orange;
begin
Assert.AreEqual(3, TResistor.colorCode(rcOrange));
end;

{ TTestColors }

procedure TTestColors.Colors;
var
Expected: TArray<TResistorColors>;
Actual: TArray<TResistorColors>;
begin
Expected := [rcBlack, rcBrown, rcRed, rcOrange, rcYellow, rcGreen, rcBlue,
rcViolet, rcGrey, rcWhite];

Actual := TResistor.colors;

CompareDynamicArrays(Expected, Actual);
end;

procedure TTestColors.CompareDynamicArrays(aExpected, aActual: TArray<TResistorColors>);
var
i: integer;
begin
Assert.AreEqual(length(aExpected), length(aActual));
for i := Low(aExpected) to High(aExpected) do
Assert.AreEqual(aExpected[i], aActual[i]);
end;

initialization
TDUnitX.RegisterTestFixture(TTestResistorColor);
TDUnitX.RegisterTestFixture(TTestColors);
end.
31 changes: 31 additions & 0 deletions exercises/resistor-color/uResistorExample.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
unit uResistor;

interface

type
TResistorColors = (rcBlack, rcBrown, rcRed, rcOrange, rcYellow, rcGreen,
rcBlue, rcViolet, rcGrey, rcWhite);

TResistor = class
class function colorCode(aColor: TResistorColors): integer;
class function colors: TArray<TResistorColors>;
end;


implementation

{ TResistor }

class function TResistor.colorCode(aColor: TResistorColors): integer;
begin
result := ord(aColor);
end;

class function TResistor.colors: TArray<TResistorColors>;
begin
SetLength(Result, ord(high(TResistorColors)) + 1);
for var aColor := Low(TResistorColors) to High(TResistorColors) do
Result[ord(aColor)] := aColor;
end;

end.