help - python question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • funkky
    New Member
    • May 2007
    • 1

    help - python question

    Please 'am a new beginnger in python. I have been trying to solve these problems, but 'am always getting stuck. Can you please help me out:

    1.
    Code:
     numBuckets = 8
    buckets = [0] * numBuckets
    bucketWidth = 1.0 / numBuckets
    for i in range(numBuckets):
          low = i * bucketWidth
          high = low + bucketWidth
          buckets[i] = inBucket(list, low, high)
    print buckets

    the output suppose to give me values, after running the program, I kept getting this:

    Code:
    >>>[none, none, none, none, none, none, none, none]
    2.
    Code:
     numBuckets = 8
    buckets = [0] * numBuckets
    for i in list:
          index = int(i * numBuckets)
          buckets[index] = buckets[index] + 1

    I am using python 2.5.1 version
    Last edited by funkky; May 23 '07, 05:17 PM. Reason: adding information for the version of python 'am using
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by funkky
    Please 'am a new beginnger in python. I have been trying to solve these problems, but 'am always getting stuck. Can you please help me out:

    1.
    Code:
     numBuckets = 8
    buckets = [0] * numBuckets
    bucketWidth = 1.0 / numBuckets
    for i in range(numBuckets):
          low = i * bucketWidth
          high = low + bucketWidth
          buckets[i] = inBucket(list, low, high)
    print buckets

    the output suppose to give me values, after running the program, I kept getting this:

    Code:
    >>>[none, none, none, none, none, none, none, none]
    2.
    Code:
     numBuckets = 8
    buckets = [0] * numBuckets
    for i in list:
          index = int(i * numBuckets)
          buckets[index] = buckets[index] + 1

    I am using python 2.5.1 version
    1: You'll need to include the inBucket function.
    2: I'm not sure what you are trying to do, but this may give you some clues to both problems:
    Code:
    numBuckets = 8
    buckets = [0] * numBuckets
    
    def ChangeBucketsInPlace():
        for i in range(numBuckets):
            index = int(i * numBuckets)
            buckets[i] = index
    
    
    print buckets
    ChangeBucketsInPlace()
    print buckets

    Comment

    • gogomon
      New Member
      • May 2007
      • 6

      #3
      I'm guessing your following the code from the site at How to Think Lika a Computer Scientist: Chapter 9. Post of the code, so we can see the values of the list and the definition of the inBucket function, there might be some typos or something like that
      Last edited by bartonc; May 25 '07, 03:09 AM. Reason: Credit to linked site

      Comment

      Working...