Skip to content

Instantly share code, notes, and snippets.

@huseyin
Created January 7, 2016 12:49
Show Gist options
  • Save huseyin/cb1e1e5ca0056c30c383 to your computer and use it in GitHub Desktop.
Save huseyin/cb1e1e5ca0056c30c383 to your computer and use it in GitHub Desktop.
Detaylandırılmamış Veri Yapıları çalışması
# -*- coding: utf-8 -*-
import sys
SYNTASTIC_ELEMENTS = {
'(': ')',
'[': ']',
'{': '}',
}
class Stack:
def __init__(self):
self.list = []
def push(self, item):
self.list.append(item)
def pop(self):
return self.list.pop()
def peek(self):
return self.list[-1]
def size(self):
return len(self.list)
def isEmpty(self):
return self.list == []
def controlParans(strLine):
stack=Stack()
for token in strLine:
if token in SYNTASTIC_ELEMENTS.keys():
stack.push(token)
else:
if stack.isEmpty() and token in SYNTASTIC_ELEMENTS.values():
idx = strLine.index(token)
sys.stdout.write("Hiç açılmamış blok. Kritik durum!\n")
sys.stdout.write("%s\n%*s\033[0;31m^\n\033[0m\n" %(strLine, idx, ''))
if not stack.isEmpty():
if token == SYNTASTIC_ELEMENTS[stack.peek()]:
stack.pop()
if not stack.isEmpty():
idx = strLine.index(stack.peek())
sys.stdout.write("Kapanmayan bloklar mevcut!\n")
sys.stdout.write("%s\n%*s\033[0;31m^\n\033[0m\n" %(strLine, idx, ''))
if __name__ == '__main__':
controlParans("()")
controlParans("(")
controlParans(")")
controlParans("if ( []")
controlParans("if (control == true) { printf(\"Doğru\"); }")
controlParans("if (control == true) { printf(\"Doğru\"); ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment