🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # shikov's solution

## to ETL in the C Track

Published at Jul 26 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.h

``````#ifndef ETL_H
#define ETL_H

typedef struct{
int points;
char* chars;
} legacy_map;

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

int convert(legacy_map* input, int input_len, new_map** output);

#endif``````

### src/etl.c

``````#include "etl.h"
#include <ctype.h>
#include <stdlib.h>

int convert(legacy_map* input, int input_len, new_map** output){
size_t count = 0;
for (size_t i=0; i<(size_t)input_len; i++) {
for (size_t j = 0; input[i].chars[j]!='\0'; j++, count++) {
*output = realloc(*output, (count+1) * sizeof(new_map));
new_map nmap = {(char)tolower(input[i].chars[j]), input[i].points}, cmap = nmap;
for(size_t k=0; k<=count; k++){
if(k == count) (*output)[k] = nmap;
else if(cmap.key < (*output)[k].key){
cmap = (*output)[k];
(*output)[k] = nmap;
nmap = cmap;
}
}
}
}
return (int)count;
}``````