-
Notifications
You must be signed in to change notification settings - Fork 5
/
check.py
78 lines (65 loc) · 2.35 KB
/
check.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2023 Ruize Tang <[email protected]>, Runze Wu
# <[email protected]>, Anpu Lu <[email protected]>.
#
# This file is a part of Disalg-ICS-NJU/algocentric.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import sys
from get_input import get_input # pylint: disable=wrong-import-position # noqa
from critical_op import mult_cost # pylint: disable=wrong-import-position, no-name-in-module # noqa
from matrix_mult import matrix_mult_dp
def check(oracle, to_check) -> bool:
'''检查oracle和to_check是否完全一致.
Args:
oracle: 给定的正确结果
to_check: 待验证的输入
Returns:
oracle是否与to_check完全一致的检查结果
'''
return oracle == to_check
def matrix_mult_check(dime_list: list, to_check: int) -> bool:
'''检查to_check是否为正确的最小开销
Args:
dime_list (list): 给定的输入list
to_check (list): 待检查的最小开销
Returns:
to_check是否为正确的最小开销
'''
return check(matrix_mult_bf(dime_list), to_check)
def matrix_mult_bf(dime_list: list) -> int:
k = len(dime_list) - 1
if k <= 1:
return 0
cost = float('inf')
for i in range(1, k):
cost1 = dime_list[i-1] * dime_list[i] * dime_list[i+1]
new_list = dime_list.copy()
new_list.pop(i)
cost2 = matrix_mult_bf(new_list)
cost = min(cost, cost1 + cost2)
return cost
if __name__ == '__main__':
testcase = get_input()
result = matrix_mult_dp(testcase[0], testcase[1])
if matrix_mult_check(testcase[1], result[1]):
print('PASS')
print('Critical op counts:', mult_cost.get_op_count())
else:
print('FAIL')
print('Input :', testcase)
print('Result:', result)
sys.exit(1)