Advertisement
here2share

# balance_detect.py

Sep 2nd, 2021
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. # balance_detect.py
  2.  
  3. import random
  4. from itertools import permutations
  5.  
  6.  
  7. brackets = permutations(['(',')']*5)
  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.     for s in sss:
  16.         if s == '(':
  17.             balance_check += 1
  18.         else:
  19.             balance_check -= 1
  20.             if balance_check < 0:
  21.                 break
  22.     balance_check = 'no' if balance_check else 'yes'
  23.     if balance_check == 'yes': balance_count += 1
  24.     print(sss + ' ' + balance_check)
  25.  
  26. print(str(balance_count) + ' ' + str(len(brackets)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement