Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

acm2140 Group Title

Hi all, I am stuck on ps 1 c. Printing it out it seems to get stuck in a neverending loop where newBalance > 0 but I don't seem to be changing the monthlyPayment like I hoped I was. Can someone give me some hints as to how to solve this bisection search problem? I feel like I understand it in theory but am unable to make it work.

  • 5 months ago
  • 5 months ago

  • This Question is Closed
  1. acm2140 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Here is my code

    • 5 months ago
    1 Attachment
  2. Ante Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    At first glance, you might wanna check that for loop. Seems like you're making it pretty certain that it will terminate after the first iteration.

    • 5 months ago
  3. seandisanti Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    if you add print 'upper' + str(upperPayment) + ' lower' + str(lowerPayment) as the first line of your loop, you'll see that you very quickly run into a situation where your upper and lower bounds are the same, at which point adding them together and dividing by 2 results in the same answer. Such begins your infinite loop. Also as Ante stated, you're breaking on every iteration of your for loop regardless of the comparison. You're very close to getting this, so I don't want to ruin it for you, but look at your code and see why your upper and lower bounds would run into each other. also step through your logic to make sure that you're doing what you think you're doing.

    • 5 months ago
  4. seandisanti Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Don't forget about your epsilon value either, and the algorithm on this one is simple enough that if necessary, you could do the problem manually on paper or in excel to see the steps necessary vs the steps being done. That's a worst case scenario of course, but you can do it

    • 5 months ago
  5. bennyboy489 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I believe it is in this line of code here: while abs(balance) >= epsilon and monthlyPayment <= x: inside the while loop, you have nothing changing the variable to make it stop, so when it ends through all the ifs/else, etc, it goes back to check if that statement is still true or false, it then goes through it again and again until you kill it

    • 5 months ago
  6. acm2140 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Ante, Sean, Benny: Thanks for your helpful tips!! And for not just telling me the answer... I realized a couple of mistakes I had made: 1. Using the print statement Sean suggested I realized that due to missing parentheses (!) the lower and upper bounds were actually set to the same value from the start!! 2. My newBalance wasn't updating at all (yay print debugging) 3. My while loop was set to check for balance, not newBalance and was also therefore doomed to fail. Now my code is working but it isn't returning the exact answers in the test code for the problem set. The examples in the problem set suggest that the remaining balance should be a small negative value (first example -0.01, second example -0.12). Mine is returning exact answers with no remainder, but seemingly correct answers. Can someone explain why my answer is slightly different from the examples in the problem set?

    • 5 months ago
    1 Attachment
  7. acm2140 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I might add that I was wondering if it has anything to do with the type of value printed (string vs float)? Could that be the case?

    • 5 months ago
  8. acm2140 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Nevermind, I just found the solution to the problem set and see that my code wasn't nearly as sophisticated when it came to rounding at different parts during the process. A bit disheartening to see that I was so far off from the solution code even though my answers were extremely close.

    • 5 months ago
  9. seandisanti Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    For any problem, especially programming ones, there are a LOT of solutions, and it's only possible to learn from attempts that we fail at. You made progress and hopefully learned something in analyzing the differences in your code, and your efforts to do it yourself. Stick with it

    • 5 months ago
  10. acm2140 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks for the encouraging words! On to the next class!

    • 5 months ago
    • Attachments:

See more questions >>>

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.