Having some trouble with PS3, part 3 testing valid words. Not sure where my code is going wrong but I cant shake the test failure.
My code: 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 """ hand2 = get_frequency_dict(word) if word in word_list: for x in hand2: if hand2[x] <=hand[x]: return 'True' else: return 'False'
The failures: FAILURE: test_is_valid_word() Expected True, but got False for word: 'hello' and hand: {'l': 2, 'h': 1, 'e': 1, 'o': 1} FAILURE: test_is_valid_word() Expected True, but got False for word: 'honey' and hand: {'d': 1, 'h': 1, 'o': 1, 'y': 1, 'n': 1, 'e': 2, 'w': 1} FAILURE: test_is_valid_word() Expected True, but got False for word: 'evil' and hand: {'l': 2, 'i': 1, 'n': 1, 'e': 1, 'v': 2}
I don't know python, but I'll post some logic of how I'd solve it ``` function isValidWord(word, hand, word_list) { var iterator = 0; //first, make sure word contains only characters in hand while iterator < word.length { if( character at position iterator, in string word is not contained in hand ) { return false; //can check if the character is in hand or not by looping over all the characters //in hand, and if there is never a match, then return false } iterator = iterator+1; } //second make sure word is a word contained in word_list iterator = 0; looping over all elements of word_list { if(word == word_list[ position iterator ]) { //then at this point we know it passes the hand test, and wordlist test return true; } iterator = iterator+1; } //at this point in code, it passed hand test, but no matches were found in //the wordlist return false; } ``` It's always helpful to print out information throughout the program to help debug too. I know someone who reads python would be a much bigger help, but hopefully this helps somewhat.
I'm not an expert in Python, but I think your problem is that you're making the unsupported assumption that hand[x] and hand2[x] represent the same letter.
Welp, I think the problem is that your function returns the strings 'True' or 'False', when it should return the booleans True or False ;) The test tells you it got False, but I'm guessing it would have said that if your function had returned anything that wasn't the boolean True.
@kizolk, Python isn't a strongly types language. false and "False" evaluate the same. @shh, After studying the problem a bit more, my answer isn't right either. Does the program have tests that do pass? Can you output the dictionary generated by get_frequency_dict(word) so you can manually compare them and make sure you're getting the expected results?
I changed from string to boolean and did some print statements to see if the values match, which they did. There is no mutation going on in the hands. Also something I noticed: I looked at the solutions posted for the class, and even when I implement their defined code, I get a similar failure, except it was returning all true instead of all false like mine is. The only thing I can think of is something to do with using python 3 ( what I use ) and them using python 2.5.x. I know print and input are different between the two, but not sure what else. Will try to rewrite it for python 2.5.x when I get home and post the results.
I'd look very closely at the code where the inputs/parameters don't provide the output you expect, use a lot of print statements during the input/output in question, and make sure that each line of code is acting as you intend it to.
Okay, found the solution to the problem. Turns out it is an error in going from 2.x to 3.x, the solution can be seen in this post: http://forums.xkcd.com/viewtopic.php?f=20&t=72920#p2973046
Thanks for all your help and ideas in solving this bug!
Join our real-time social learning platform and learn together with your friends!