H0_0H

Untitled

Oct 16th, 2021 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. s = input()
  2. st_num = []
  3. st_s = []
  4. ok = False
  5. res = ''
  6. num = ''
  7. s1 = ''
  8. now = ''
  9. count = 0
  10. for c in s:
  11.     if c == '(':
  12.         ok = True
  13.         if s1 != '':
  14.             st_s.append(s1)
  15.             s1 = ''
  16.         if num != '':
  17.             st_num.append(int(num))
  18.             num = ''
  19.         if st_s:
  20.             b = st_s.pop()
  21.             if count > 0:
  22.                 now += b
  23.             res += b
  24.         count += 1
  25.     elif c == ')':
  26.         count -= 1
  27.         if s1 != '':
  28.             st_s.append(s1)
  29.             s1 = ''
  30.         if num != '':
  31.             st_num.append(int(num))
  32.             num = ''
  33.         if ok:
  34.             ok = False
  35.             n = st_num.pop()
  36.             b = st_s.pop()
  37.             res += n * b
  38.             now += n * b
  39.         elif st_s:
  40.             b = st_s.pop()
  41.             res += b
  42.             now += b
  43.         if count == 0:
  44.             if st_num:
  45.                 res += (st_num.pop() - 1) * now
  46.             now = ''
  47.     elif c.isdigit():
  48.         num += c
  49.         if s1 != '':
  50.             st_s.append(s1)
  51.             s1 = ''
  52.     else:
  53.         s1 += c
  54.         if num != '':
  55.             st_num.append(int(num))
  56.             num = ''
  57.  
  58. while st_num:
  59.     res *= st_num.pop()
  60. if s1:
  61.     res += s1
  62. print(res)
  63.  
Add Comment
Please, Sign In to add comment