-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sam Gibson
committed
Nov 13, 2009
0 parents
commit 6431014
Showing
7 changed files
with
179,410 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from random import randint | ||
|
||
# Points for each letter | ||
scores = { | ||
'a':1, 'b':3, 'c':3, 'd':2, 'e':1, 'f':4, 'g':2, 'h':4, 'i':1, | ||
'j':8, 'k':5, 'l':1, 'm':3, 'n':1, 'o':1, 'p':3, 'q':10, 'r':1, | ||
's':1, 't':1, 'u':1, 'v':4, 'w':4, 'x':8, 'y':4, 'z':10, '_':0 | ||
} | ||
|
||
# Distribution of each letter | ||
distribution = { | ||
'a':16, 'b': 4, 'c':6, 'd':8, 'e':24, 'f': 4, 'g':5, 'h':5, 'i':13, | ||
'j': 2, 'k': 2, 'l':7, 'm':6, 'n':13, 'o':15, 'p':4, 'q':2, 'r':13, | ||
's':10, 't':15, 'u':7, 'v':3, 'w': 4, 'x': 2, 'y':4, 'z':2, '_': 0 | ||
} | ||
|
||
class Bag: | ||
def __init__(self): | ||
self.bag = [] | ||
for letter, count in distribution.iteritems(): | ||
self.bag += [letter] * count | ||
|
||
def remaining(self, letter = None): | ||
if not letter: | ||
return len(self.bag) | ||
else: | ||
return len(filter(lambda c: c == letter, self.bag)) | ||
|
||
def random(self, draw = True): | ||
if len(self.bag) == 0: | ||
raise BagError("The bag is empty.") | ||
else: | ||
index = randint(0, len(self.bag)-1) | ||
|
||
c = self.bag[index] | ||
if draw: | ||
self.bag.remove(c) | ||
return c | ||
|
||
def draw(self, letter): | ||
if len(self.bag) == 0: | ||
raise BagError("The bag is empty.") | ||
elif letter not in bag: | ||
raise BagError("There are no more `%s' remaining in the bag." % letter) | ||
else: | ||
self.bag.remove(self.bag.index(letter)) |
Oops, something went wrong.