 # ajborla's solution

## to ETL in the C Track

Published at Aug 16 2019 · 0 comments
Instructions
Test suite
Solution

We are going to do the `Transform` step of an Extract-Transform-Load.

### ETL

Extract-Transform-Load (ETL) is a fancy way of saying, "We have some crufty, legacy data over in this system, and now we need it in this shiny new system over here, so we're going to migrate this."

(Typically, this is followed by, "We're only going to need to run this once." That's then typically followed by much forehead slapping and moaning about how stupid we could possibly be.)

### The goal

We're going to extract some scrabble scores from a legacy system.

The old system stored a list of letters per score:

• 1 point: "A", "E", "I", "O", "U", "L", "N", "R", "S", "T",
• 2 points: "D", "G",
• 3 points: "B", "C", "M", "P",
• 4 points: "F", "H", "V", "W", "Y",
• 5 points: "K",
• 8 points: "J", "X",
• 10 points: "Q", "Z",

The shiny new scrabble system instead stores the score per letter, which makes it much faster and easier to calculate the score for a word. It also stores the letters in lower-case regardless of the case of the input letters:

• "a" is worth 1 point.
• "b" is worth 3 points.
• "c" is worth 3 points.
• "d" is worth 2 points.
• Etc.

Your mission, should you choose to accept it, is to transform the legacy data format to the shiny new format.

### Notes

A final note about scoring, Scrabble is played around the world in a variety of languages, each with its own unique scoring table. For example, an "E" is scored at 2 in the Māori-language version of the game while being scored at 4 in the Hawaiian-language version.

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

The Jumpstart Lab team http://jumpstartlab.com

## Submitting Incomplete Solutions

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

### test_etl.c

``````#include "vendor/unity.h"
#include "../src/etl.h"
#include <stdlib.h>

void setUp(void)
{
}

void tearDown(void)
{
}

static int compare_map(new_map * a, new_map * b, int len)
{
if (a == NULL)
return (b == NULL) ? 0 : 1;
if (b == NULL)
return 1;
while (--len >= 0)
if ((a[len].key != b[len].key) || (a[len].value != b[len].value))
return 1;
return !(len == -1);
}

static void test_a_single_letter(void)
{
legacy_map input[] = { {1, "A"} };
int input_len = 1;

new_map expected_map[] = { {'a', 1} };
int expected_length = 1;

new_map *output = NULL;
int actual_length = convert(input, input_len, &output);

TEST_ASSERT_EQUAL_INT(expected_length, actual_length);
TEST_ASSERT_EQUAL_INT(0, compare_map(expected_map, output, actual_length));

free(output);
}

static void test_single_score_with_multiple_letters(void)
{
TEST_IGNORE();               // delete this line to run test
legacy_map input[] = { {1, "AEIOU"} };
int input_len = 1;

new_map expected_map[] = {
{'a', 1}, {'e', 1}, {'i', 1}, {'o', 1}, {'u', 1}
};
int expected_length = 5;

new_map *output = NULL;
int actual_length = convert(input, input_len, &output);

TEST_ASSERT_EQUAL_INT(expected_length, actual_length);
TEST_ASSERT_EQUAL_INT(0, compare_map(expected_map, output, actual_length));

free(output);
}

static void test_multiple_scores_with_multiple_letters(void)
{
TEST_IGNORE();
legacy_map input[] = { {1, "AE"}, {2, "DG"} };
int input_len = 2;

new_map expected_map[] = {
{'a', 1}, {'d', 2}, {'e', 1}, {'g', 2}
};
int expected_length = 4;

new_map *output = NULL;
int actual_length = convert(input, input_len, &output);

TEST_ASSERT_EQUAL_INT(expected_length, actual_length);
TEST_ASSERT_EQUAL_INT(0, compare_map(expected_map, output, actual_length));

free(output);
}

static void test_multiple_scores_with_differing_numbers_of_letters(void)
{
TEST_IGNORE();
legacy_map input[] = {
{10, "QZ"}, {1, "AEIOULNRST"}, {3, "BCMP"}, {2, "DG"},
{5, "K"}, {4, "FHVWY"}, {8, "JX"}
};
int input_len = 7;

new_map expected_map[] = {
{'a', 1}, {'b', 3}, {'c', 3}, {'d', 2}, {'e', 1}, {'f', 4},
{'g', 2}, {'h', 4}, {'i', 1}, {'j', 8}, {'k', 5}, {'l', 1},
{'m', 3}, {'n', 1}, {'o', 1}, {'p', 3}, {'q', 10}, {'r', 1},
{'s', 1}, {'t', 1}, {'u', 1}, {'v', 4}, {'w', 4}, {'x', 8},
{'y', 4}, {'z', 10}
};
int expected_length = 26;

new_map *output = NULL;
int actual_length = convert(input, input_len, &output);

TEST_ASSERT_EQUAL_INT(expected_length, actual_length);
TEST_ASSERT_EQUAL_INT(0, compare_map(expected_map, output, actual_length));

free(output);
}

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

RUN_TEST(test_a_single_letter);
RUN_TEST(test_single_score_with_multiple_letters);
RUN_TEST(test_multiple_scores_with_multiple_letters);
RUN_TEST(test_multiple_scores_with_differing_numbers_of_letters);

return UnityEnd();
}``````

### src/etl.c

``````/* ------------------------------------------------------------------------- */
/* exercism.io                                                               */
/* C Track Exercise: etl                                                     */
/* Contributed: Anthony J. Borla (ajborla@bigpond.com)                       */
/* ------------------------------------------------------------------------- */

#include "etl.h"

#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int convert(const legacy_map* input, const size_t input_len,
new_map** output)
{
// Local work buffer
new_map local_map[] = {
{'a', 0}, {'b', 0}, {'c', 0}, {'d', 0}, {'e', 0}, {'f', 0},
{'g', 0}, {'h', 0}, {'i', 0}, {'j', 0}, {'k', 0}, {'l', 0},
{'m', 0}, {'n', 0}, {'o', 0}, {'p', 0}, {'q', 0}, {'r', 0},
{'s', 0}, {'t', 0}, {'u', 0}, {'v', 0}, {'w', 0}, {'x', 0},
{'y', 0}, {'z', 0}
};

char* p = NULL, c = '\0'; int entries = 0;

// Perform ETL. For each input table entry ...
for (size_t i = 0; i < input_len; ++i)
{
p = (char*) input[i].keys;

// Traverse keys, update corresponding local table entry
while ((c = tolower(*p++)) != '\0')
{
local_map[c - 'a'].value = input[i].value;
++entries;
}
}

// Allocate and validate memory for output table
if ((*output = (new_map*) malloc(sizeof(new_map) * entries)) == NULL) return 0;

// Copy only selected contents of local table to output table
for (size_t i = 0, e = 0; i < sizeof(local_map) / sizeof(local_map); ++i)
{
// Skip if not an updated table entry
if (local_map[i].value == 0) continue;

// Copy entry to output table
(*output)[e].key = local_map[i].key;
(*output)[e].value = local_map[i].value;

++e;
}

// Number of actual entries in output table
return entries;
}``````

### src/etl.h

``````/* ------------------------------------------------------------------------- */
/* exercism.io                                                               */
/* C Track Exercise: etl                                                     */
/* Contributed: Anthony J. Borla (ajborla@bigpond.com)                       */
/* ------------------------------------------------------------------------- */

#ifndef ETL_H
#define ETL_H

#include <stddef.h>

typedef struct {
int value;
const char *keys;
} legacy_map;

typedef struct {
char key;
int value;
} new_map;

/* ------------------------------------------------------------------------- */
/* Given an input table, and its entry count ('input' and 'input_len') copy  */
/* and transform each entry to conform to output table ('output') table str- */
/* ucture. Success sees output table filled, and its entry count returned;   */
/* error sees empty table, and zero count returned                           */
/* ------------------------------------------------------------------------- */
int convert(const legacy_map* input, const size_t input_len,
new_map** output);

#endif``````