patternjavaMinor
Inverting a Scrabble letter score map
Viewed 0 times
mapscrabblescoreinvertingletter
Problem
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:
Can the above code be made more readable and optimized?
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> oldData) {
Map items = new HashMap();
for(Map.Entry> 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?
Solution
The two things I would change:
Other than that, I don't see much more to it than what you've got there already.
- The
Mapyou're returning should have type parameters.
- The key is best to be a
Characterrather than aString.
Other than that, I don't see much more to it than what you've got there already.
Context
StackExchange Code Review Q#143612, answer score: 2
Revisions (0)
No revisions yet.