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:
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
To run the tests, run the command dotnet test
from within the exercise directory.
Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the Skip
property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using exercism submit ResistorColor.cs
For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.
Maud de Vries, Erik Schierboom https://github.com/exercism/problem-specifications/issues/1458
// This file was auto-generated based on version 1.0.0 of the canonical data.
using Xunit;
public class ResistorColorTest
{
[Fact]
public void Black()
{
Assert.Equal(0, ResistorColor.ColorCode("black"));
}
[Fact(Skip = "Remove to run test")]
public void White()
{
Assert.Equal(9, ResistorColor.ColorCode("white"));
}
[Fact(Skip = "Remove to run test")]
public void Orange()
{
Assert.Equal(3, ResistorColor.ColorCode("orange"));
}
[Fact(Skip = "Remove to run test")]
public void Colors()
{
Assert.Equal(new[] { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white" }, ResistorColor.Colors());
}
}
using System;
using System.Linq;
public static class ResistorColor
{
private static string[] ResistorColors = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"};
public static int ColorCode(string color)
{
return Array.FindIndex(ResistorColors, x => x == color);
}
public static string[] Colors()
{
return ResistorColors;
}
}
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments