-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
49 lines (46 loc) · 1.84 KB
/
test.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
import sys
import itertools
import math
import random
import subprocess
if __name__ == "__main__":
n = int(sys.argv[1])
# numbers = random.sample(range(-2147483648, 2147483647), n)
numbers = list(range(1, n + 1))
max_case = ""
max_score = 0
min_score = 1000000
scores_sum = 0
if n <= 5:
permutations = list(itertools.permutations(numbers))
nb_cases = len(permutations)
for nums in permutations:
args = " ".join([str(nb) for nb in nums])
print(args + ' : ', end="")
push_swap = subprocess.run('./push_swap ' + args + " | wc -l", stdout=subprocess.PIPE, shell=True)
nb_operations = int(push_swap.stdout.decode('utf-8').strip('\n'))
if nb_operations > max_score:
max_case = args
max_score = nb_operations
elif nb_operations < min_score:
min_score = nb_operations
scores_sum += nb_operations
print(('\033[1m\033[31mKO' if (n == 3 and nb_operations > 2) or (n == 5 and nb_operations > 12) else 'OK') + ' (' + str(nb_operations) + ')\033[0m')
else:
nb_cases = 1000
for i in range(nb_cases):
random.shuffle(numbers)
args = " ".join([str(nb) for nb in numbers])
push_swap = subprocess.run('./push_swap ' + args + " | wc -l", stdout=subprocess.PIPE, shell=True)
nb_operations = int(push_swap.stdout.decode('utf-8').strip('\n'))
if nb_operations > max_score:
max_case = args
max_score = nb_operations
elif nb_operations < min_score:
min_score = nb_operations
scores_sum += nb_operations
if n == 100 and nb_operations > 700 or n == 500 and nb_operations > 5500:
print(args + ' -> ', end='')
print(('\033[1m\033[31mKO' if (n == 100 and nb_operations > 700) or (n == 500 and nb_operations > 5500) else 'OK') + ' (' + str(nb_operations) + ')\033[0m')
print(f'\n- - -\nMax : {max_case}', end='')
print(f'\n- - -\nMax : {max_score}\nAvg : {math.ceil(scores_sum / nb_cases)}\nMin : {min_score}')