3
\$\begingroup\$

Question:

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 write a program that transforms the legacy data format to the shiny new format.

Code:

import java.util.Map;
import java.util.List;
import java.util.HashMap;

import com.google.inject.AbstractModule;

public class Etl extends AbstractModule {
  public Map transform(Map<Integer, List<String>> oldData) {
    Map<String, Integer> items = new HashMap<String, Integer>();

    for(Map.Entry<Integer, List<String>> entry: oldData.entrySet()) {
      for(String chr: entry.getValue()) {
       items.put(chr.toLowerCase(), entry.getKey()); 
      }
    }
    return items;
  }
}

Can the above code be made more readable and optimized?

\$\endgroup\$
2
2
\$\begingroup\$

One way to improve readability is through the naming of your variables.

  • oldData can be called something like pointsMap or pointsGroups
  • items can be called result or characterPoints
  • entry can be called pointGroup
  • chr can be called character (and would as the other answer suggests be better of as a Character and not String)

Additionally, your class is called Etl which I just have no clue what that's supposed to mean. I'm also not sure why it extends AbstractModule.

Also, your transform method can be static

\$\endgroup\$
2
\$\begingroup\$

The two things I would change:

  • The Map you're returning should have type parameters.
  • The key is best to be a Character rather than a String.

Other than that, I don't see much more to it than what you've got there already.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.