Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of neiesc

neiesc's solution

to Resistor Color in the C# Track

Published at Mar 27 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

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

Running the tests

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

Further information

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
    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;

public static class ResistorColor
    public static int ColorCode(string color)
        return Array.FindIndex(Colors(), x => x == color);

    public static string[] Colors()
        return new[] { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white" };

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?