Hi everybody, I just start learning python, and beacuse of that I don't really know all usefull packages and methods, that this language has. But I would like to know how would you write this program, or how would you change it (or may be some part of it) to looks it more elegant and to be more efficient.
here is the code:
import random
class FrequenceCounter:
count = 0
def update_count(self):
self.count += 1
def get_count(self):
return self.count
nums = xrange(10)
freq_of_num = {}
runs = int(raw_input("Enter number of runs "))
for x in xrange(10):
freq_of_num[x] = FrequenceCounter()
for x in xrange(runs):
freq = random.choice(nums)
freq_of_num[freq].update_count()
for x in xrange(10):
print "Number %d occurred %d" % (x, freq_of_num[x].get_count())
And that's how my output looks like:
Enter number of runs 88
Number 0 occurred 9
Number 1 occurred 12
Number 2 occurred 10
Number 3 occurred 2
Number 4 occurred 5
Number 5 occurred 13
Number 6 occurred 9
Number 7 occurred 10
Number 8 occurred 11
Number 9 occurred 7