|
4 | 4 |
|
5 | 5 | from .analysis import Analyzer
|
6 | 6 | from .formatters import AnalysisFormatter
|
7 |
| -from .enums import NotationFormat |
| 7 | +from .enums import NotationFormat, CommonBigO |
8 | 8 |
|
9 | 9 | '''
|
10 | 10 | Base class of asymptotic notation
|
@@ -56,6 +56,118 @@ class BigO(AsymptoticNotation):
|
56 | 56 |
|
57 | 57 | def __init__(self) -> None:
|
58 | 58 | 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 |
59 | 171 |
|
60 | 172 | @AsymptoticNotation.report
|
61 | 173 | def complexity(self, func) -> AnalysisFormatter:
|
|
0 commit comments