Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Nov 17th, 2018 68 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1.  
  2. # Python program for linked list implementation of stack
  3.  
  4. # Class to represent a node
  5. class StackNode:
  6.  
  7.     # Constructor to initialize a node
  8.     def __init__(self, data):
  9.         self.data = data  
  10.         self.next = None
  11.  
  12. class Stack:
  13.      
  14.     # Constructor to initialize the root of linked list
  15.     def __init__(self):
  16.         self.root = None
  17.  
  18.     def isEmpty(self):
  19.         return True if self.root is None else  False
  20.  
  21.     def push(self, data):
  22.         newNode = StackNode(data)
  23.         newNode.next = self.root  
  24.         self.root = newNode
  25.         #print "%d pushed to stack" %(data)
  26.      
  27.     def pop(self):
  28.         if (self.isEmpty()):
  29.             return float("-inf")
  30.         temp = self.root  
  31.         self.root = self.root.next
  32.         popped = temp.data
  33.         return popped
  34.      
  35.     def peek(self):
  36.         if self.isEmpty():
  37.             return float("-inf")
  38.         return self.root.data
  39.        
  40. def mismoTipo(abierto):
  41.     if(abierto == '('): return ')'
  42.     if(abierto == '['): return ']'
  43.     if(abierto == '{'): return '}'
  44.     return 'x'
  45.  
  46. def braces_status(s):
  47.     pila = Stack()
  48.     abiertos = '([{'
  49.     cerrados = ')]}'
  50.    
  51.     for i in range(0, len(s)) :
  52.         if(s[i] in abiertos):
  53.             pila.push(s[i])
  54.         #if (pila.isEmpty()):return False
  55.         if(s[i] in cerrados):
  56.             if(mismoTipo( pila.peek()) == s[i] ):
  57.                 pila.pop()
  58.             else:
  59.                 return False
  60.     return pila.isEmpty()
  61.  
  62.  
  63. #s = "{}{)"
  64. s = '[][)[][]'
  65. print(braces_status(s))
  66. s = '(12[x]34)'
  67. print(braces_status(s))
  68. s = "()y(){}y{(){}()}" #True
  69. print(braces_status(s))
  70. #expecting: "True"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top