Avatar of ZapAnton

ZapAnton's solution

to Resistor Color in the C Track

Published at Oct 20 2019 · 0 comments
Instructions
Test suite
Solution

Note:

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

Getting Started

Make sure you have read the "Guides" section of the C track on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

Source

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

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

test_resistor_color.c

#include "vendor/unity.h"
#include "../src/resistor_color.h"

#define ARRAY_LENGTH(A) (sizeof(A) / sizeof(A[0]))

void setUp(void)
{
}

void tearDown(void)
{
}

static void test_black(void)
{
   TEST_ASSERT_EQUAL_UINT16(0, colorCode(BLACK));
}

static void test_white(void)
{
   TEST_IGNORE();               // delete this line to run test
   TEST_ASSERT_EQUAL_UINT16(9, colorCode(WHITE));
}

static void test_orange(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL_UINT16(3, colorCode(ORANGE));
}

static void test_colors(void)
{
   TEST_IGNORE();
   const resistor_band_t expected[] = {
      BLACK, BROWN, RED, ORANGE, YELLOW,
      GREEN, BLUE, VIOLET, GREY, WHITE
   };
   TEST_ASSERT_EQUAL_INT_ARRAY(expected, colors(), ARRAY_LENGTH(expected));
}

int main(void)
{
   UnityBegin("test/test_resistor_color.c");

   RUN_TEST(test_black);
   RUN_TEST(test_white);
   RUN_TEST(test_orange);
   RUN_TEST(test_colors);

   return UnityEnd();
}

src/resistor_color.c

#include "resistor_color.h"

const resistor_band_t available_colors[COLOR_COUNT] = {
    BLACK,
    BROWN,
    RED,
    ORANGE,
    YELLOW,
    GREEN,
    BLUE,
    VIOLET,
    GREY,
    WHITE,
};

int colorCode(const resistor_band_t band) {
    const int color_code = (band < COLOR_COUNT) ? available_colors[band] : UNKNOWN_COLOR;
    return color_code;
}

const resistor_band_t* colors() {
    return available_colors;
}

src/resistor_color.h

#ifndef RESISTOR_COLOR_H
#define RESISTOR_COLOR_H

typedef enum {
    BLACK,
    BROWN,
    RED,
    ORANGE,
    YELLOW,
    GREEN,
    BLUE,
    VIOLET,
    GREY,
    WHITE,
    COLOR_COUNT,
    UNKNOWN_COLOR
} resistor_band_t;

int colorCode(const resistor_band_t name);

const resistor_band_t* colors();

#endif

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?