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
from random import randrange
from sys import exit
def existing_combination(number, reds, combinations):
for red in reds:
if (red + number) in combinations:
def existing_combination(test_number, other_side, combinations):
for number in other_side:
if (test_number + number) in combinations:
return True
return False
def update_combinations(number, reds, combinations):
for red in reds:
combinations.append(red + number)
def update_combinations(test_number, other_side, combinations):
for number in other_side:
combinations.append(test_number + number)
return combinations
debug = True
red_size = 6
blue_size = red_size
def find_number(already_found, combine_with):
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 = []
blues = []
combinations = []
for i in range(red_size):
new_number = randrange(99)
while new_number in reds:
new_number = randrange(99)
for i in range(max(red_size, blue_size)):
if i < red_size:
new_number = find_number(reds, blues)
reds.append(new_number)
if debug:
print(f'red #{i+1} found')
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)
print(f'red #{i+1} found: {new_number}')
if i < blue_size:
new_number = find_number(blues, reds)
blues.append(new_number)
combinations = update_combinations(new_number, reds, combinations)
if debug:
print(f'blue #{i+1} found')
print(f'blue #{i+1} found: {new_number}')
reds.sort()
blues.sort()
combinations.sort()
print(f'reds: {reds}')
print(f'blues: {blues}')
if debug:
print(blues)
print(combinations)
print(f'combinations: {combinations}')
# vim: set sw=2 ts=2 et: