Had to do a problem where the teachers conveniently added their own test codes. I was really proud of myself for creating a nice eloquent solution that only took one parameter. The test code is this cheesy thing:
Code:
def test_get_word_score():
"""
Unit test for get_word_score
"""
failure=False
# dictionary of words and scores
words = {("", 7):0, ("it", 7):2, ("was", 7):6, ("scored", 7):9, ("waybill", 7):65, ("outgnaw", 7):61, ("outgnawn", 8):62}
for (word, n) in words.keys():
score = get_word_score(word, n)
if score != words[(word, n)]:
print "FAILURE: test_get_word_score()"
print "\tExpected", words[(word, n)], "points but got '" + str(score) + "' for word '" + word + "', n=" + str(n)
failure=True
if not failure:
print "SUCCESS: test_get_word_score()"
and of course, I wrote this function which did tested every word as it is supposed to be tested:
Code:
def get_word_score(word, n):
points = 0
for letters in word:
if letters == ' ':
points = 0
break
x = SCRABBLE_LETTER_VALUES[letters]
points = points + x
if n >= 7:
points = points + 50
return points
##word = 'it'
##word = 'outgnawn'
##word = 'scored'
##word = ' '
##word = 'outgnaw'
##word = 'was'
##word = 'waybill'
##get_word_score(word, len(word))
(I know, the testing I created is lazy and horrible)
but end up having to change
to:
Code:
if len(word) >= 7 and n >= 7:
Just so I can pass the testing. Why? Apparently, they want to have the players type in how many letters they used rather than automate it? I doubt that. Why not automate the word-counting in the function and have one parameter?
and seriusly, Python:
Code:
if len(word) and n >= 7:
is 10x better. Mmmkay?
I feel oppressed.