in quiz 1 of 6.00SC. ques no. 4 the solution code given for the problem has some semantic error. the output is not what we expect it to give. my solution code to the problem is very long. does anyone have a smaller code for the problem?
what i coded """ Function requitrements: assumes: wordList is a list of words in lowercase. lStr is a str of lowercase letters. No letter occurs in lStr more than once returns: a list of all the words in wordList that contain each of the letters in lStr exactly once and no letters not in lStr. """ def findAll1 (wordlist,letters): """ 1.the function first extracts all the words from the wordlist having length=length of letters. 2.then all the words of length letters, having repeated characters in them are removed. 3.now the words passing through the above 2 conditions are compared with letters.If all the charecters in word are present in letters,b=len(letters) and word is added to the list. 4.return list """ result=[] for word in wordlist: #word='act' if len(word)==len(letters): #print word b=0 index=0 while index<len(letters)-1: inc=1 while index+inc<len(letters): if word[index]==word[index+inc]: #print word b+=1 inc+=1 index+=1 if b==0: index=0 while index<len(letters): inc=0 while(inc<len(letters)): if word[index]==letters[inc]: b+=1 inc+=1 index+=1 if b==len(word): result.append(word) return result
That file you linked has a solution in it. I ran it. http://dpaste.com/hold/1273682/ The wording is a bit tricky, so it may be that you did not understand the intent.
yeah. I still can't understand the program written in the solution.What i understood from the discription is that the function will return all the words from wordlist having alphabets present in letters. example: if letters contain 'opt', the function should print 'pot','opt','top'..(only if all the three words are present in wordlist). the 2nd print statement you used in ur program gives 'tils' as letters to the function and since the wordlist has the word 'list' in it, i think list should be returned. but its not. or else the fuction means something else.
You are right, the posted solution doesn't work quite right. I corrected it I believe like this: http://pastebin.com/p1tvNmcs . The posted solution sorts every word in the word list and compares to the sorted letters if they match this is a "hit". The problem is instead of returning a list of words that match (like it should) it returns the matching words after they are sorted (i.e. don't look like the matching words anymore) Try what I posted that should word correctly for you.
Now I see what you mean. OK. There is even a slightly shorter fix than what dmahler did. No need to store the sorted word if it is just used for the test! http://dpaste.com/hold/1274149/
letters = sorted(letters) makes sense because that means you only call the sort function once for that list of letters. However, for each word, they need to be sorted and are only used once. That means it is actually a waste to stave it in a variable for a singe use. How big if a waste? Umm... like a drop in the ocean. If we have like a trillion drops, it would mean something. For one little operation, it does not really matter and my fix to that part is no better than dmahler's. But it is worth seeing.
Thank You guys for your clarification. It really was a small error but it changed the output completely. @e.mccormick the code that you posted the first time,i.e. the solution given in pdf and the actual solution you pasted, has a very minute difference. http://dpaste.com/hold/1274211/ if we don't use an additional variable to store sorted(w), the output is altered. i know if we just remove the additional variable and compare letter with sorted(w), it saves memory and gives correct output. But why is it not working if we use variable 'w' in place of 'sortw'??
I got the answer to that question. In the solution given in the pdf. statement 'w=sorted(w)',makes w a list with elements sorted. So, if w was word- 'list' initially, it becomes ['i','l','s','t']. so does all the words having such characters. and simply these lists r appended to the result. object string from w gets overridden by the sorted(w), and we append that .
@sahilpatel EXACTLY! It is replacing w, which is not needed when you want the word.
Join our real-time social learning platform and learn together with your friends!