Is anyone having problems with test_py5? I'm trying to run the is_word_valid def, which manually works with all the words in the test. When I run the test, I get code errors.
def is_valid_word(word, hand, word_list): """ Returns True if word is in the word_list and is entirely composed of letters in the hand. Otherwise, returns False. Does not mutate hand or word_list. word: string hand: dictionary (string -> int) word_list: list of lowercase strings """ count=0 v=0 lenwordlist=len(word_list) valid=False value=0 lenword=len(word) for k in range(0,lenword): if word[k] in hand is None: value=value else: v=word[k] if word.count(word[k]) <= hand[v]: value=value+1 if value==lenword: valid=True for z in range(0,lenwordlist): if word == word_list[z]: count=count+1 if count > 0 and valid == True: valid=True else: valid=False return valid
def test_is_valid_word(word_list): """ Unit test for is_valid_word """ failure=False # test 1 word = "hello" hand = get_frequency_dict(word) if not is_valid_word(word, hand, word_list): print "FAILURE: test_is_valid_word()" print "\tExpected True, but got False for word: '" + word + "' and hand:", hand failure = True # test 2 hand = {'r': 1, 'a': 3, 'p': 2, 'e': 1, 't': 1, 'u':1} word = "rapture" if is_valid_word(word, hand, word_list): print "FAILURE: test_is_valid_word()" print "\tExpected False, but got True for word: '" + word + "' and hand:", hand failure = True # test 3 hand = {'n': 1, 'h': 1, 'o': 1, 'y': 1, 'd':1, 'w':1, 'e': 2} word = "honey" if not is_valid_word(word, hand, word_list): print "FAILURE: test_is_valid_word()" print "\tExpected True, but got False for word: '"+ word +"' and hand:", hand failure = True # test 4 hand = {'r': 1, 'a': 3, 'p': 2, 't': 1, 'u':2} word = "honey" if is_valid_word(word, hand, word_list): print "FAILURE: test_is_valid_word()" print "\tExpected False, but got True for word: '" + word + "' and hand:", hand failure = True # test 5 hand = {'e':1, 'v':2, 'n':1, 'i':1, 'l':2} word = "evil" if not is_valid_word(word, hand, word_list): print "FAILURE: test_is_valid_word()" print "\tExpected True, but got False for word: '" + word + "' and hand:", hand failure = True # test 6 word = "even" if is_valid_word(word, hand, word_list): print "FAILURE: test_is_valid_word()" print "\tExpected False, but got True for word: '" + word + "' and hand:", hand print "\t(If this is the only failure, make sure is_valid_word() isn't mutating its inputs)" failure = True if not failure: print "SUCCESS: test_is_valid_word()"
use dpaste.com to paste your code then post the link here - thnx
my code should be ps5.py in order to get the test running well/
I think I know where the problem is......... with the word 'honey'......
couple of suggestions, a string is iterable. also, you can test for the presence of a thing in a list by using 'in'. see this: http://dpaste.com/553873/
i was surprised that there are some common words missing from the wordlist that they gave us - it was driving me crazy till i looked through the text file and found that they were not there. Comparisons in-line docs link : http://docs.python.org/reference/expressions.html?highlight=comparisons#notin in and notin operators are described near the end of that section
Thanks !!!!!!!!1 :)
does it work? looks like it might. again at line 18; any sequence is iterable, so unless you need to keep track of the index during the loop you don't need to use the range function - you could use: "for word2 in word_list:" to get each item in that list sequentially. dictionaries are also iterable - but the sequence that the items are returned is not in any definable order
Join our real-time social learning platform and learn together with your friends!