My question concerns problem 4 in problem set 4 for the 2011 course. As many of you know, the solutions posted on MIT's page do not fit the problems, and after two days of banging my head against problem 4 I've reached a point where I am simply forced to admit that solving it is beyond my abilities. However, I would very much like to see a solution for it and as MIT have not provided one, I was hoping that some of you might have instead? If you do not, but have a hint that might help me along the way, I will be very grateful for that, too!
have you tried writing down in words the steps you need to take to solve the problem? - that helps alot when you get stuck. You can use it as an outline for your coding - it helps you organize the problem into seperate logical chunks. if you want to post what you have (using a code pasting site (like http://dpaste.com) and tell us what you think the problem is maybe we can help. or, i'll post my solution in a bit if really want.
Thank you for the reply. I have written out my own pseudocode and tried implementing the pseudocode provided by MIT, but after two days of working on the problem nothing has turned out right. I would post my code but I deleted my two previous iterations in hope that rewriting the code from scratch would make things clearer (it didn't). The steps are right but I can't implement them. My problem seemed to be the start parameter and how it worked in the recursive function call. After inserting a few print statements I noticed that it always looked in the wrong place of the string, but even after hours of trying I wasn't able to solve it. Inserting +1 or -1 made it look several spaces from where I wanted it to start looking (the index of the space it had found). Whatever the cause was, I've admitted that I am simply not good enough to crack this problem right now. Instead of writing out some more of my crappy spaghetti code for you to look at, I would much rather look at some efficient implementations (your solutions to P1-P3 were fantastic!).
it's a multi-layer shift. my approach was: i assumed that the first shift was applied from index 0 then the second shift was applied later in the string and the third shift was applied some place after the second shift starting index .... They also told us that shifts were applied on 'word boundaries'. This means that there are a (finite) number of words at the beginning of the string that only have one shift - so find the shift that produces the most number of words at the beginning of the string. if multiple shifts produce the same number of words (at the beginning of the string) use the one that produces the longest word. now remove that first part of the string that has been decoded and save it. do the same thing to the Rest of the string. Sometimes the busy-work part of the code (keeping track of a lot of stuff, administrative housekeeping stuff, comparing things) obscures the algorithm you are trying to implement. when this happens it makes it hard to see if you are implementing it the way you imagined. when this starts to happen it helps to write separate functions (or classes) to handle that stuff. if you give those functions (or classes) descriptive names, when you implement your algorithm it is very easy to read it and see what you are doing. I decided to make a class that would hold the result of applying a shift and had comparison methods. I wrote this about a year after I had gone through the 2008 course And after I had finished the EDx course...... http://dpaste.com/1310643/ and after looking at it, looks like it could be improved
Your solution works great, thank you! I will keep your tips in mind when writing new code, and pore over the code you have provided to compare your (cleaner and more efficient) solutions to mine. On reflection, I think the course might be a tad too hard for me right now as it seems to assume a greater level of programming competence than I first believed.
Join our real-time social learning platform and learn together with your friends!