auto size max number and factorize code

This commit is contained in:
Cedric Girard 2019-02-10 16:57:07 +01:00
parent adfa2031f7
commit a28c312c0d
Signed by: X-dark
GPG key ID: 37995E758B76710B

View file

@ -1,57 +1,73 @@
#! env python3 #! env python3
from random import randrange from random import randrange
from sys import exit
def existing_combination(number, reds, combinations): def existing_combination(test_number, other_side, combinations):
for red in reds: for number in other_side:
if (red + number) in combinations: if (test_number + number) in combinations:
return True return True
return False return False
def update_combinations(number, reds, combinations): def update_combinations(test_number, other_side, combinations):
for red in reds: for number in other_side:
combinations.append(red + number) combinations.append(test_number + number)
return combinations return combinations
debug = True def find_number(already_found, combine_with):
red_size = 6
blue_size = red_size global biggest_number, combinations, max_retry, debug, increment_size
new_number = randrange(biggest_number) + 1
retry = 0
while new_number in already_found or new_number in combine_with or existing_combination(new_number, combine_with, combinations):
if retry > max_retry:
biggest_number = biggest_number + increment_size
retry = 0
if debug:
print(f'Increasing max number to {biggest_number}')
retry = retry + 1
new_number = randrange(biggest_number) + 1
combinations = update_combinations(new_number, combine_with, combinations)
return new_number
debug = False
red_size = 14
blue_size = 14
biggest_number = 19
increment_size = 10
max_retry = 1000000
reds = [] reds = []
blues = [] blues = []
combinations = [] combinations = []
for i in range(red_size): for i in range(max(red_size, blue_size)):
new_number = randrange(99)
while new_number in reds:
new_number = randrange(99)
if i < red_size:
new_number = find_number(reds, blues)
reds.append(new_number) reds.append(new_number)
if debug: if debug:
print(f'red #{i+1} found') print(f'red #{i+1} found: {new_number}')
if i < blue_size:
new_number = find_number(blues, reds)
blues.append(new_number)
if debug:
print(f'blue #{i+1} found: {new_number}')
reds.sort() reds.sort()
if debug:
print(reds)
for i in range(blue_size):
new_number = randrange(99)
while new_number in reds or new_number in blues or existing_combination(new_number, reds, combinations):
new_number = randrange(99)
blues.append(new_number)
combinations = update_combinations(new_number, reds, combinations)
if debug:
print(f'blue #{i+1} found')
blues.sort() blues.sort()
combinations.sort() combinations.sort()
print(f'reds: {reds}')
print(f'blues: {blues}')
if debug: if debug:
print(blues) print(f'combinations: {combinations}')
print(combinations)
# vim: set sw=2 ts=2 et: # vim: set sw=2 ts=2 et: