Advertisement
malixds_

Untitled

Sep 12th, 2022
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. myfile=open('text.txt')
  2. i = 0
  3. rep = 0
  4. open = 0
  5. ans = 1e9
  6. first_close = 1e9
  7. tot_close = 0
  8. tot_open = 0
  9. while(myfile):
  10.     a = myfile.get()
  11.     if (a!="{" and a!="}"):
  12.         i+=1
  13.         continue
  14.     if (a=='{'):
  15.         open+=1
  16.         tot_open+=1
  17.     else:
  18.         first_close=min(first_close, i+1)
  19.         if(open==0):
  20.             rep+=1
  21.         else:
  22.             close+=1
  23.             tot_close+=1
  24.     if(open==1):
  25.         ans=min(ans,i+1)
  26.     if(open==0):
  27.         ans=1e9
  28.     i+=1
  29. if(rep>1 or abs(tot_open-tot_close)>1):
  30.     print(-1)
  31. elif(tot_open-tot_close == 0 or (open>1) or (rep==1 and open>0)):
  32.     print(-1)
  33. else:
  34.     if(tot_close>tot_open):
  35.         print(first_close)
  36.     else:
  37.         print(ans)
  38.    
  39.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement