-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathboatQuestion_while.py
More file actions
60 lines (45 loc) · 1.49 KB
/
boatQuestion_while.py
File metadata and controls
60 lines (45 loc) · 1.49 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
import itertools as it
import random
import time
# some population cases
population = [30, 100, 200, 150, 80, 40, 100, 100, 100, 100, 90]
population = [10, 10, 10, 10, 10, 180, 180, 180, 180, 180]
population = [100, 200, 150, 80]
population = [random.randint(1,200) for x in range(100)]
boatLimit = 200
def rescue(population, boatLimit, numBoats, keep_going=True):
'''
Take population, limit and return smallest number of boats required
input:
population: array on numbers
limit: number
return:
number: smallest number of boats
'''
population.sort(reverse=True)
c = it.combinations(population, 2)
viablePairs = []
for i in c:
# find valid pairs
if i[0]+i[1] <= boatLimit:
viablePairs.append(i)
# print('reductions made, the size left:', len(viablePairs))
if len(viablePairs)==0:
#print('no viable pairs')
numBoats = numBoats + len(population)
print('the number of boats:',numBoats)
keep_going = False
else:
# remove pair
population.remove(viablePairs[0][0])
population.remove(viablePairs[0][1])
numBoats = numBoats + 1
return population, boatLimit, numBoats, keep_going
t1 = time.time()
# rescue(population, boatLimit, 0)
numBoats = 0
keep_going = True
while keep_going:
population, boatLimit, numBoats, keep_going = rescue(population, boatLimit, numBoats)
t2 = time.time()
print('time taken:', round(t2-t1, 3), 'seconds')