Skip to content

Commit 41ccba6

Browse files
author
Keivan Rezaei
committed
add dp calc
1 parent 58a3023 commit 41ccba6

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

dp_calc.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import numpy as np
2+
import json
3+
4+
def find_array(K = 1210):
5+
A = np.zeros((K, K,), dtype=int)
6+
7+
for i in range(K):
8+
for j in range(K):
9+
if i == 0 and j == 0:
10+
A[i, j] = 0
11+
elif i <= 1 and j <= 2:
12+
A[i, j] = 1
13+
elif i <= 2 and j <= 1:
14+
A[i, j] = 1
15+
elif j <= 1:
16+
A[i, j] = (i + 1) // 2
17+
elif i <= 1:
18+
A[i, j] = (j + 1) // 2
19+
else:
20+
A[i, j] = 1 + min(A[i - 2, j - 1], A[i - 1, j - 2])
21+
return A
22+
23+
def get_value(A: np.ndarray, g1:int, g2:int):
24+
return A[g1, g2]
25+
26+
def save_json(B:np.ndarray):
27+
with open('data/array_v2.json', 'w') as f:
28+
f.write(json.dumps(B.tolist()))
29+
return
30+
31+
def load_json():
32+
with open('data/array_v2.json', 'r') as f:
33+
return np.array(json.loads(f.read()), dtype=int)
34+
35+
def debug(B, g1, g2):
36+
print(f'g1, g2: {g1, g2} --> {B[g1][g2]}')
37+
38+
if __name__ == '__main__':
39+
K = 1210
40+
A = find_array(K)
41+
B = np.zeros((K - 1, K - 1, ), dtype=int)
42+
43+
for i in range(K-1):
44+
for j in range(K - 1):
45+
B[i, j] = get_value(A, i, j)
46+
47+
save_json(B)
48+
49+
B = load_json()
50+
debug(B, 0, 0)
51+
debug(B, 0, 1)
52+
debug(B, 10, 10)
53+
debug(B, 2, 2)
54+
debug(B, 3, 3)
55+
debug(B, 0, 10)
56+
debug(B, 10, 10)
57+
debug(B, 6, 10)
58+
debug(B, 7, 10)
59+
60+
61+

0 commit comments

Comments
 (0)