A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 4 years ago
Just looking to verify the ps2 problems and make sure my program is generating the right answer and looking for any general feedback.
If someone could provide some insight into the actual proof of the theorem that would be great, my math is incredibly rusty and while I can pen and paper and see that it is true I'm having trouble explaining it.
code:
http://pastebin.com/Rep3pFLw
anonymous
 4 years ago
Just looking to verify the ps2 problems and make sure my program is generating the right answer and looking for any general feedback. If someone could provide some insight into the actual proof of the theorem that would be great, my math is incredibly rusty and while I can pen and paper and see that it is true I'm having trouble explaining it. code: http://pastebin.com/Rep3pFLw

This Question is Closed

maitre_kaio
 4 years ago
Best ResponseYou've already chosen the best response.1Your code seems pretty good. I particularly like the way you use a function to solve a part of the problem and the way you use variable names that make sense. All this makes the code easy to read. I didn't know you could do something like findHighestImpossibleCombo(*packageSizes). Do you have a reference explaining how it works ? I noted 2 things though: 1. In findHighestImpossibleCombo(), you are passing the package sizes. But sadly, in the function you are using constants where you could reuse the package sizes. For example: comboNumberTest = x; # start at the lowest possible combo of nuggets which is 6 while possibleCount < x: This way, if the package sizes change, you don't have to change the code inside the function 2. In isComboPossible(), you are mixing 2 return types. When you find a solution, your return a tuple, when not you return a boolean. It is better to be coherent, always return a boolean, or always return a value (empty tuple or None if not found).

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0Thanks for the feedback. I was wondering if there was an easier way to pass the values of a tuple to a function instead of referencing each individual value and was able to find http://stackoverflow.com/questions/6699592/howtopasstupleasparametersofafunction I'm not sure if in python you are passing a true reference or not, i'll have to spend more time reading the docs. Also in regards to isComboPossible you're correct about the return types however I wasn't sure if that was a feature of the language or not. Normally I would consider it best practice to use a flag and then base return type around that but I've run into some weird things in python so I wasn't quite sure what to do in this case. Best practices in mind I'll definitely remember that in the future. Not sure why I used a constant in findHighestImpossibleCombo when I had already gone to the trouble of making isComboPossible() able to handle generic cases. Thanks for pointing that out! Gotta say python is definitely one of the most interesting languages I've worked with.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0@kl365. "I was wondering if there was an easier way to pass the values of a tuple to a function instead of referencing each individual value." Don't understand your question. Tuples can be used as arguments for functions just like other data objects. You don't need to reference individual values. For example, the following is perfectly valid. def tupletest(tup): print tup mytuple=(2,4,6,8,10) tupletest(mytuple)

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0An elegant answer, my code is quite messy compared to yours! I also haven't utilized functions because they were not yet included in the lecture (up to lec. 3). Question for you... given that problem 1 includes the following, "however you chose to solve this problem, list the combinations of 6, 9 and 20 packs of McNuggets you need to buy in order to get each of the exact amounts." How does your code handle listing the combinations possible? Your function, isComboPossible(n,x,y,z) will return the first and only the first combination. Here is my work http://pastebin.com/9xGNaB7F (feedback is welcome)

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0Also, I do recognize that my code takes a lot of run time because it iterates through all possible values of a, b, and c.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0@malpaso I was interested in passing the values of the tuple as such def myFunction(x, y, z): stuff goes here myTuple = (1, 2, 3) myFunction(myTuple[0], myTuple[1], myTuple[2]) VS myFunction(*myTuple) @AlexanderR You're correct in that I haven't checked for all possible combinations, the way I've written the function now it exits on the first possible combination it finds. I think to handle all possible combinations I would fix my function to include a variable to track the number of possible combinations, returning 0 if none. Then add a list which can store the tuple combinations as they are found. let me try it out and see what i can find. I'm not sure when working on these problems if we're bound to the features in python that have been brought up in the lectures or since the course isn't strictly about python if we're free to use what we're comfortable with. Could always just build a string to return instead of a list but lists are easier to work with in this case i think.

maitre_kaio
 4 years ago
Best ResponseYou've already chosen the best response.1The question of the assignment is 'does it exist a combination that meets the requirements ?'. You are not expected to find all the combinations and it won't help you to solve the problem. It may be interesting to code, but I think it should be noted that it is out of the range of the problem.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0@ mairtre_kaio Listing the combinations is a part of problem 1 from the set, though it does not specify that it needed to be coded. I actually was only working on this part of the problem set without reading further.. hehe @kl365 Apologies if I am misguiding you with my question, I will work further into the problem sets before my commentary in the open study group!! Thanks for sharing your work and the response.
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.