Advertisement
kompilainenn

Untitled

May 2nd, 2024
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. def solve_runes(runes):
  2.        
  3.     if runes[0] not in ([str(x) for x in range(10)] + ['-'] + ['?'] or ['-+', '-*'] in runes) or runes.startswith('0') or runes.startswith('-0'):
  4.         return -1
  5.    
  6.     if runes.find('+-') != -1:
  7.         first = runes[:runes.find('+-')]
  8.         operation = '+'
  9.         second = runes[runes.find('+-') + 1:runes.find('=')]
  10.         total = runes[runes.find('=') + 1:]
  11.     elif runes.find('*-') != -1:
  12.         first = runes[:runes.find('*-')]
  13.         operation = '*'
  14.         second = runes[runes.find('*-') + 1:runes.find('=')]
  15.         total = runes[runes.find('=') + 1:]
  16.     elif runes.find('--') != -1:
  17.         first = runes[:runes.find('--')]
  18.         operation = '-'
  19.         second = runes[runes.find('--') + 1:runes.find('=')]
  20.         total = runes[runes.find('=') + 1:]
  21.     elif runes.find('+') != -1:
  22.         first = runes[:runes.find('+')]
  23.         operation = '+'
  24.         second = runes[runes.find('+') + 1:runes.find('=')]
  25.         total = runes[runes.find('=') + 1:]
  26.     elif runes.find('*') != -1:
  27.         first = runes[:runes.find('*')]
  28.         operation = '*'
  29.         second = runes[runes.find('*') + 1:runes.find('=')]
  30.         total = runes[runes.find('=') + 1:]
  31.     elif runes.count('-') == 2:
  32.         first = runes[:runes.rfind('-')]
  33.         operation = '-'
  34.         second = runes[runes.rfind('-') + 1:runes.find('=')]
  35.         total = runes[runes.find('=') + 1:]
  36.    
  37.     for i in range(10):
  38.         first_for = first.replace('?', str(i), first.count('?'))
  39.         if first_for.startswith('00'):
  40.             break
  41.         second_for = second.replace('?', str(i), second.count('?'))
  42.         if second_for.startswith('00'):
  43.             break
  44.         total_for = total.replace('?', str(i), total.count('?'))
  45.         if total_for.startswith('00'):
  46.             break
  47.        
  48.         if operation == '+':
  49.             if int(first_for) + int(second_for) == int(total_for):
  50.                 return i
  51.         elif operation == '-':
  52.             if int(first_for) - int(second_for) == int(total_for):
  53.                 return i
  54.         elif operation== '*':
  55.             if int(first_for) * int(second_for) == int(total_for):
  56.                 return i
  57.     return -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement