You are viewing a single comment's thread from:
RE: A tiny OOP challenge (Python, Java, C++, any language with class) /
In Python (my personal favorite...also check out sage and https://www.cocalc.com), here's how I would do it:
class Averager:
l = []
def add(self,a):
self.l.append(a)
def getAverage(self):
return sum(self.l)/float(len(self.l))
def getSum(self):
return sum(self.l)
def getNum(self):
return len(self.l)
def reset(self):
self.l = []
Same sort of list comprehensions as C# :) Class looks similar in implementation as well.