Advertisement
here2share

# balance_detect_2.py

Sep 2nd, 2021
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. # balance_detect_2.py
  2.  
  3. import random
  4. from itertools import permutations
  5.  
  6.  
  7. brackets = permutations(['(','X',')']*3)
  8. brackets = list(set(brackets))
  9. random.shuffle(brackets)
  10. balance_count = 0
  11.  
  12. for i in brackets:
  13.     balance_check = 0
  14.     sss = ''.join(i)
  15.     p = ''
  16.     for s in sss:
  17.         if s == '(':
  18.             balance_check += 1
  19.         elif s == ')':
  20.             balance_check -= 1
  21.             if balance_check < 0:
  22.                 break
  23.         else:
  24.             if p == 'X':
  25.                 balance_check = -1
  26.                 break
  27.         p = s
  28.     balance_check = 'no' if balance_check else 'yes'
  29.     if balance_check == 'yes': balance_count += 1
  30.     print(sss + ' ' + balance_check)
  31.  
  32. print(str(balance_count) + ' ' + str(len(brackets)))
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement