Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whales C17- Nishat Salsabil and Julie Warren Adagrams #43

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
38 changes: 38 additions & 0 deletions adagrams/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
LETTER_POOL = {
'A': 9,
'B': 2,
'C': 2,
'D': 4,
'E': 12,
'F': 2,
'G': 3,
'H': 2,
'I': 9,
'J': 1,
'K': 1,
'L': 4,
'M': 2,
'N': 6,
'O': 8,
'P': 2,
'Q': 1,
'R': 6,
'S': 4,
'T': 6,
'U': 4,
'V': 2,
'W': 2,
'X': 1,
'Y': 2,
'Z': 1
}

LETTER_SCORE_DICT = {
1: ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"],
2: ["D", "G"],
3: ["B", "C", "M", "P"],
4: ["F", "H", "V", "W", "Y"],
5: ["K"],
8: ["J", "X"],
10: ["Q", "Z"]
}
81 changes: 75 additions & 6 deletions adagrams/game.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,80 @@
import random
import copy
from adagrams.constants import LETTER_POOL, LETTER_SCORE_DICT

def draw_letters():
pass
'''
input: none

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay docstrings!

output: return list of 10 strings, 1 letter each
frequency of each letter cannot exceed the value of
each letter in the LETTER_POOL
'''
hand = []
letter_freq = {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent use of a dictionary to keep track of letters and their frequencies


while len(hand) < 10:
random_letter = random.choice(list(LETTER_POOL))
if random_letter in letter_freq:
if letter_freq[random_letter] < LETTER_POOL[random_letter]:
letter_freq[random_letter] += 1
hand.append(random_letter)
else:
letter_freq[random_letter] = 1
hand.append(random_letter)
return(hand)


def uses_available_letters(word, hand):
'''
input: word (a string) and hand (a list of strings, one char each)
output: Returns True if each char is uniquely in hand. Returns
False otherwise or if char in word is not in hand.
'''
word = word.upper()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This will not introduce a side effect because strings are immutable!

hand_copy = copy.deepcopy(hand)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great way to avoid side effects!

for letter in word:
if letter in hand_copy:
hand_copy.remove(letter)
elif letter not in hand_copy:
return False
return True

def uses_available_letters(word, letter_bank):
pass

def score_word(word):
pass
'''
input: word (a string of characters)
output: Returns a total score based on the value of each char in
word, as defined in LETTER_SCORE_DICT. Words between 7 and 10
char score an extra 8 points.
'''
score = 0
word = word.upper()
for letter in word:
for key, value in LETTER_SCORE_DICT.items():
if letter in value:
score += key
if 7 <= len(word) <= 10:
score += 8
return score


def get_highest_word_score(word_list):
'''
input: list of strings representing each word user has created
output: returns tuple with highest scoring word and score. If tied:
shortest length word is preferred, unless length is 10 char
'''
played_word_dict = {}
for word in word_list:
score = score_word(word)
played_word_dict[word] = score

highest_score = max(played_word_dict.values())
best_word_list = [key for key, value in played_word_dict.items()\

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely list comprehension!

if value == highest_score]

def get_highest_word_score(word_list):
pass
if len(best_word_list) > 1:
for word in best_word_list:
if len(word) == 10:
return word, highest_score
return min(best_word_list, key=len), highest_score
3 changes: 2 additions & 1 deletion adagrams/ui_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ def display_goodbye_message():
# is the word and the second element is the score.
def display_highest_score(word_score):
print("Thanks for playing Adagrams!")
print(f"The highest score from this game was {word_score[0]}, which was worth {word_score[1]} points.")
print(f"The highest score from this game was {word_score[0]}, which was worth {word_score[1]} points.")

4 changes: 4 additions & 0 deletions tests/test_wave_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
'Z': 1
}

# Test 1
def test_draw_letters_draws_ten():
# Arrange/Act
letters = draw_letters()

# Assert
assert len(letters) == 10

# Test 2
def test_draw_letters_is_list_of_letter_strings():
# Arrange/Act
letters = draw_letters()
Expand All @@ -49,13 +51,15 @@ def test_draw_letters_is_list_of_letter_strings():
assert type(elem) == str
assert len(elem) == 1

# Test 3
def test_letter_not_selected_too_many_times():

for i in range(1000):
# Arrange/Act
letters = draw_letters()

