harsimran_hs4
  • harsimran_hs4
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()
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
TuringTest
  • TuringTest
why do you have maxwords=0 ?
TuringTest
  • TuringTest
oh I see, nevermind
TuringTest
  • TuringTest
try inserting some print statements, like print every new best shift to see if you are entering all the loops

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

MicroBot
  • MicroBot
dont 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
  • harsimran_hs4
@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
  • harsimran_hs4
@MicroBot and @TuringTest Guys help please.....especially you have been quite good at clearing my problems looking forward for a reply...
MicroBot
  • MicroBot
oh 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
  • MicroBot
@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
  • harsimran_hs4
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-2/lecture-10-hashing-and-classes/ problem set 4 (The Caesar Cipher)
harsimran_hs4
  • harsimran_hs4
the 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
  • MicroBot
by 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
  • harsimran_hs4
yes !! 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
  • MicroBot
im 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
  • harsimran_hs4
ok 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
  • MicroBot
ye i actually find it very interesting:)plus debugging and understanding other ppl code is a great way to learn.
harsimran_hs4
  • harsimran_hs4
+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
  • MicroBot
i sure will!
MicroBot
  • MicroBot
ok 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
  • MicroBot
let me know how it is going:)
harsimran_hs4
  • harsimran_hs4
thanks 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
  • harsimran_hs4
and how are the test1 and test2 different from each other ?
MicroBot
  • MicroBot
goodmorning:) let me see again:)
MicroBot
  • MicroBot
ok 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
  • MicroBot
>>> 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
  • harsimran_hs4
ok 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
  • MicroBot
i am doing the 3rd test fopr you now so you can see:) ill ask a friend on what he thinks about it...
harsimran_hs4
  • harsimran_hs4
ok thanks you also continue with test3 and yes reference from another person would help....:)
MicroBot
  • MicroBot
@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
  • MicroBot
http://pastebin.com/dw32HKNY with test 3 that is the same as test 1 just with other shift
MicroBot
  • MicroBot
or even better you can change last test's shifts to this [(0,8), (3, 5), (12, 5),(21,2)]
harsimran_hs4
  • harsimran_hs4
yes 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
  • MicroBot
nono 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
harsimran_hs4
  • harsimran_hs4
ok..
MicroBot
  • MicroBot
Hint: 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
  • harsimran_hs4
isn't it what i implemented in find_best_shifts_rec() ?
MicroBot
  • MicroBot
recShifts = 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
  • MicroBot
wish smone could help us out here xD
MicroBot
  • MicroBot
id recursively run that function (find_best_shift for text[start:] where start ==word in wordlist(the next char just after it)
harsimran_hs4
  • harsimran_hs4
maybe 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
  • MicroBot
there is only a discussion forum but usually teachers do not answer but student do ..so its kinda the same.
MicroBot
  • MicroBot
and most of them are here too
MicroBot
  • MicroBot
btw @thepenguin since you are doing the same problem join us here:)
MicroBot
  • MicroBot
rethink 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
  • harsimran_hs4
yes sure!!
harsimran_hs4
  • harsimran_hs4
@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
  • harsimran_hs4
i 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
  • harsimran_hs4
this 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
  • MicroBot
uhm could you please link me to the fable.txt so i can download it somewhere?
harsimran_hs4
  • harsimran_hs4
i have already linked you to ps4 it is one of the files in zip folder there for download
MicroBot
  • MicroBot
ok sec
MicroBot
  • MicroBot
>>> ================================ 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
  • harsimran_hs4
yes it`s slow just run and it would take something around 5-10 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
  • MicroBot
ok ill let it run then
harsimran_hs4
  • harsimran_hs4
it took me around 10 min (i ran it just now).....what about you?
MicroBot
  • MicroBot
yes it finished and worked (GJ) but i still have to see smthing i dont understand
MicroBot
  • MicroBot
print '.....................' 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
  • MicroBot
if 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
  • harsimran_hs4
yes that was only for applying shifts from start of a word..... it was great experience collaborating with you Thanks!!
MicroBot
  • MicroBot
Well same goes for me!It was great doing this! Ask me anytime to join you :)
harsimran_hs4
  • harsimran_hs4
yes sure!!!

Looking for something else?

Not the answer you are looking for? Search for more explanations.