-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathtest_list_08.py
66 lines (50 loc) · 1.35 KB
/
test_list_08.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
from lpython import i32, f64, InOut
from copy import deepcopy
def sort(l: InOut[list[i32]]) -> list[i32]:
i: i32; j: i32
for i in range(len(l)):
for j in range(i + 1, len(l)):
if l[i] > l[j]:
l[i], l[j] = l[j], l[i]
return l
def sort_list():
x: list[i32] = []
size: i32 = 50
i: i32; j: i32
for i in range(size):
x.append(size - i)
x = deepcopy(sort(x))
for i in range(size - 1):
assert x[i] <= x[i + 1]
assert len(x) == size
def l1norm(v: list[i32]) -> f64:
result: f64 = 0.0
i: i32
for i in range(len(v)):
result = result + f64(v[i])
return result
def sort_by_l1_norm():
mat: list[list[i32]] = []
vec: list[i32] = []
i: i32; j: i32; rows: i32; cols: i32; k: i32;
norm1: f64; norm2: f64;
rows = 10
cols = 7
k = rows * cols
for i in range(rows):
for j in range(cols):
vec.append(k)
k = k - 1
mat.append(deepcopy(vec))
vec.clear()
for i in range(rows):
for j in range(i + 1, rows):
if l1norm(mat[i]) > l1norm(mat[j]):
mat[i], mat[j] = deepcopy(sort(mat[j])), deepcopy(sort(mat[i]))
k = 1
for i in range(rows):
for j in range(cols):
assert mat[i][j] == k
k = k + 1
sort_list()
sort_by_l1_norm()