Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of rootulp

rootulp's solution

to ETL in the Java Track

Published at Jul 13 2018 · 2 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.

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

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.

EtlTest.java

import org.junit.Test;
import org.junit.Ignore;

import java.util.*;

import static org.junit.Assert.assertEquals;


public class EtlTest {
    private final Etl etl = new Etl();

    @Test
    public void testTransformOneValue() {
        Map<Integer, List<String>> old = new HashMap<Integer, List<String>>() {
            {
                put(1, Arrays.asList("A"));
            }
        };
        old = Collections.unmodifiableMap(old);

        Map<String, Integer> expected = new HashMap<String, Integer>() {
            {
                put("a", 1);
            }
        };
        expected = Collections.unmodifiableMap(expected);

        assertEquals(expected, etl.transform(old));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTransformMoreValues() {
        Map<Integer, List<String>> old = new HashMap<Integer, List<String>>() {
            {
                put(1, Arrays.asList("A", "E", "I", "O", "U"));
            }
        };
        old = Collections.unmodifiableMap(old);

        Map<String, Integer> expected = new HashMap<String, Integer>() {
            {
                put("a", 1);
                put("e", 1);
                put("i", 1);
                put("o", 1);
                put("u", 1);
            }
        };
        expected = Collections.unmodifiableMap(expected);

        assertEquals(expected, etl.transform(old));
    }

    @Ignore("Remove to run test")
    @Test
    public void testMoreKeys() {
        Map<Integer, List<String>> old = new HashMap<Integer, List<String>>() {
            {
                put(1, Arrays.asList("A", "E"));
                put(2, Arrays.asList("D", "G"));
            }
        };
        old = Collections.unmodifiableMap(old);

        Map<String, Integer> expected = new HashMap<String, Integer>() {
            {
                put("a", 1);
                put("e", 1);
                put("d", 2);
                put("g", 2);
            }
        };
        expected = Collections.unmodifiableMap(expected);

        assertEquals(expected, etl.transform(old));
    }

    @Ignore("Remove to run test")
    @Test
    public void testFullDataset() {
        Map<Integer, List<String>> old = new HashMap<Integer, List<String>>() {
            {
                put(1, Arrays.asList("A", "E", "I", "O", "U", "L", "N", "R", "S", "T"));
                put(2, Arrays.asList("D", "G"));
                put(3, Arrays.asList("B", "C", "M", "P"));
                put(4, Arrays.asList("F", "H", "V", "W", "Y"));
                put(5, Arrays.asList("K"));
                put(8, Arrays.asList("J", "X"));
                put(10, Arrays.asList("Q", "Z"));
            }
        };
        old = Collections.unmodifiableMap(old);

        Map<String, Integer> expected = new HashMap<String, Integer>() {
            {
                put("a", 1);
                put("b", 3);
                put("c", 3);
                put("d", 2);
                put("e", 1);
                put("f", 4);
                put("g", 2);
                put("h", 4);
                put("i", 1);
                put("j", 8);
                put("k", 5);
                put("l", 1);
                put("m", 3);
                put("n", 1);
                put("o", 1);
                put("p", 3);
                put("q", 10);
                put("r", 1);
                put("s", 1);
                put("t", 1);
                put("u", 1);
                put("v", 4);
                put("w", 4);
                put("x", 8);
                put("y", 4);
                put("z", 10);
            }
        };
        expected = Collections.unmodifiableMap(expected);

        assertEquals(expected, etl.transform(old));
    }
}
import java.util.Map;
import java.util.List;
import java.util.HashMap;

public class Etl {

  public Map<String, Integer> transform(Map<Integer, List<String>> oldMap) {
    Map<String, Integer> newMap = new HashMap<String, Integer>();

    oldMap.forEach((val, list) -> {
      list.forEach((character) -> {
        if (!newMap.containsKey(character)) {
          newMap.put(character.toLowerCase(), val);
        }
      });
    });

    return newMap;
  }

}

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of maugus33

Hi! I really like the solution you gave. You make good use of Lambda Expressions ( I didn't even know Java used them now! ) and passive iterators to make your code succulent and understandable.

The only question I have is your reasoning for checking if the new map doesn't contain a particular character. It seems a little bit unnecessary for the purposes of this function in the context of this question ( as the client merely wants a reformat of the data, not to also check that each character has only one point associated with it). I like the idea, but it might be more than what was asked for ( in which case, it would lead to side effects users of the function might not want).

Avatar of jtigger

That's a good point, @maugus33. This is an easy thing to slip into.

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?