Guest User

Untitled

a guest
Feb 23rd, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. def balanced_parens(n):
  2.    
  3.     def push_in(s, n1, n2):
  4.         print('przed ifami', s, n1, n2)
  5.         if n1 + n2 == n * 2: res.append(s); print(s); return
  6.         if n1 < n          : push_in(s + '(', n1 + 1, n2)
  7.         if n1 - n2 > 0     : push_in(s + ')', n1, n2 + 1)
  8.         print('jestem po ifach','n1:',n1,'n2:',n2,'s:',s)
  9.    
  10.     res = []; push_in('', 0, 0)
  11.    
  12.     return res
  13. print(balanced_parens(2))
  14.  
  15. '''
  16. OUTPUT:
  17.  
  18. przed ifami  0 0
  19. przed ifami ( 1 0
  20. przed ifami (( 2 0
  21. przed ifami (() 2 1
  22. przed ifami (()) 2 2
  23. (())
  24. jestem po ifach n1: 2 n2: 1 s: (()
  25. jestem po ifach n1: 2 n2: 0 s: ((
  26. przed ifami () 1 1
  27. przed ifami ()( 2 1
  28. przed ifami ()() 2 2
  29. ()()
  30. jestem po ifach n1: 2 n2: 1 s: ()(
  31. jestem po ifach n1: 1 n2: 1 s: ()
  32. jestem po ifach n1: 1 n2: 0 s: (
  33. jestem po ifach n1: 0 n2: 0 s:
  34. ['(())', '()()']
  35. '''
Advertisement
Add Comment
Please, Sign In to add comment