-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ1.py
More file actions
89 lines (72 loc) · 2.78 KB
/
Q1.py
File metadata and controls
89 lines (72 loc) · 2.78 KB
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
import numpy as np
import time
def sort_indices_without_numpy(lst):
# Check if the input list is empty or not numerical
if not lst:
raise ValueError("Input list is empty")
if not all(isinstance(x, (int, float)) for x in lst):
raise ValueError("Input list contains non-numerical elements")
# Enumerate the elements along with their indices
indexed_list = list(enumerate(lst))
# Sort the indexed list based on the values
sorted_indexed_list = sorted(indexed_list, key=lambda x: x[1])
# Extract the indices after sorting
sorted_indices = [index for index, _ in sorted_indexed_list]
return sorted_indices
def sort_indices_with_numpy(lst):
# Check if the input list is empty or not numerical
if not lst:
raise ValueError("Input list is empty")
if not all(isinstance(x, (int, float)) for x in lst):
raise ValueError("Input list contains non-numerical elements")
# Convert the list to a NumPy array
arr = np.array(lst)
# Get the indices of the sorted elements
sorted_indices = np.argsort(arr)
return sorted_indices
# Test the functions
list1 = [23, 104, 5, 190, 8, 7, -3]
list2 = []
list3 = np.random.randint(0, 1000000, 1000000).tolist()
try:
start_time = time.time()
indices1 = sort_indices_without_numpy(list1)
print("Sorted indices without NumPy for list 1:", indices1)
print("Time taken without NumPy for list1:", time.time() - start_time, "seconds")
except ValueError as e:
print(e)
try:
start_time = time.time()
indices1 = sort_indices_with_numpy(list1)
print("Sorted indices with NumPy for list 1:", indices1)
print("Time taken with NumPy for list1:", time.time() - start_time, "seconds")
except ValueError as e:
print(e)
try:
start_time = time.time()
indices2 = sort_indices_without_numpy(list2)
print("Sorted indices without NumPy for list 2:", indices2)
print("Time taken without NumPy for list2:", time.time() - start_time, "seconds")
except ValueError as e:
print(e)
try:
start_time = time.time()
indices2 = sort_indices_with_numpy(list2)
print("Sorted indices with NumPy for list 2:", indices2)
print("Time taken with NumPy for list2:", time.time() - start_time, "seconds")
except ValueError as e:
print(e)
try:
start_time = time.time()
indices3 = sort_indices_without_numpy(list3)
#print("Sorted indices without NumPy for list 3:", indices3)
print("Time taken without NumPy for list3:", time.time() - start_time, "seconds")
except ValueError as e:
print(e)
try:
start_time = time.time()
indices3 = sort_indices_with_numpy(list3)
#print("Sorted indices with NumPy for list 3:", indices3)
print("Time taken with NumPy for list3:", time.time() - start_time, "seconds")
except ValueError as e:
print(e)