Skip to content

Instantly share code, notes, and snippets.

@p3t0r
Created October 9, 2010 19:49
Show Gist options
  • Save p3t0r/618540 to your computer and use it in GitHub Desktop.
Save p3t0r/618540 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;
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()) {
score += scoreChar(c);
}
return score;
}
public static int scoreChar(char c) {
switch(c) {
case 'a': return 1;
case 'b': return 3;
case 'c': return 5;
case 'd': return 2;
case 'e': return 1;
case 'f': return 4;
case 'g': return 3;
case 'h': return 4;
case 'i': return 1;
case 'j': return 4;
case 'k': return 3;
case 'l': return 3;
case 'm': return 3;
case 'n': return 1;
case 'o': return 1;
case 'p': return 3;
case 'q': return 10;
case 'r': return 2;
case 's': return 2;
case 't': return 2;
case 'u': return 4;
case 'v': return 4;
case 'w': return 5;
case 'x': return 8;
case 'y': return 8;
case 'z': return 4;
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment