Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.76 KB | None | 0 0
  1. 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'], '-': ['/'], '/': ['-']}
  2. dict_subtraction = {'6': ['5'], '7': ['1'], '8': ['0', '6', '9'], '9': ['3', '5'], '+': ['-'], '*': ['/']}
  3. dict_additions = {'0': ['8'], '1': ['7'], '3': ['9'], '5': ['6', '9'], '6': ['8'], '9': ['8'], '-': ['+'], '/': ['*']}
  4.  
  5. name_file_in = "e2.txt"
  6. name_file_out = "e0_dop_ans.txt"
  7.  
  8. file_in = open(name_file_in, 'r')
  9. n = int(file_in.readline().rstrip())
  10. for i in range(n):
  11.     example = file_in.readline().rstrip()
  12.     ind_sign_equal = example.find('=')
  13.  
  14.     left_str = example[:ind_sign_equal]
  15.     right_str = example[ind_sign_equal + 1:]
  16.     ind_left = ind_sign_equal - 1
  17.     ind_right = ind_sign_equal  # так как далее будет конкатенация
  18.     left_value = left_str.replace('/', '//')
  19.     left_value = left_str.replace('^11', '**11')
  20.     right_value = right_str.replace('/', '//')
  21.     right_value = right_str.replace('^11', '**11')
  22.  
  23.     example_num = left_str + right_str
  24.     ans = 0
  25.     equality_true = False
  26.     for index in range(len(example_num)):
  27.         if example_num[index] in dict_without_changes:
  28.             for modification_index in dict_without_changes[example_num[index]]:
  29.                 ans += 1
  30.                 #print(left_str, right_str)
  31.                 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] == '*'))):
  32.                     pass
  33.                 elif (modification_index == '+' or modification_index == '*' or modification_index == '/') and (index == 0 or index == ind_right):
  34.                     pass
  35.                 else:
  36.                     if index <= ind_left:
  37.                         #print(modification_index)
  38.                         left_str = left_str[:index] + modification_index + left_str[index + 1:]
  39.                         #print(example_num[:index])
  40.                         left_value = left_str.replace('/', '//')
  41.                         left_value = left_str.replace('^11', '**11')
  42.                     else:
  43.                         '''ans += 1
  44.                        print(ans, 'right', right_str[:index - ind_sign_equal], modification_index, right_str[index - ind_sign_equal + 1:])'''
  45.                         right_str = right_str[:index - ind_sign_equal] + modification_index + right_str[index - ind_sign_equal + 1:]
  46.                         right_value = right_str.replace('/', '//')
  47.                         right_value = right_str.replace('^11', '**11')
  48.                     ans += 1
  49.                     #print(left_str, right_str, 'cravnenie')
  50.                     if eval(left_value) == eval(right_value):
  51.                         equality_true = True
  52.  
  53.                     if equality_true:
  54.                         break
  55.                 left_str = example[:ind_sign_equal]
  56.                 right_str = example[ind_sign_equal + 1:]
  57.                 left_value = left_str.replace('/', '//')
  58.                 right_value = right_str.replace('/', '//')
  59.                 left_value = left_str.replace('^11', '**11')
  60.                 right_value = right_str.replace('^11', '**11')
  61.                 '''ans += 1
  62.                print(left_str, right_str)'''
  63.         if equality_true:
  64.             break
  65.  
  66.     if not equality_true:
  67.         for index in range(len(example_num)):
  68.             if example_num[index] in dict_subtraction:
  69.                 for modification_index in dict_subtraction[example_num[index]]:
  70.                     '''ans += 1
  71.                    print(modification_index, example)'''
  72.                     if equality_true:
  73.                         break
  74.                     for symbol_mileage_2 in range(len(example_num)):
  75.                         if example_num[symbol_mileage_2] in dict_additions:
  76.                             for modification_symbol_mileage_2 in dict_additions[example_num[symbol_mileage_2]]:
  77.                                 ans += 1
  78.                                 #print(left_str + '=' + right_str)
  79.                                 if index <= ind_left:
  80.                                     left_str = left_str[:index] + modification_index + left_str[index + 1:]
  81.                                     left_value = left_str.replace('/', '//')
  82.                                     left_value = left_str.replace('^11', '**11')
  83.                                 else:
  84.                                     right_str = right_str[:index - ind_sign_equal] + modification_index + right_str[index - ind_sign_equal + 1:]
  85.                                     right_value = right_str.replace('/', '//')
  86.                                     right_value = right_str.replace('^11', '**11')
  87.                                 ans += 1
  88.                                 #print(left_str + '=' + right_str)
  89.                                 if symbol_mileage_2 <= ind_left:
  90.                                     left_str = left_str[:symbol_mileage_2] + modification_symbol_mileage_2 + left_str[symbol_mileage_2 + 1:]
  91.                                     left_value = left_str.replace('/', '//')
  92.                                     left_value = left_str.replace('^11', '**11')
  93.                                 else:
  94.                                     right_str = right_str[:symbol_mileage_2 - ind_sign_equal] + modification_symbol_mileage_2 + right_str[symbol_mileage_2 - ind_sign_equal + 1:]
  95.                                     right_value = right_str.replace('/', '//')
  96.                                     right_value = right_str.replace('^11', '**11')
  97.                                 ans += 1
  98.                                 #print(left_str + '=' + right_str, 'cravnenie')
  99.                                 if eval(left_value) == eval(right_value):
  100.                                     equality_true = True
  101.  
  102.                                 if equality_true:
  103.                                     break
  104.  
  105.                                 left_str = example[:ind_sign_equal]
  106.                                 right_str = example[ind_sign_equal + 1:]
  107.                                 left_value = left_str.replace('/', '//')
  108.                                 right_value = right_str.replace('/', '//')
  109.                                 left_value = left_str.replace('^11', '**11')
  110.                                 right_value = right_str.replace('^11', '**11')
  111.  
  112.                                 '''ans += 1
  113.                                print(left_str + '=' + right_str)'''
  114.                         if equality_true:
  115.                             break
  116.             if equality_true:
  117.                 break
  118.     if equality_true:
  119.         print(left_str + '=' + right_str)
  120.     else:
  121.         print(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement