auto size max number and factorize code

Cedric Girard 2019-02-10 16:57:07 +01:00
parent adfa2031f7
commit a28c312c0d
Signed by: X-dark
GPG Key ID: 37995E758B76710B
1 changed files with 49 additions and 33 deletions

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)
for i in range(max(red_size, blue_size)):
while new_number in reds:
new_number = randrange(99)
if i < red_size:
new_number = find_number(reds, blues)
if debug:
print(f'red #{i+1} found: {new_number}')
if debug:
print(f'red #{i+1} found')
if i < blue_size:
new_number = find_number(blues, reds)
if debug:
print(f'blue #{i+1} found: {new_number}')
if debug:
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)
combinations = update_combinations(new_number, reds, combinations)
if debug:
print(f'blue #{i+1} found')
print(f'reds: {reds}')
print(f'blues: {blues}')
if debug:
print(f'combinations: {combinations}')
# vim: set sw=2 ts=2 et: