A community for students.
Here's the question you clicked on:
 0 viewing
harsimran_hs4
 3 years ago
hi guys
i am facing problem in ps4 (6.00, 2011)
in the function find_best_shifts(wordlist, text)[part 4 of ps4], it is returning none type instead of some list of tuples
can you tell me the mistake...
[this part has been solved in discussion]
the code is here:
http://pastebin.com/VvGpMb5S
NOW THE PROBLEM is to correct the function find_best_shifts()
harsimran_hs4
 3 years ago
hi guys i am facing problem in ps4 (6.00, 2011) in the function find_best_shifts(wordlist, text)[part 4 of ps4], it is returning none type instead of some list of tuples can you tell me the mistake... [this part has been solved in discussion] the code is here: http://pastebin.com/VvGpMb5S NOW THE PROBLEM is to correct the function find_best_shifts()

This Question is Closed

TuringTest
 3 years ago
Best ResponseYou've already chosen the best response.0why do you have maxwords=0 ?

TuringTest
 3 years ago
Best ResponseYou've already chosen the best response.0try inserting some print statements, like print every new best shift to see if you are entering all the loops

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1dont know if that helps(or if im wrong) but on line 345 you call : find_best_shifts_rec(wordlist, text, 0) . i believe you need a return for it since its in a def function. so it should be: return find_best_shifts_rec(wordlist, text, 0)

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1@MicroBot thanks that worked i.e it returned the value but still it`s not giving the correct output as given in the problem set. this portion >>> s = apply_shifts("Do Androids Dream of Electric Sheep?", [(0,6), (3, 18), (12, 16)]) >>> s 'JufYkaolfapxQdrnzmasmRyrpfdvpmEurrb?' >>> shifts = find_best_shifts(wordlist, s) >>> print apply_shifts(s, shifts) Do Androids Dream of Electric Sheep? but it giving something weird, please can you tell the mistake that still exists....

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1@MicroBot and @TuringTest Guys help please.....especially you have been quite good at clearing my problems looking forward for a reply...

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1oh sry was late when i read it and i said ill respond tomorrow! then i forgot xD let me see if i can figure smthing out!

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1@harsimran_hs4 can you link me to the problelm set ? as im not doint this course ...just did a similar one on edx courses.

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1http://ocw.mit.edu/courses/electricalengineeringandcomputerscience/600scintroductiontocomputerscienceandprogrammingspring2011/unit2/lecture10hashingandclasses/ problem set 4 (The Caesar Cipher)

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1the test which i mentioned above i did return the list of tuples with six elements of which 1st three were correct and the other three were extra ....maybe this will help you in figuring out the problem

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1by loading your code i get this: Loading word list from file... 55909 words loaded. recShifts : [(30, 0)] recShifts : [(30, 0)] recShifts : [(21, 0), (30, 0)] recShifts : [(21, 0), (30, 0)] recShifts : [(18, 0), (21, 0), (30, 0)] recShifts : [(18, 0), (21, 0), (30, 0)] recShifts : [(12, 16), (18, 0), (21, 0), (30, 0)] recShifts : [(12, 16), (18, 0), (21, 0), (30, 0)] recShifts : [(3, 18), (12, 16), (18, 0), (21, 0), (30, 0)] recShifts : [(3, 18), (12, 16), (18, 0), (21, 0), (30, 0)] is it the same you get?

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1yes !! if you comment out the print statement then it returns [(3, 18), (12, 16), (18, 0), (21, 0), (30, 0)] this is the test i was running : >>> s = apply_shifts("Do Androids Dream of Electric Sheep?", [(0,6), (3, 18), (12, 16)]) >>> s 'JufYkaolfapxQdrnzmasmRyrpfdvpmEurrb?' >>> shifts = find_best_shifts(wordlist, s) #here i should get the same shifts i used to code it >>> print apply_shifts(s, shifts) Do Androids Dream of Electric Sheep? And use this code http://pastebin.com/NDxdNMJB it will return [(0, 6), (3, 18), (12, 16), (18, 0), (21, 0), (30, 0)] a little better than before as it`s returning (0,6) also

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1im trying it right now(forgive me if im slow but its new to me aswell since for my course i didnt have the multiple encryption part)

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1ok Thanks for taking pain to help me out ... you will surely enjoy this part of multiple encryption it's quite interesting and maybe a little time consuming also...

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1ye i actually find it very interesting:)plus debugging and understanding other ppl code is a great way to learn.

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1+1 for {plus debugging and understanding other ppl code is a great way to learn.} Yes indeed thats great way to learn do message me whenever you get to a solution !!!

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1ok this is what i see: see this code (its your code with more print statements and more test cases): http://pastebin.com/LZ3attfE i believe you have written your function around the test case you were doing ...means your code searches the best shifts for starters that are after ' '(space chars) only....you can see that if you put different starters that are not space chars. Thats why you get the 3 starters that you dont need....cause it searches every ' ' char in the string. sry i explain bad ,,,but if you see the code and the output i believe youll understand what i mean.

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1let me know how it is going:)

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1thanks but can you explain " you can see that if you put different starters that are not space chars." as far as i get it is giving the last step i.e the first start point as three outputs where as we don`t need all.....is it this bug you are talking about? and also we need to apply the shifts we get from find_best_shifts(wordlist, s) in the negetive order...is it so?

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1and how are the test1 and test2 different from each other ?

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1goodmorning:) let me see again:)

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1ok 1st how the test 1 is different from test 2: on test1 you apply shifts starting from the char after every space in the string. and your findbestshifts is based on that!(that the code is applied after every space in the string. on test 2 you apply shifts on random chars in the string... does it sound better now?

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1>>> Loading word list from file... 55909 words loaded. shifts : [(0,6), (3, 18), (12, 16)] s : JufYkaolfapxQdrnzmasmRyrpfdvpmEurrb? space is at index: 2 space is at index: 11 space is at index: 17 space is at index: 20 space is at index: 29 recShifts1 : [(30, 0)] recShifts2 : [(30, 0)] recShifts1 : [(21, 0), (30, 0)] recShifts2 : [(21, 0), (30, 0)] recShifts1 : [(18, 0), (21, 0), (30, 0)] recShifts2 : [(18, 0), (21, 0), (30, 0)] recShifts1 : [(12, 16), (18, 0), (21, 0), (30, 0)] recShifts2 : [(12, 16), (18, 0), (21, 0), (30, 0)] recShifts1 : [(3, 18), (12, 16), (18, 0), (21, 0), (30, 0)] recShifts2 : [(3, 18), (12, 16), (18, 0), (21, 0), (30, 0)] bestSHIFTS : [(0, 6), (3, 18), (12, 16), (18, 0), (21, 0), (30, 0)] ..................... shifts : [(2,6), (5, 18), (10, 16)] s2 : DofGtaolfaemQdrnzmasmRyrpfdvpmEurrb? SHIFTS : None >>> this is what i get by applying those 2 tests

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1ok i got it.....now i think i will have review the code again , a little more deep insight into it would help (get back soon with the test3 ) do keep writing of the progress you make in this problem

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1i am doing the 3rd test fopr you now so you can see:) ill ask a friend on what he thinks about it...

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1ok thanks you also continue with test3 and yes reference from another person would help....:)

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1@harsimran_hs4 do you too see that you implemented the code just for the test 1 to come true? if you see smthing else say so...

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1http://pastebin.com/dw32HKNY with test 3 that is the same as test 1 just with other shift

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1or even better you can change last test's shifts to this [(0,8), (3, 5), (12, 5),(21,2)]

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1yes i wrote it to come true only for those which start at spaces........ so that means it will not work for test 2 and test 3 in test 3 you have applied shifts which start at the spaces, is it so ? how did you deduce [(0,8), (3, 5), (12, 5),(21,2)] from [(0, 8), (3, 5), (12, 5), (18, 0), (21, 0), (30, 0)]

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1nono just chenge the test ..thats what i mean....21 is a char just after a space since in that specific text the spaces are on: space is at index: 2 space is at index: 11 space is at index: 17 space is at index: 20 space is at index: 29 as you can see in your output so i just ask the program to apply shift there aswell

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1Hint: You will find this function much easier to implement if you use recursion. (as said in the comments) i think you should use recursive function to find_best_shift function (not find_best_shiftS)

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1isn't it what i implemented in find_best_shifts_rec() ?

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1recShifts = find_best_shifts_rec(wordlist, new_string, i+1) this you mean? no i mean the find_best_shift(wordlist,text) line 249 if that can help....just dont know how!:P

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1wish smone could help us out here xD

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1id recursively run that function (find_best_shift for text[start:] where start ==word in wordlist(the next char just after it)

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1maybe i`ll have to approach the question with a somewhat better approach using all the suggestions in the discussion. hey you are taking Edx course ...if yes then do you have any provision of asking mentors or teachers....maybe they could help and advice us better

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1there is only a discussion forum but usually teachers do not answer but student do ..so its kinda the same.

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1and most of them are here too

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1btw @thepenguin since you are doing the same problem join us here:)

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1rethink it a bit ...i have to go study for edx:P xD when you have some new ideas post it here so we can discuss:)

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1@MicroBot the problem was what we got when we did >>> s = apply_shifts("Do Androids Dream of Electric Sheep?", [(0,6), (3, 18), (12, 16)]) >>> s 'JufYkaolfapxQdrnzmasmRyrpfdvpmEurrb?' >>> shifts = find_best_shifts(wordlist, s) NOTE:*look below >>> print apply_shifts(s, shifts) Do Androids Dream of Electric Sheep? NOTE: before apply shifts we need to reverse them and take the negetive of the shift (as we are doing the reverse process of encrypting i.e deciphering) we got [(0, 8), (3, 5), (12, 5), (18, 0), (21, 0), (30, 0)] hence we must generate a list say correct = [(30, 0), (21, 0), (18, 0), (12, 5), (3, 5), (0, 8)] and then print apply_shifts(s, correct) and this works pretty well i hope you get what i am saying as i don`t explain that well

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1i deciphered the text file fable this is what i got : An Ingenious Man who had built a flying machine invited a great concourse of people to see it go up. at the appointed moment, everything being ready, he boarded the car and turned a a he power. the machine immediately broke through the massive substructure upon which it was builded, and sank out of sight into the earth, the aeronaut springing out barely in time to save himself. "well," said he, "i have done enough to demonstrate the correctness of my details. the defects," he added, with a add hat the ruined brick work, "are merely basic and fundamental." upon this assurance the people came ox ward with subscriptions to build a second machine

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1this is the corrected code: http://pastebin.com/axHn8mLH i have made changes in find_best_shifts() in the code in test1 after line 392 just add print apply_shifts(s, shifts) output is : Loading word list from file... 55909 words loaded. shifts : [(0,6), (3, 18), (12, 16)] s : JufYkaolfapxQdrnzmasmRyrpfdvpmEurrb? bestSHIFTS : [(30, 0), (21, 0), (18, 0), (12, 16), (3, 18), (0, 6)] Do Androids Dream of Electric Sheep? .....................

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1uhm could you please link me to the fable.txt so i can download it somewhere?

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1i have already linked you to ps4 it is one of the files in zip folder there for download

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1>>> ================================ RESTART ================================ >>> Loading word list from file... 55909 words loaded. >>> print decrypt_fable() i get stuck there...it just stays like this wont do anything...not sure if its veeeeeeeeeeeeeery slow or what.

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1yes it`s slow just run and it would take something around 510 min i suppose to run {for the length of trials : just try and print the find_best_shifts() and you will find that the length of the list is quite large....so perhaps the fable has been coded nicely :)}

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1ok ill let it run then

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1it took me around 10 min (i ran it just now).....what about you?

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1yes it finished and worked (GJ) but i still have to see smthing i dont understand

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1print '.....................' s = random_scrambled(wordlist, 3) print s shifts = find_best_shifts(wordlist, s) print 'bestSHIFTS : ',shifts print apply_shifts(s, shifts) print '......................' doing this test seems to work perfectly!congratz!

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1if im not reading wrong ,random_scrambled its applying shifts that are after each space char aswell ,so yes then you were totaly right to write findbestshifts for space chars! so it should only work for that ...i didnt know thats why i was testing it for any index in the string.

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1yes that was only for applying shifts from start of a word..... it was great experience collaborating with you Thanks!!

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1Well same goes for me!It was great doing this! Ask me anytime to join you :)
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.