Skip to content

Resistor Color Trio

Read a resistor’s first three color bands as a resistance, written in the largest metric unit that keeps the value whole.

Adapted from Exercism’s problem specification (MIT). Listed under Encoding & Ciphers.

// FILE: resistor_color_trio.nomi
//# The resistance a resistor's first three color bands give, in the largest
//# metric unit that keeps it whole.

/// A band color, in value order: black is 0 and white is 9.
pub enum Color
    Black
    Brown
    Red
    Orange
    Yellow
    Green
    Blue
    Violet
    Grey
    White
end

/// The unit a resistance is written in, each a thousand times the one before.
pub enum Magnitude
    Ohms
    Kiloohms
    Megaohms
    Gigaohms
end

/// A resistance as the label on a resistor reads it: `33 kiloohms`.
pub struct Resistance
    value: Int
    unit:  Magnitude
end

/// The resistance the first three bands give: two digits, then the number
/// of zeros after them. Bands after the third are ignored. `None` when there
/// are fewer than three bands.
pub fn label(bands: List<Color>) -> Maybe<Resistance>
    [first, second, zeros, ..] = bands else return None

    ohms = (digit(first) * 10 + digit(second)) * Int.pow(10, digit(zeros))
    Some(in_largest_unit(ohms))
end

/// `ohms` in the largest unit that keeps the value whole.
fn in_largest_unit(ohms: Int) -> Resistance
    [Magnitude.Kiloohms, .Megaohms, .Gigaohms]
    |> Iter.reduce(Resistance{value: ohms, unit: .Ohms}, fn resistance, unit ->
        if resistance.value != 0 and resistance.value % 1000 == 0 then
            {value: resistance.value / 1000, unit:}
        else
            resistance
        end
    end)
end

fn digit(color: Color) -> NonNegativeInt
    case color
        .Black  -> 0
        .Brown  -> 1
        .Red    -> 2
        .Orange -> 3
        .Yellow -> 4
        .Green  -> 5
        .Blue   -> 6
        .Violet -> 7
        .Grey   -> 8
        .White  -> 9
    end
end

// FILE: resistor_color_trio_test.nomi
import
    resistor_color_trio
    resistor_color_trio.{Color, Magnitude, Resistance}
end

test "label"
    [
        ("Orange and orange and black", [Color.Orange, .Orange, .Black], 33, Magnitude.Ohms),
        ("Blue and grey and brown", [.Blue, .Grey, .Brown], 680, .Ohms),
        ("Red and black and red", [.Red, .Black, .Red], 2, .Kiloohms),
        ("Green and brown and orange", [.Green, .Brown, .Orange], 51, .Kiloohms),
        ("Yellow and violet and yellow", [.Yellow, .Violet, .Yellow], 470, .Kiloohms),
        ("Blue and violet and blue", [.Blue, .Violet, .Blue], 67, .Megaohms),
        ("Minimum possible value", [.Black, .Black, .Black], 0, .Ohms),
        ("Maximum possible value", [.White, .White, .White], 99, .Gigaohms),
        ("First two colors make an invalid octal number", [.Black, .Grey, .Black], 8, .Ohms),
        ("Ignore extra colors", [.Blue, .Green, .Yellow, .Orange], 650, .Kiloohms),
    ]
    |> subtests (description, bands, value, unit), description
        assert resistor_color_trio.label(bands) == Some(Resistance{value:, unit:})
    end
end

// Not a canonical case: the exercise always gives at least three bands.
test "fewer than three bands have no label"
    assert resistor_color_trio.label([.Orange, .Orange]) == None
end
  • The answer is a Resistance struct of a value and a Magnitude enum (Ohms, Kiloohms, Megaohms, Gigaohms), matching the canonical {value:, unit:}, rather than the label text. The enum is Magnitude, not Unit: Unit names Nomi’s empty value and is reserved, and Magnitude says what the variants are.
  • label takes a List<Color> and answers Maybe<Resistance>, None for fewer than three bands, for the reason resistor-color-duo gives. The binding else keeps the success path unindented.
  • The third band’s zeros are Int.pow(10, digit(zeros)), inline in the ohms expression.
  • in_largest_unit folds over the larger units with Iter.reduce, dividing by 1000 while the value stays whole and is not zero. It needs no arm for running out of units, which a recursive walk down a list of units would.
  • The ten canonical cases are one subtests table whose rows hold the expected value and unit as two columns, rebuilt into a Resistance in the assertion.