-
Notifications
You must be signed in to change notification settings - Fork 2
/
alternating_array.py
55 lines (47 loc) · 2.09 KB
/
alternating_array.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
import functools
from test_framework import generic_test
from test_framework.test_failure import PropertyName, TestFailure
from test_framework.test_utils import enable_executor_hook
def rearrange(A):
for i in range(len(A) - 1):
if (
i % 2 == 0 and A[i] > A[i+1] or
i % 2 == 1 and A[i] < A[i+1]
):
A[i], A[i+1] = A[i+1], A[i]
@enable_executor_hook
def rearrange_wrapper(executor, A):
def check_answer(A):
for i in range(len(A)):
if i % 2:
if A[i] < A[i - 1]:
raise TestFailure().with_property(
PropertyName.RESULT, A).with_mismatch_info(
i, 'A[{}] <= A[{}]'.format(i - 1, i),
'{} > {}'.format(A[i - 1], A[i]))
if i + 1 < len(A):
if A[i] < A[i + 1]:
raise TestFailure().with_property(
PropertyName.RESULT, A).with_mismatch_info(
i, 'A[{}] >= A[{}]'.format(i, i + 1),
'{} < {}'.format(A[i], A[i + 1]))
else:
if i > 0:
if A[i - 1] < A[i]:
raise TestFailure().with_property(
PropertyName.RESULT, A).with_mismatch_info(
i, 'A[{}] >= A[{}]'.format(i - 1, i),
'{} < {}'.format(A[i - 1], A[i]))
if i + 1 < len(A):
if A[i + 1] < A[i]:
raise TestFailure().with_property(
PropertyName.RESULT, A).with_mismatch_info(
i, 'A[{}] <= A[{}]'.format(i, i + 1),
'{} > {}'.format(A[i], A[i + 1]))
executor.run(functools.partial(rearrange, A))
check_answer(A)
if __name__ == '__main__':
exit(
generic_test.generic_test_main("alternating_array.py",
'alternating_array.tsv',
rearrange_wrapper))