Avatar of hhhisme

hhhisme's solution

to ETL in the C Track

Published at Jun 28 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();
}
#include <stdlib.h>
#include <string.h>
#include "etl.h"

int compare_pairs(const void *a, const void *b)
{
    const new_pair_t *A = a, *B = b;
    return A->key - B->key;
}

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

    int num_elem = 0;
    for (int i = 0; i < input_len; i++)
    {
        num_elem += strlen(input[i].value);
    }

    *output = calloc(num_elem, sizeof(new_pair_t));

    int j = 0;

    for (int i = 0; i < input_len; i++)
    {
        int length = strlen(input[i].value);
        for (int k = 0; k < length; k++)
        {
            (*output)[j].key = input[i].value[k] + 32;
            (*output)[j++].value = input[i].key;
        }
    }

    qsort(*output, num_elem, sizeof(new_pair_t), compare_pairs);

    return num_elem;
}

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?