241 lines
9.6 KiB
Python
241 lines
9.6 KiB
Python
import random as rnd
|
|
|
|
""" def strCalc(str_to_calc):
|
|
str_to_calc = str_to_calc.replace('+', ' + ')
|
|
str_to_calc = str_to_calc.replace('-', ' - ')
|
|
str_to_calc = str_to_calc.replace('x', ' x ')
|
|
str_to_calc = str_to_calc.replace(':', ' : ')
|
|
str_to_calc = str_to_calc.split()
|
|
num1 = str_to_calc[0]
|
|
op = str_to_calc[1]
|
|
num2 = str_to_calc[2]
|
|
res = 0
|
|
|
|
if op == '-':
|
|
res = int(num1) - int(num2)
|
|
elif op == '+':
|
|
res = int(num1) + int(num2)
|
|
elif op == ':':
|
|
res = int(num1) / int(num2)
|
|
elif op == 'x':
|
|
res = int(num1) * int(num2)
|
|
|
|
return res """
|
|
|
|
|
|
def exoGen(model):
|
|
# print(model)
|
|
model = model.replace('{', ' {') # ajout d'espace pour le .split a venir
|
|
model = model.replace('}', '} ') # ajout d'espace pour le .split a venir
|
|
model = model.replace('(', ' (') # ajout d'espace pour le .split a venir
|
|
model = model.replace(')', ') ') # ajout d'espace pour le .split a venir
|
|
|
|
model = model.split()
|
|
|
|
index_brackets = {}
|
|
# print(model)
|
|
starts = -1
|
|
result = [] # resultat afficher
|
|
workResult = model[:] # resultat de travail
|
|
i = 0
|
|
#print(model)
|
|
while i < len(model):
|
|
c = model[i]
|
|
# print(c)
|
|
if c == '(':
|
|
starts = model.index(c)
|
|
# print('found', starts)
|
|
|
|
if c == ')' and starts != -1:
|
|
#print('found end ')
|
|
end = model.index(c)
|
|
index_brackets[starts] = end
|
|
#print('() ', ''.join(model[starts + 1:end]))
|
|
|
|
bra_result = exo(''.join(model[starts + 1:end]))
|
|
# print(bra_result)
|
|
# print(starts, end)
|
|
|
|
#print("modmode", model)
|
|
del model[starts:end]
|
|
del workResult[starts:end]
|
|
model[starts] = f'({bra_result["result"]})'
|
|
workResult[starts] = str(bra_result["resultat"])
|
|
starts = -1
|
|
i -= end-starts
|
|
#print('mod', model, workResult)
|
|
""" workResult.append(f'({bra_result["resultat"]})')
|
|
result.append(f'({bra_result["result"]})') """
|
|
i += 1
|
|
|
|
#print('resultats: \n',result, '\n', workResult, '\n', model)
|
|
|
|
for item in model:
|
|
if item[0] == '{':
|
|
item = item.replace('{', '')
|
|
item = item.replace('}', '')
|
|
choices_list = item.split('/')
|
|
if choices_list[0].replace('.', '').isdigit() or choices_list[1].replace('.', '').isdigit():
|
|
result.append(item)
|
|
workResult[model.index('{'+item+'}')] = item
|
|
model[model.index('{'+item+'}')] = item
|
|
|
|
else:
|
|
choice = rnd.choice(choices_list)
|
|
result.append(str(choice) + '')
|
|
workResult[model.index('{'+item+'}')] = choice
|
|
model[model.index('{'+item+'}')] = choice
|
|
|
|
else:
|
|
result.append(item + '')
|
|
|
|
# print(model, 'mod\nres', result, '\n wr', workResult)
|
|
calc_result = 0
|
|
result = workResult[:]
|
|
for i in range(0, len(workResult)):
|
|
choices = workResult[i]
|
|
|
|
if choices.count('/') >= 1:
|
|
choices_list = choices.split('/')
|
|
if choices_list[0].replace('.', '').isdigit() or choices_list[1].replace('.', '').isdigit():
|
|
|
|
# trouver le type pour les conversion
|
|
Type = ('int', 'float')[choices_list[0].count('.') >= 1]
|
|
TypeLimit = Type
|
|
passer = False
|
|
|
|
try:
|
|
DecimalNumber = len(choices_list[0].split('.')[1])
|
|
except:
|
|
DecimalNumber = 0
|
|
|
|
previous = workResult[i - 1].replace(' ', '') # item précédent
|
|
#print('previous', result.index(previous))
|
|
try:
|
|
# si il y a un next item sinon on le set a rien
|
|
next_item = workResult[i + 1].replace(' ', '')
|
|
except:
|
|
next_item = ''
|
|
|
|
if previous == '-':
|
|
#si le précédent est un moins, on vérifie que le max pour l'item actuel ne dépasse pas le résultat actuel
|
|
previous_number = float(
|
|
workResult[i - 2]) if workResult[i - 2].count('.') >= 1 else int(workResult[i - 2])
|
|
maxGive = int(choices_list[1]) if Type == 'int' else float(
|
|
choices_list[1])
|
|
max_limit = (calc_result, maxGive)[calc_result >= maxGive]
|
|
TypeLimit = 'float' if type(
|
|
calc_result) == float else 'int'
|
|
# print('max --', max_limit)
|
|
|
|
elif previous == ':':
|
|
try:
|
|
nextChoices = workResult[i + 2].split('/')
|
|
nextMin = int(nextChoices[0]) if nextChoices[1].count(
|
|
".") < 1 else float(nextChoices[0])
|
|
nextMax = int(nextChoices[1]) if nextChoices[1].count(
|
|
".") < 1 else float(nextChoices[1])
|
|
max_limit = calc_result/nextMin
|
|
min_limit = calc_result/nextMax
|
|
passer = True
|
|
except:
|
|
max_limit = float(choices_list[1]) if Type == 'float' else int(
|
|
choices_list[1])
|
|
|
|
else:
|
|
if not passer:
|
|
max_limit = float(choices_list[1]) if Type == 'float' else int(
|
|
choices_list[1])
|
|
|
|
if next_item == '-' and not passer:
|
|
if i != 0:
|
|
next_number = workResult[i + 2]
|
|
next_choices = next_number.split('/')
|
|
next_min = float(next_choices[0]) if Type == 'float' else int(
|
|
next_choices[0])
|
|
# print('previous', previous)
|
|
# print('to calc',f'{calc_result}{previous}{next_min}')
|
|
#print('calc_result next _min',strCalc(f'{calc_result}{previous}{next_min}'))
|
|
|
|
next_number = workResult[i + 2]
|
|
next_choices = next_number.split('/')
|
|
next_min = float(next_choices[0]) if Type == 'float' else int(
|
|
next_choices[0])
|
|
actual_min = float(choices_list[0]) if Type == 'float' else int(
|
|
choices_list[0])
|
|
if next_min > actual_min:
|
|
min_limit = float(next_choices[0]) if Type == 'float' else int(
|
|
next_choices[0])
|
|
elif next_min < actual_min:
|
|
min_limit = float(choices_list[0]) if Type == 'float' else int(
|
|
choices_list[0])
|
|
elif next_min == actual_min:
|
|
min_limit = float(choices_list[0]) if Type == 'float' else int(
|
|
choices_list[0])
|
|
#print('min ---', min_limit)
|
|
|
|
else:
|
|
if not passer:
|
|
next_min = -0
|
|
min_limit = float(choices_list[0]) if Type == 'float' else int(
|
|
choices_list[0])
|
|
#print('min &&', min_limit)
|
|
#print(min_limit, max_limit)
|
|
item_result = rnd.randint(min_limit, max_limit) if TypeLimit == 'int' else round(
|
|
rnd.uniform(min_limit, max_limit), DecimalNumber)
|
|
|
|
if previous.replace(' ', '') == '-':
|
|
calc_result = calc_result - item_result
|
|
elif previous.replace(' ', '') == '+':
|
|
calc_result = calc_result + item_result
|
|
elif previous.replace(' ', '') == ':':
|
|
calc_result = calc_result / item_result
|
|
elif previous.replace(' ', '') == 'x':
|
|
calc_result = calc_result * item_result
|
|
else:
|
|
calc_result = calc_result + item_result
|
|
|
|
#print((tempo_result < next_min and next_item == '- '), 'next')
|
|
|
|
if calc_result < next_min and i != 0 and next_min != -0 and previous.replace(' ', '') == '-':
|
|
|
|
previous_max = result[i - 2].split('/')[1]
|
|
|
|
if previous_number + next_min > (int(previous_max) if type(previous_max) == int else float(previous_max)):
|
|
item_result -= next_min
|
|
max_limit -= next_min
|
|
calc_result += next_min
|
|
|
|
else:
|
|
max_limit += next_min
|
|
workResult[i - 2] = str((int(workResult[i - 2]) if type(workResult[i - 1])
|
|
== int else float(workResult[i-2])) + next_min)
|
|
calc_result += next_min
|
|
|
|
if type(item_result) == float and item_result - int(item_result) == 0 and Type == 'int':
|
|
item_result = int(item_result)
|
|
|
|
workResult[i] = str(item_result) + ''
|
|
model[i] = str(item_result) + ''
|
|
#print('changed', workResult)
|
|
|
|
elif choices.count('/') == 0:
|
|
if choices.replace('.', '').isdigit() or choices.replace('.', '').isdigit():
|
|
calc_result += int(choices) if choices.count(
|
|
'.') == 0 else float(choices)
|
|
|
|
result = ' '.join(model)
|
|
return {'result': result, 'resultat': calc_result}
|
|
|
|
|
|
def exo(model):
|
|
result = None
|
|
|
|
while result == None:
|
|
try:
|
|
result = exoGen(model)
|
|
return result
|
|
except:
|
|
result = None
|
|
return result
|