By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,994 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,994 IT Pros & Developers. It's quick & easy.

help - python question

P: 1
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.
Expand|Select|Wrap|Line Numbers
  1.  numBuckets = 8
  2. buckets = [0] * numBuckets
  3. bucketWidth = 1.0 / numBuckets
  4. for i in range(numBuckets):
  5.       low = i * bucketWidth
  6.       high = low + bucketWidth
  7.       buckets[i] = inBucket(list, low, high)
  8. print buckets

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

Expand|Select|Wrap|Line Numbers
  1. >>>[none, none, none, none, none, none, none, none]
2.
Expand|Select|Wrap|Line Numbers
  1.  numBuckets = 8
  2. buckets = [0] * numBuckets
  3. for i in list:
  4.       index = int(i * numBuckets)
  5.       buckets[index] = buckets[index] + 1

I am using python 2.5.1 version
May 23 '07 #1
Share this Question
Share on Google+
2 Replies


bartonc
Expert 5K+
P: 6,596
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.
Expand|Select|Wrap|Line Numbers
  1.  numBuckets = 8
  2. buckets = [0] * numBuckets
  3. bucketWidth = 1.0 / numBuckets
  4. for i in range(numBuckets):
  5.       low = i * bucketWidth
  6.       high = low + bucketWidth
  7.       buckets[i] = inBucket(list, low, high)
  8. print buckets

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

Expand|Select|Wrap|Line Numbers
  1. >>>[none, none, none, none, none, none, none, none]
2.
Expand|Select|Wrap|Line Numbers
  1.  numBuckets = 8
  2. buckets = [0] * numBuckets
  3. for i in list:
  4.       index = int(i * numBuckets)
  5.       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:
Expand|Select|Wrap|Line Numbers
  1. numBuckets = 8
  2. buckets = [0] * numBuckets
  3.  
  4. def ChangeBucketsInPlace():
  5.     for i in range(numBuckets):
  6.         index = int(i * numBuckets)
  7.         buckets[i] = index
  8.  
  9.  
  10. print buckets
  11. ChangeBucketsInPlace()
  12. print buckets
  13.  
May 23 '07 #2

P: 6
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
May 23 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.