letter_freq = {}
#for each letter in your hand, count how many times it shows up
for letter in letters:
if letter in letter_freq:
letter_freq[letter] += 1
Expand Down
5 changes: 5 additions & 0 deletions tests/test_wave_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from adagrams.game import uses_available_letters

# Test 4
def test_uses_available_letters_true_word_in_letter_bank():
# Arrange
letters = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"]
Expand All @@ -13,6 +14,7 @@ def test_uses_available_letters_true_word_in_letter_bank():
# Assert
assert is_valid == True

# Test 5
def test_uses_available_letters_false_word_in_letter_bank():
# Arrange
letters = ["D", "O", "X", "X", "X", "X", "X", "X", "X", "X"]
Expand All @@ -24,6 +26,7 @@ def test_uses_available_letters_false_word_in_letter_bank():
# Assert
assert is_valid == False

# Test 6
def test_uses_available_letters_false_word_overuses_letter():
# Arrange
letters = ["A", "X", "X", "X", "X", "X", "X", "X", "X", "X"]
Expand All @@ -35,6 +38,7 @@ def test_uses_available_letters_false_word_overuses_letter():
# Assert
assert is_valid == False

# Test 7
def test_uses_available_letters_does_not_change_letter_bank():
# Arrange
letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
Expand All @@ -48,6 +52,7 @@ def test_uses_available_letters_does_not_change_letter_bank():
assert is_valid == True
assert letters == letters_copy

# Test 8
def test_uses_available_letters_ignores_case():
# Arrange
letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
Expand Down
4 changes: 4 additions & 0 deletions tests/test_wave_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

from adagrams.game import score_word

# Test 9
def test_score_word_accurate():
# Assert
assert score_word("A") == 1
assert score_word("DOG") == 5
assert score_word("WHIMSY") == 17

# Test 10
def test_score_word_accurate_ignores_case():
# Assert
assert score_word("a") == 1
assert score_word("dog") == 5
assert score_word("wHiMsY") == 17

# Test 11
def test_score_zero_for_empty():
# Assert
assert score_word("") == 0

# Test 12
def test_score_extra_points_for_seven_or_longer():
# Assert
assert score_word("XXXXXXX") == 64
Expand Down
7 changes: 7 additions & 0 deletions tests/test_wave_04.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from adagrams.game import score_word, get_highest_word_score

# Test 13
def test_get_highest_word_score_accurate():
# Arrange
words = ["X", "XX", "XXX", "XXXX"]
Expand All @@ -14,6 +15,7 @@ def test_get_highest_word_score_accurate():
assert best_word[0] == "XXXX"
assert best_word[1] == 32

# Test 14
def test_get_highest_word_score_accurate_unsorted_list():
# Arrange
words = ["XXX", "XXXX", "XX", "X"]
Expand All @@ -25,6 +27,7 @@ def test_get_highest_word_score_accurate_unsorted_list():
assert best_word[0] == "XXXX"
assert best_word[1] == 32

# Test 15
def test_get_highest_word_tie_prefers_shorter_word():
# Arrange
words = ["MMMM", "WWW"]
Expand All @@ -38,6 +41,7 @@ def test_get_highest_word_tie_prefers_shorter_word():
assert best_word[0] == "WWW"
assert best_word[1] == 12

# Test 16
def test_get_highest_word_tie_prefers_shorter_word_unsorted_list():
# Arrange
words = ["WWW", "MMMM"]
Expand All @@ -51,6 +55,7 @@ def test_get_highest_word_tie_prefers_shorter_word_unsorted_list():
assert best_word[0] == "WWW"
assert best_word[1] == 12

# Test 17
def test_get_highest_word_tie_prefers_ten_letters():
# Arrange
words = ["AAAAAAAAAA", "BBBBBB"]
Expand All @@ -62,6 +67,7 @@ def test_get_highest_word_tie_prefers_ten_letters():
assert best_word[0] == "AAAAAAAAAA"
assert best_word[1] == 18

# Test 18
def test_get_highest_word_tie_prefers_ten_letters_unsorted_list():
# Arrange
words = ["BBBBBB", "AAAAAAAAAA"]
Expand All @@ -73,6 +79,7 @@ def test_get_highest_word_tie_prefers_ten_letters_unsorted_list():
assert best_word[0] == "AAAAAAAAAA"
assert best_word[1] == 18

# Test 19
def test_get_highest_word_tie_same_length_prefers_first():
# Arrange
words = ["AAAAAAAAAA", "EEEEEEEEEE"]
Expand Down