finish extra/distract-the-trainers #11
@ -1,12 +1,15 @@
|
||||
def solution(banana_list):
|
||||
fewest_possible_number = len(banana_list)
|
||||
memo = {}
|
||||
for pairs in generate_trainer_pairs(list(range(len(banana_list)))):
|
||||
possible_number = len(banana_list)
|
||||
for i, j in pairs:
|
||||
if check_loop(banana_list[i], banana_list[j]):
|
||||
if check_loop(banana_list[i], banana_list[j], memo):
|
||||
possible_number -= 2
|
||||
if possible_number < fewest_possible_number:
|
||||
fewest_possible_number = possible_number
|
||||
if fewest_possible_number < 2: # early termination
|
||||
break
|
||||
return fewest_possible_number
|
||||
|
||||
import copy
|
||||
@ -15,28 +18,25 @@ def generate_trainer_pairs(trainer_id_list):
|
||||
yield []
|
||||
else:
|
||||
first_id = trainer_id_list[0]
|
||||
temp_list = copy.deepcopy(trainer_id_list)
|
||||
temp_list.remove(first_id)
|
||||
for second_id in temp_list:
|
||||
tt_list = copy.deepcopy(temp_list)
|
||||
tt_list.remove(second_id)
|
||||
for pairs in generate_trainer_pairs(tt_list):
|
||||
for second_id in trainer_id_list[1:]:
|
||||
reduced_list = copy.deepcopy(trainer_id_list)
|
||||
seduced_list.remove(first_id)
|
||||
reduced_list.remove(second_id)
|
||||
for pairs in generate_trainer_pairs(reduced_list):
|
||||
yield [(first_id, second_id)] + pairs
|
||||
|
||||
def check_loop(x, y):
|
||||
if (x + y) % 2 == 1: # sum is odd
|
||||
return True
|
||||
a, b = x, y
|
||||
memo = []
|
||||
while a != b:
|
||||
if a > b:
|
||||
a, b = a - b, 2*b
|
||||
else:
|
||||
a, b = 2*a, b - a
|
||||
def check_loop(a, b, memo):
|
||||
if (a, b) in memo:
|
||||
return memo[(a, b)]
|
||||
while a != b:
|
||||
if (a + b) % 2 == 1: # sum is odd
|
||||
memo[(a, b)] = True
|
||||
return True
|
||||
if a > b:
|
||||
a, b = (a - b)/2, b
|
||||
else:
|
||||
memo += [(a, b)]
|
||||
a, b = a, (b - a)/2
|
||||
memo[(a, b)] = False
|
||||
return False
|
||||
|
||||
tests = [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user