-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashcode.py
More file actions
71 lines (61 loc) · 1.54 KB
/
hashcode.py
File metadata and controls
71 lines (61 loc) · 1.54 KB
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
def open_file(path):
with open(f'./input/{path}.in', 'r') as file:
content = []
for item in file:
content.append(item)
return content
def get_max_slices(list_value):
aux = ''
for n in list_value:
if n == ' ':
break
aux += n
target = int(aux)
return target
def get_slices(list_slices):
slices = []
temp1 = list_slices.split('\n')
temp2 = temp1[0].split(' ')
for number in temp2:
slices.append(int(number))
return slices
def solve_problem(total_slices, nums_slices):
best_solution = []
score = j = 0
i = len(nums_slices) - 1
test1 = test2 = True
while (i >= 0) and test1:
temp_best_solution = []
j = i
add = 0
while (j >= 0) and test2:
temp = nums_slices[j]
if (add + temp) < total_slices:
add += temp
temp_best_solution.append(j)
elif (add + temp) == total_slices:
add += temp
temp_best_solution.append(j)
test1 = False
test2 = False
j -= 1
if score < add:
score = add
best_solution = temp_best_solution
if len(best_solution) == len(nums_slices):
test1 = False
i -= 1
return sorted(best_solution)
def save_file(list_output, path):
op = str(len(list_output)) + '\n'
for item in list_output:
op += str(item) + ' '
with open(f'./output/{path}.out', 'w') as fileOutput:
fileOutput.write(op)
fileNames = ("a_example", "b_small", "c_medium", "d_quite_big", "e_also_big")
for name in fileNames:
result = open_file(name)
max_slices = get_max_slices(result[0])
number_slices = get_slices(result[1])
output = solve_problem(max_slices, number_slices)
save_file(output, name)