Skip to content

Commit b66e4cc

Browse files
Add common Big O complexities
1 parent 7ce19aa commit b66e4cc

File tree

2 files changed

+140
-2
lines changed

2 files changed

+140
-2
lines changed

pycomplexity/enums.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,30 @@ class NotationFormat(Enum):
1010
BIG_THETA : str = 'BIG Θ (THETA)'
1111

1212
def __str__(self) -> str:
13-
return str(self.value)
13+
return self.value
14+
15+
16+
'''
17+
The most common Big O complexity notations enum
18+
'''
19+
class CommonBigO(Enum):
20+
21+
NON_COMMON : str = 'Non common'
22+
CONSTANT : str = '1'
23+
LINEAR : str = 'N'
24+
LOGARITHMIC : str = 'log(N)'
25+
LINEARITHMIC : str = 'N*log(N)'
26+
QUADRATIC : str = 'N^2'
27+
CUBIC : str = 'N^3'
28+
EXPONENTIAL : str = '2^N'
29+
FACTORIAL : str = 'N!'
30+
31+
def __str__(self) -> str:
32+
if self is not self.NON_COMMON:
33+
return f'O({self.value})'
34+
return self.value
35+
36+
def __repr__(self) -> str:
37+
_lower = self.name.replace('_', ' ').lower()
38+
_formatted = _lower[0].upper() + _lower[1:]
39+
return f'{_formatted} time complexity'

pycomplexity/notations.py

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .analysis import Analyzer
66
from .formatters import AnalysisFormatter
7-
from .enums import NotationFormat
7+
from .enums import NotationFormat, CommonBigO
88

99
'''
1010
Base class of asymptotic notation
@@ -56,6 +56,118 @@ class BigO(AsymptoticNotation):
5656

5757
def __init__(self) -> None:
5858
super().__init__()
59+
60+
def constant(self, func) -> CommonBigO:
61+
is_constant = False
62+
63+
'''
64+
TODO: function that checks if algorithm has constant complexity
65+
'''
66+
67+
if is_constant:
68+
return CommonBigO.CONSTANT
69+
return CommonBigO.NON_COMMON
70+
71+
def linear(self, func) -> CommonBigO:
72+
is_linear = False
73+
74+
'''
75+
TODO: function that checks if algorithm has linear complexity
76+
'''
77+
78+
if is_linear:
79+
return CommonBigO.LINEAR
80+
return CommonBigO.NON_COMMON
81+
82+
def logarithmic(self, func) -> CommonBigO:
83+
is_logarithmic = False
84+
85+
'''
86+
TODO: function that checks if algorithm has logarithmic complexity
87+
'''
88+
89+
if is_logarithmic:
90+
return CommonBigO.LOGARITHMIC
91+
return CommonBigO.NON_COMMON
92+
93+
def linearithmic(self, func) -> CommonBigO:
94+
is_linearithmic = False
95+
96+
'''
97+
TODO: function that checks if algorithm has linearithmic complexity
98+
'''
99+
100+
if is_linearithmic:
101+
return CommonBigO.LINEARITHMIC
102+
return CommonBigO.NON_COMMON
103+
104+
def quadratic(self, func) -> CommonBigO:
105+
is_quadratic = False
106+
107+
'''
108+
TODO: function that checks if algorithm has quadratic complexity
109+
'''
110+
111+
if is_quadratic:
112+
return CommonBigO.QUADRATIC
113+
return CommonBigO.NON_COMMON
114+
115+
def cubic(self, func) -> CommonBigO:
116+
is_cubic = False
117+
118+
'''
119+
TODO: function that checks if algorithm has cubic complexity
120+
'''
121+
122+
if is_cubic:
123+
return CommonBigO.CUBIC
124+
return CommonBigO.NON_COMMON
125+
126+
def exponential(self, func) -> CommonBigO:
127+
is_exponential = False
128+
129+
'''
130+
TODO: function that checks if algorithm has exponential complexity
131+
'''
132+
133+
if is_exponential:
134+
return CommonBigO.EXPONENTIAL
135+
return CommonBigO.NON_COMMON
136+
137+
def factorial(self, func) -> CommonBigO:
138+
is_factorial = False
139+
140+
'''
141+
TODO: function that checks if algorithm has factorial complexity
142+
'''
143+
144+
if is_factorial:
145+
return CommonBigO.FACTORIAL
146+
return CommonBigO.NON_COMMON
147+
148+
def _calculate_complexity(self, func) -> CommonBigO:
149+
checking_funcs = [
150+
self.constant,
151+
self.linear,
152+
self.logarithmic,
153+
self.linearithmic,
154+
self.quadratic,
155+
self.cubic,
156+
self.exponential,
157+
self.factorial,
158+
]
159+
160+
_complexities = []
161+
for _check in checking_funcs:
162+
if (_complexity := _check(func)) is not CommonBigO.NON_COMMON:
163+
_complexities.append(_complexity)
164+
165+
if _complexities:
166+
# returns the minimum from the list of possible complexities
167+
return _complexities[0]
168+
169+
# if all checking funcs returns CommonBigO.NON_COMMON
170+
return CommonBigO.NON_COMMON
59171

60172
@AsymptoticNotation.report
61173
def complexity(self, func) -> AnalysisFormatter:

0 commit comments

Comments
 (0)