Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dict_without_changes = {'0': ['6', '9'], '1': ['^11'], '2': ['3'], '3': ['2', '5'], '4': ['11'], '5': ['3'], '6': ['0', '9'], '9': ['0', '6'], '11': ['4'], '^11': ['1'], '-': ['/'], '/': ['-']}
- dict_subtraction = {'6': ['5'], '7': ['1'], '8': ['0', '6', '9'], '9': ['3', '5'], '+': ['-'], '*': ['/']}
- dict_additions = {'0': ['8'], '1': ['7'], '3': ['9'], '5': ['6', '9'], '6': ['8'], '9': ['8'], '-': ['+'], '/': ['*']}
- name_file_in = "e2.txt"
- name_file_out = "e0_dop_ans.txt"
- file_in = open(name_file_in, 'r')
- n = int(file_in.readline().rstrip())
- for i in range(n):
- example = file_in.readline().rstrip()
- ind_sign_equal = example.find('=')
- left_str = example[:ind_sign_equal]
- right_str = example[ind_sign_equal + 1:]
- ind_left = ind_sign_equal - 1
- ind_right = ind_sign_equal # так как далее будет конкатенация
- left_value = left_str.replace('/', '//')
- left_value = left_str.replace('^11', '**11')
- right_value = right_str.replace('/', '//')
- right_value = right_str.replace('^11', '**11')
- example_num = left_str + right_str
- ans = 0
- equality_true = False
- for index in range(len(example_num)):
- if example_num[index] in dict_without_changes:
- for modification_index in dict_without_changes[example_num[index]]:
- ans += 1
- #print(left_str, right_str)
- if modification_index == '^11' and (index == 0 or index == ind_sign_equal or (index != 0 and (example_num[index - 1] == '+' or example_num[index - 1] == '-' or example_num[index - 1] == '/' or example_num[index - 1] == '*'))):
- pass
- elif (modification_index == '+' or modification_index == '*' or modification_index == '/') and (index == 0 or index == ind_right):
- pass
- else:
- if index <= ind_left:
- #print(modification_index)
- left_str = left_str[:index] + modification_index + left_str[index + 1:]
- #print(example_num[:index])
- left_value = left_str.replace('/', '//')
- left_value = left_str.replace('^11', '**11')
- else:
- '''ans += 1
- print(ans, 'right', right_str[:index - ind_sign_equal], modification_index, right_str[index - ind_sign_equal + 1:])'''
- right_str = right_str[:index - ind_sign_equal] + modification_index + right_str[index - ind_sign_equal + 1:]
- right_value = right_str.replace('/', '//')
- right_value = right_str.replace('^11', '**11')
- ans += 1
- #print(left_str, right_str, 'cravnenie')
- if eval(left_value) == eval(right_value):
- equality_true = True
- if equality_true:
- break
- left_str = example[:ind_sign_equal]
- right_str = example[ind_sign_equal + 1:]
- left_value = left_str.replace('/', '//')
- right_value = right_str.replace('/', '//')
- left_value = left_str.replace('^11', '**11')
- right_value = right_str.replace('^11', '**11')
- '''ans += 1
- print(left_str, right_str)'''
- if equality_true:
- break
- if not equality_true:
- for index in range(len(example_num)):
- if example_num[index] in dict_subtraction:
- for modification_index in dict_subtraction[example_num[index]]:
- '''ans += 1
- print(modification_index, example)'''
- if equality_true:
- break
- for symbol_mileage_2 in range(len(example_num)):
- if example_num[symbol_mileage_2] in dict_additions:
- for modification_symbol_mileage_2 in dict_additions[example_num[symbol_mileage_2]]:
- ans += 1
- #print(left_str + '=' + right_str)
- if index <= ind_left:
- left_str = left_str[:index] + modification_index + left_str[index + 1:]
- left_value = left_str.replace('/', '//')
- left_value = left_str.replace('^11', '**11')
- else:
- right_str = right_str[:index - ind_sign_equal] + modification_index + right_str[index - ind_sign_equal + 1:]
- right_value = right_str.replace('/', '//')
- right_value = right_str.replace('^11', '**11')
- ans += 1
- #print(left_str + '=' + right_str)
- if symbol_mileage_2 <= ind_left:
- left_str = left_str[:symbol_mileage_2] + modification_symbol_mileage_2 + left_str[symbol_mileage_2 + 1:]
- left_value = left_str.replace('/', '//')
- left_value = left_str.replace('^11', '**11')
- else:
- right_str = right_str[:symbol_mileage_2 - ind_sign_equal] + modification_symbol_mileage_2 + right_str[symbol_mileage_2 - ind_sign_equal + 1:]
- right_value = right_str.replace('/', '//')
- right_value = right_str.replace('^11', '**11')
- ans += 1
- #print(left_str + '=' + right_str, 'cravnenie')
- if eval(left_value) == eval(right_value):
- equality_true = True
- if equality_true:
- break
- left_str = example[:ind_sign_equal]
- right_str = example[ind_sign_equal + 1:]
- left_value = left_str.replace('/', '//')
- right_value = right_str.replace('/', '//')
- left_value = left_str.replace('^11', '**11')
- right_value = right_str.replace('^11', '**11')
- '''ans += 1
- print(left_str + '=' + right_str)'''
- if equality_true:
- break
- if equality_true:
- break
- if equality_true:
- print(left_str + '=' + right_str)
- else:
- print(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement