-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo_template_sort.py
107 lines (82 loc) · 2.67 KB
/
algo_template_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# -*- coding: utf-8 -*-
"""
@Author: Federico Bau
@Date: 31/07/2022
Rules:
Complexity (worst):
Space Complexity:
"""
import time
import random
class AlgoSort:
def __init__(self):
self.count = 0
self.start = None
self.end = None
self.start_native = None # Start time of Python native [].sort() method
self.end_native = None # End time of Python native [].sort() method
self.inputs = None
self.expected = None
self.order = None
def __call__(self, inputs: list, expected: list, order='ASC'):
self.inputs = inputs
self.expected = expected
self.order = order
self.run()
def run(self):
self.start = time.perf_counter()
result = self.algorithm()
self.end = time.perf_counter()
self.sort_native() # Sort list with Python native [].sort() method
self.check_result(result)
self.reset_algo()
def algorithm(self) -> list:
inputs = self.inputs
result = []
return result
# ////////////////////////////
# ~~~~~~ Recursive ~~~~~~~~~~~
# ////////////////////////////
# -----------------------
# Helpers
# -----------------------
def sort_native(self) -> None:
"""
Sorts the inputs list using native method [].sort() and records its timing
Returns:
None
"""
inputs_copy = self.inputs.copy() # make a copy so that the original input is not modified
self.start_native = time.perf_counter()
inputs_copy.sort()
self.end_native = time.perf_counter()
def check_result(self, result: list) -> None:
total_values = len(self.inputs)
expected = self.expected
total_time = self.end - self.start
total_time_native = self.end_native - self.start_native
total_time_diff = abs(total_time_native - total_time)
name = id(self)
assert expected == result
print(f"""Runner {name} SUCCESS
* Total time: {total_time:.6f}
* Native {total_time_native:.6f}
* Diff {total_time_diff:.6f}
* Volume: {total_values} items x {self.count} times
""")
def reset_algo(self):
self.count = 0
self.start = None
self.end = None
self.start_native = None
self.end_native = None
def main():
qs = AlgoSort()
cases = 10
test_1 = [random.randint(0, 1000) for _ in range(cases)]
print(f"<<<<<<<<< TEST ONE - CASES {cases}>>>>>>>>>")
test_1_expected = sorted(test_1)
qs(test_1, test_1_expected)
print("\n----------------------------------------------------- \n")
if __name__ == '__main__':
main()