Skip to content

Instantly share code, notes, and snippets.

@TheBlackParrot
Created December 3, 2016 12:02
Show Gist options
  • Save TheBlackParrot/62ddece95e412183fd275d5e535c20e6 to your computer and use it in GitHub Desktop.
Save TheBlackParrot/62ddece95e412183fd275d5e535c20e6 to your computer and use it in GitHub Desktop.
sort a text file's words by value in Scrabble
#!/usr/bin/python
import sys;
points = {};
for char in "eaionrtlsu":
points[char] = 1;
for char in "dg":
points[char] = 2;
for char in "bcmp":
points[char] = 3;
for char in "fhvwy":
points[char] = 4;
points["k"] = 5;
for char in "jx":
points[char] = 8;
for char in "qz":
points[char] = 10;
def normalize(data):
return ''.join(char.lower() for char in data if char.isalnum());
def getScrabbleValue(word):
value = 0;
for char in word:
value += points[char];
return value;
with open(sys.argv[1], "r") as file:
lines = [_.strip() for _ in file];
used = {};
for line in lines:
for word in line.split():
normal = normalize(word);
if len(normal) > 1 and normal not in used:
used[normal] = getScrabbleValue(normal);
for word in sorted(used, key=used.get, reverse=True):
print("{}: {}".format(word, used[word]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment