Skip to content

Instantly share code, notes, and snippets.

@p3t0r
Created October 9, 2010 20:18
Show Gist options
  • Save p3t0r/618565 to your computer and use it in GitHub Desktop.
Save p3t0r/618565 to your computer and use it in GitHub Desktop.
package com.log4p;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Scorer {
public static void main(String args[]) {
FileInputStream fis = null;
try {
fis = new FileInputStream(args[0]);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line;
while ((line = br.readLine()) != null) {
System.out.printf("%s,%d%n", line, score(line));
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
// ignore
}
}
}
public static int score(String word) {
int score = 0;
for (char c : word.toCharArray()) {
String key = ""+c;
if(scoreMap.containsKey(key))
score += scoreMap.get(key);
}
return score;
}
static final Map<String, Integer> scoreMap = new HashMap<String, Integer>();
static {
scoreMap.put("a", 1);
scoreMap.put("b", 3);
scoreMap.put("c", 5);
scoreMap.put("d", 2);
scoreMap.put("e", 1);
scoreMap.put("f", 4);
scoreMap.put("g", 3);
scoreMap.put("h", 4);
scoreMap.put("i", 1);
scoreMap.put("j", 4);
scoreMap.put("k", 3);
scoreMap.put("l", 3);
scoreMap.put("m", 3);
scoreMap.put("n", 1);
scoreMap.put("o", 1);
scoreMap.put("p", 3);
scoreMap.put("q", 10);
scoreMap.put("r", 2);
scoreMap.put("s", 2);
scoreMap.put("t", 2);
scoreMap.put("u", 4);
scoreMap.put("v", 4);
scoreMap.put("w", 5);
scoreMap.put("x", 8);
scoreMap.put("y", 8);
scoreMap.put("z", 4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment