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.
I'm stuck on that same problem. Did you answer the first part with a program or just doing it by hand? I'm just trying to make sure I know where to begin on these things!
You can try using nested 'for' loops. In the order which you described, you want to cycle through all b values and then restart a at 0, and like wise you wanted to cycle through all c values before restart b at 0. Try the following for c in range(someCvalue): for b in range(someBvalue): for a in range(someAvalue): Your code to check if condition is meet. etc. first iteration will be c=0, b=0, a=0 second iteration will be c=0, b=0, a=1 someAvalue iteration will be c=0, b=0, a=someAvalue-1 someAvalue+1 iteration will be c=0, b=1, a=0 someAvalue+2 iteration will be c=0, b=1, a=1 and so on.
Forgive my ignorance, I'm just trying to figure out how to get to some of these...?
Hi avasbabygirl, what part are you trying to get to? I'm not understanding your question.
I'm just trying to figure out where to start on this one, stuyboyz. Sorry, it's been a LONG time since I wrote code (for DOS, date myself there) and I want to learn this so bad. I just feel like the lectures aren't helping with the problem sets at all...
Hi avasbabygirl, I believe you're looking for help with problem 3 which is trying to find the largest number of McNuggets that cannot be bought. If so, you should start by looking at how you would calculate the solution if a specific number (e.g. 6, or 28) is given. Then expand upon your code to look for a solution given any arbitrary number (e.g. n). After you can determine where an arbitrary number, n, has a solution or not, you just need to set n=1, n=2, n=3, ... until you find the largest n that you can't find a solution to.
Yes, it's problem 3 - the first problem said you could do it by hand or use a program and I did that one by hand, hence part of the holdup right now. I know this has to be an if code, just trying to figure out where to go from there...with the three possible numbers that could be part of the sum. Sorry I'm struggling so bad with this, just trying to get going so I can figure it out...
Hi avasbabygirl, everyone has to start somewhere, so don't apologize just because you don't understand. If you have worked through it by hand then break down what you did one step at a time. Most likely it's something like, can I find a solution using just packs of 6. No, then can I find a solution using just packs of 9. No, then can I find a solution with pack of 20. No, then can I use a combination of packs. let's say 0 packs of 20, 1 pack of 9, 2 packs of 6. No. let's try 0 packs of 20, 1 pack of 9, 3 packs of 6. etc. Key is to think about how you can get to a solution systematically. once you get that, you have your program, then it's just a matter of converting your program into python syntax code.
Ok, so something like 6n=x, then do an n+1 scenario? That I can do, just hafta figure out how to do it where it checks all three at once... x=66 (since we already did up to 65) n=1 if 6*n==x print something? elif 6*(n+1)==x I know, extremely rudimentary, just trying to learn the new language so to speak...
Hi avasbabygirl, I was trying to steer you towards a solution without giving one out. So you got a if condition to check for 6*n==x and if that doesn't work try 6*(n+1). What you need now is something to do the n+1 automatically for you. This can be done using a looping structure, e.g. a 'for' loop or a 'while' loop. For example, the code for n in range(5) would assign n=0 in the first iteration, then n=1, n=2, n=3, n=4, and stop. You can do the same thing using a 'while' loop. For example the code n = 0 while n<5 : code to check if 6*n==x or not n=n+1 You need to find the appropriate exit conditions for these loops. let's take your x as 66, you know that you can stop once n=12, because 6*n will be greater than 66. Once you have that, you can just assume, you're using 0 packs of 9 and 0 packs of 20, so you can really modify your code to check 6*n+9*b+20*c==x, where b=0 and c=0. Give that a shot.
Hint: for digit in '123': for letter in 'abc': print digit, letter # prints... 1 a 1 b 1 c 2 a 2 b 2 c 3 a 3 b 3 c
i can't seem to get it to work just yet, but i came up with this code: http://dpaste.com/538599/ with something like that I think it will check any number n that i give it. Therefore, (when i can get it to work) I think I can say something like: n= 1 while checker(n) == True: n = n + 1
Hi double_o_seven, Your code is closer, however your final 'else' clause is in the wrong place. That's the main issue. (Hint, you don't need an explicit 'else' clause). An example of implicit 'else' clause if a == 1: return True return False There are several other issues: 1. line 5, n=1 overrides whatever parameter you pass into the method. You could have left it in from testing your code. 2. lines 52 to 62 will never get executed, because you have already return from the method. The keyword "return" ends the method at that spot. You have a return in both your 'if' clause and 'else' clause, so either a return True or return False is guaranteed to happen, so your code from 52 onwards will never get executed.
Ok, I'm calling it a night for tonight, will work on this after practice tomorrow night. I hafta say, this particular code is more than a little frustrating! Oh well, will figure it out, darnit!!! :)
I attached a file above, containing a function definition that would help solve this kind of problem. I'm happy to improve it if it doesn't make things clear?
I saw it, thanks, carl. Sorry I didn't respond sooner, Wednesday is practice night and I was worn out when I got home! I'm going to look at it after I get my history work done tonight. :)
No worries. I'm kinda building up a few explanatory scripts and might as well try and polish them up and put 'em up. I might just put them all on NIXTY when I've got enough.
Ugh, I just wish there was a little more explanation in this stuff I guess...maybe I'm just getting old or missing something, but it seems like we don't get a lot of explanation or examples before these problem sets? The recitations are probably where they're getting most of those, not sure, just feel like I'm missing some explanation on how to do this somewhere...
sorry I haven't been replying. I travel a LOT sometimes so i can't exactly get online all the time. ANYWAY, during my internet silence period i got my program together. here it is: http://dpaste.com/hold/539728/ I redid some of it so it would work like an actual counter(which is what i was aiming for). thanks for your comments earlier carl, they really helped. however, im not currently satisfied with what i have since my "checker" is kinda long and dreary. I was hoping to break it down so that there would be smaller definitions inside, but that would come later i guess. anyway now im working on part b (ugh) oh and if you have any suggestions on how to make the program here pasted better PLEASE let me know
oops, slight bug in my program. :/ add this statement: adder = n after and on the same level as the notSolved = False (line 143) or else it will keep running after the answer is found. sorry bout that
From what I can tell, though I only spent a few minutes reading it, you've got the bottom part pretty clean, I like the drum roll bit :), but there's just way too much code in the checking part. It's impressive to see you just kept attacking until you've got it working, that's certainly a good attitude, but you could refine a lot of the top half, like you've said above. I'll attach my script for this, but it's got some error checks and jewellery in it so it's a bit more code than I needed, but you might find the approach to checking the permutations, which is the same as the one offered by stuyboyz above.
*** might find the approach to checking the permutations useful,
Really gotta start reading my posts before committing to them.
oops I meant to thank studyboyz for his comments. Oh well. Thanks man. I got the b part finished last night (used my original code for as the basis). http://dpaste.com/hold/539860/ I had most of the checks there from the beginning, but I added the != on line 30 because when i was looking through your code this morning and realized I hadn't dealt with floats. By the way, I like your code because it's shorter and everything(about 60 lines vs 190 lines), but the only thing I don't like is that you have used many expressions such as try, except, and the way you did the while loop on line 49 that haven't been covered in class up to this point.
Ah. The `try` and `except` statements are really useful and you can do much more clever stuff with them than what I've done here, but they're easy to understand. I didn't need them in this script, you never do, but they handle errors which can be handy of course. They work much like `if` and `else`. You do a `try` and then indent a bunch of code. The interpreter will attempt to run the code. If it throws an exception, instead of crashing a printing an error message, it'll execute the `except` block instead. try: a = open('/path/file.py', 'r') except: print 'Couldn't open file.py, file not found.' Really simple, powerful keywords. Sometimes there's no way to ensure the user will enter something sane or that conditions external to the app haven't changed ~ loss of network access, deleted files etc.. They're good things to know.
The while loop thing was perhaps not the best way to do that bit, but I think I was planning on adding more and never did in the end. It simple to follow though. I first created a variable, `loop`, that is assigned the value True. Then used it as an 'expression' in the top of the while loop. loop = True while loop: Because `loop` evaluates to True, the loop runs and will keep doing so unless `loop` stops being True which can happen anywhere I put loop = False I only did that once in the loop, but you can put as many `loop = False` statements as you need in there. It's better to use `break` for most things and that's what I should of used there, I just often use `while loop:` for stuff, it works slightly differently, `break` quits immediately, but `loop = False` finishes the block before quitting the loop. Your right though, it isn't the best way to achieve that. I'll have a look through your code and get back to you soon, it's 12.40am here (UK) so it's a bit late to do it now.
oooooh ok thanks. That should really come in handy in the future. The thing I don't like about my part b code is that if you enter a letter and not a number it returns an error. Thanks for all the info!
Just try and convert it to a number, perhaps inside a loop that'll only end if the conversion works. while True: usrin = raw_input('Enter a number: ') try: usrin = int(usrin) break except: print usrin, 'is not a number.'
Sorry, I know Im asking many questions, but what does the break statement do?
The 'break' statement allows you to exit a loop immediately. In carlsmith's example above, the 'break' statement would exit the 'while True' loop (a.k.a. infinite loop). Note, 'break' statement will only exit the inner most loop. So if you have two nested loops while loopA==True : while loopB==True: break The break would only exit loopB. You'll need a separate 'break' statement to exit loopA. The following link provide some other options to breaking out of loops http://www.daniweb.com/software-development/python/code/216789
So it's basically the same thing as saying loopB = True to exit the loop?
not exactly. The end effect is that you exit the loop, but the state in which you exit can be different. When you reach the break statement, the loop exits, no further instruction is processed, meaning loopB can be false and the loop is still forced to exit. When you set loopB=True, other statements in the loop are still processed and the loop only exit when it does the check in the 'while' loop's conditional check (i.e. at the beginning of the next iteration).
That's exactly it, break will kick you out of the loop on contact, but changing some condition to False so you exit the loop, won't drop you out of the loop until the end of the current cycle. while True: break print 'Hello' Would not print 'Hello', whereas loop = True while loop: loop = False print 'Hello' would print hello. Aside from that, both bits of code do exactly the same thing.
I put you a medal up for you stuyboyz, nice work mate. @double_o_seven It's fine, you can ask tons of questions, we all do :)
thanks for all your help. I'll be back if i need more help!
No worries. Chat soon then.