1
0

Corrected complexity for build_stats method.

This commit is contained in:
Miguel Salgado 2022-08-12 09:50:21 -07:00
parent cfc9c1939d
commit 0a92dcc89d

8
app.py
View File

@ -14,12 +14,17 @@ class Stats:
a linear amount of time, in the data length. a linear amount of time, in the data length.
""" """
_data = {} _data = {}
# loop is linear in the data.
for v in data: for v in data:
# Dictionary access should be constant time.
_data[v] = _data.get(v, 0) + 1 _data[v] = _data.get(v, 0) + 1
_list, prev = [], 0 _list, prev = [], 0
# Adding a constant 1001 steps.
for i in range(0, 1002): for i in range(0, 1002):
# Constant time step
new = _data.get(i, 0) + prev new = _data.get(i, 0) + prev
# constant time step
_list.append(new) _list.append(new)
prev = new prev = new
self._list = _list self._list = _list
@ -72,7 +77,8 @@ class DataCapture:
which I think it's a good assumption to start which I think it's a good assumption to start
with. with.
""" """
return Stats(sorted(self._values)) # The sorted where turns this in to O(n*log(n))
return Stats(self._values)
if __name__ == "__main__": if __name__ == "__main__":