• Lucid Dreaming - Dream Views




    Results 1 to 6 of 6

    Thread: Halp! (Python)

    Hybrid View

    1. #1
      Ehh..Well..Uhm...HUGS!
      Join Date
      Dec 2008
      Gender
      Location
      Netherlands
      Posts
      842
      Likes
      0
      Quote Originally Posted by Ynot View Post
      edited OP
      Thanks. So, does anybody have any help?

      I'm still stuck with it >.<
      http://i96.photobucket.com/albums/l199/ablativus/spidermansig2.png

    2. #2
      adversary RedfishBluefish's Avatar
      Join Date
      Apr 2007
      Location
      Now
      Posts
      495
      Likes
      4
      One thing: the range() function is (in mathy words) inclusive-exclusive [a,b)... in Pythony words, range(0, x) returns [0, 1, 2, ..., x-2, x-1]. So you'll want to change eg. "for a in range(0,200)" -> "for a in range(0, 200+1)" to get those cases where any one coin makes up the whole thing.

      This actually might explain the problem with your second code... if a is the number if 1p coins that need to be added to get to £2, and you use range(0, a), then it will only go up to a-1 and never actually reach the value a, and so never obtain £2. Similarly of course for the outer loops. So you need to add one to each of those ranges as well.

      That probably answers your question.

      Just because I have to put my own ideas in , you can actually eliminate the final inner loop in the second version completely. If r >= 0 then clearly you can add exactly r 1p coins to make £2, so that's one combination done .

      Another trick is using xrange() instead of range() which is exactly the same but returns a generator instead of a list, probably speeding things up a bit since you don't have to allocate lots and lots of lists.

    3. #3
      Ehh..Well..Uhm...HUGS!
      Join Date
      Dec 2008
      Gender
      Location
      Netherlands
      Posts
      842
      Likes
      0
      Quote Originally Posted by RedfishBluefish View Post
      One thing: the range() function is (in mathy words) inclusive-exclusive [a,b)... in Pythony words, range(0, x) returns [0, 1, 2, ..., x-2, x-1]. So you'll want to change eg. "for a in range(0,200)" -> "for a in range(0, 200+1)" to get those cases where any one coin makes up the whole thing.

      This actually might explain the problem with your second code... if a is the number if 1p coins that need to be added to get to £2, and you use range(0, a), then it will only go up to a-1 and never actually reach the value a, and so never obtain £2. Similarly of course for the outer loops. So you need to add one to each of those ranges as well.

      That probably answers your question.

      Just because I have to put my own ideas in , you can actually eliminate the final inner loop in the second version completely. If r >= 0 then clearly you can add exactly r 1p coins to make £2, so that's one combination done .

      Another trick is using xrange() instead of range() which is exactly the same but returns a generator instead of a list, probably speeding things up a bit since you don't have to allocate lots and lots of lists.
      Thanks
      I figured the r thingey today, too.
      Thanks for your advice!
      http://i96.photobucket.com/albums/l199/ablativus/spidermansig2.png

    Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •