Skip to content

Instantly share code, notes, and snippets.

@hd42
Created June 27, 2016 19:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hd42/d77b339ca5491edd5b5898b80ee0157a to your computer and use it in GitHub Desktop.
Save hd42/d77b339ca5491edd5b5898b80ee0157a to your computer and use it in GitHub Desktop.
package software.schwering.javacodechallenge;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
public class ScrabbleSets {
private static final Map<Character, Integer> COUNT_BY_LETTER = new HashMap<>();
static{
//see http://scrabblewizard.com/scrabble-tile-distribution/
COUNT_BY_LETTER.put('A', 9);
COUNT_BY_LETTER.put('B', 2);
COUNT_BY_LETTER.put('C', 2);
COUNT_BY_LETTER.put('D', 4);
COUNT_BY_LETTER.put('E', 12);
COUNT_BY_LETTER.put('F', 2);
COUNT_BY_LETTER.put('G', 3);
COUNT_BY_LETTER.put('H', 2);
COUNT_BY_LETTER.put('I', 9);
COUNT_BY_LETTER.put('J', 1);
COUNT_BY_LETTER.put('K', 1);
COUNT_BY_LETTER.put('L', 4);
COUNT_BY_LETTER.put('M', 2);
COUNT_BY_LETTER.put('N', 6);
COUNT_BY_LETTER.put('O', 8);
COUNT_BY_LETTER.put('P', 2);
COUNT_BY_LETTER.put('Q', 1);
COUNT_BY_LETTER.put('R', 6);
COUNT_BY_LETTER.put('S', 4);
COUNT_BY_LETTER.put('T', 6);
COUNT_BY_LETTER.put('U', 4);
COUNT_BY_LETTER.put('V', 2);
COUNT_BY_LETTER.put('W', 2);
COUNT_BY_LETTER.put('X', 1);
COUNT_BY_LETTER.put('Y', 2);
COUNT_BY_LETTER.put('Z', 1);
COUNT_BY_LETTER.put('_', 2);
}
public static void printRemainingTiles(String tilesInPlay){
Map<Character, Integer> remainingCount = new HashMap<>(COUNT_BY_LETTER);
tilesInPlay.chars().forEach(i->remainingCount.put((char)i, remainingCount.get((char)i)-1));
List<Character> errors = remainingCount.entrySet().stream().filter(e->e.getValue()<0).map(Entry::getKey).collect(Collectors.toList());
if(errors.isEmpty()){
Map<Integer, String> tilesByCount = remainingCount.entrySet().stream().collect(Collectors.groupingBy(Entry::getValue, Collectors.mapping(Entry::getKey, Collectors.mapping(String::valueOf,Collectors.joining(", ")))));
tilesByCount.entrySet().stream().sorted((e1,e2)->e2.getKey().compareTo(e1.getKey())).forEachOrdered(e->System.out.printf("%s: %s\n",e.getKey(),e.getValue()));
}else{
errors.forEach(c->System.out.printf("Invalid input. More %s's have been taken from the bag than possible.\n",c));
}
}
}
package software.schwering.javacodechallenge;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ScrabbleSetsTest {
private ByteArrayOutputStream baos;
private PrintStream out;
@Before
public void redirectOut() {
baos = new ByteArrayOutputStream();
out = new PrintStream(baos);
System.setOut(out);
}
@After
public void closeStreams() {
out.close();
}
private String getOutputString() throws UnsupportedEncodingException{
return baos.toString(StandardCharsets.UTF_8.name());
}
@Test
public void testChallengeInput1() throws UnsupportedEncodingException {
ScrabbleSets.printRemainingTiles("PQAREIOURSTHGWIOAE_");
assertThat(getOutputString(), is(equalTo(
"10: E\n" +
"7: A, I\n" +
"6: N, O\n" +
"5: T\n" +
"4: D, L, R\n" +
"3: S, U\n" +
"2: B, C, F, G, M, V, Y\n" +
"1: H, J, K, P, W, X, Z, _\n" +
"0: Q\n")));
}
@Test
public void testChallengeInput2() throws UnsupportedEncodingException {
ScrabbleSets.printRemainingTiles("LQTOONOEFFJZT");
assertThat(getOutputString(), is(equalTo(
"11: E\n" +
"9: A, I\n" +
"6: R\n" +
"5: N, O\n" +
"4: D, S, T, U\n" +
"3: G, L\n" +
"2: B, C, H, M, P, V, W, Y, _\n" +
"1: K, X\n" +
"0: F, J, Q, Z\n")));
}
@Test
public void testChallengeInput3() throws UnsupportedEncodingException {
ScrabbleSets.printRemainingTiles("AXHDRUIOR_XHJZUQEE");
assertThat(getOutputString(), is(equalTo("Invalid input. More X's have been taken from the bag than possible.\n")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment