Skip to content

Instantly share code, notes, and snippets.

@austa
Created November 5, 2013 19:15
Show Gist options
  • Save austa/7324475 to your computer and use it in GitHub Desktop.
Save austa/7324475 to your computer and use it in GitHub Desktop.
import math
#Listenin sonu yigitin tepesi
class Stack:
def __init__(self):
self.liste = []
def isEmpty(self):
return self.liste == []
def push(self,eleman):
self.liste.append(eleman)
def peek(self):
return self.liste[-1]
def pop(self):
return self.liste.pop()
def sizeOf(self):
return len(self.liste)
#Listenin basi yigitin tepesi
class Stack1:
def __init__(self):
self.liste = []
def isEmpyt(self):
return self.liste == []
def push(self, eleman):
self.liste.insert(0,eleman)
def pop(self):
return self.liste.pop(0)
def peek(self):
return self.liste[-1]
def sizeOf(self):
return len(self.liste)
#Stack kullanilarak verilen sayiyi 2'lik tabana cevirme
def ikiliyeCevir(sayi):
s = Stack()
while sayi > 0:
kalan = sayi % 2
s.push(kalan)
sayi = sayi / 2
bosString = ""
while not s.isEmpty():
bosString = bosString + str(s.pop())
return bosString
print ikiliyeCevir(55)
print "-----------------------------------------"
#Stack kullanilarak verilen sayisi, istenilen tabana cevirme
def tabanCevir(sayi,taban):
s=Stack()
degerler = "0123456789ABCDEF"
while sayi > 0:
kalan = sayi % taban
s.push(kalan)
sayi = sayi / taban
bosString = ""
while not s.isEmpty():
bosString = bosString + str(degerler[s.pop()])
return bosString
print tabanCevir(26,16)
print "-----------------------------------------"
#16,8,2'lik tabanda verilen sayilari, Stack kullanilarak 10'luk tabana cevirmek
def kontrolEtDonustur(sayi):
deger = 0
s=Stack()
degerler = "0123456789ABCDEF"
x = 0
for i in sayi:
if i == "0":
if sayi[sayi.index(i) + 1] == "x":
print "Girmis oldugunuz sayi 16 tabaninda, 10'luk tabana donusecek"
for a in sayi[2:]:
s.push(a)
while not s.isEmpty():
deger = deger + degerler.index(s.pop())*math.pow(16,x)
x +=1
return deger
elif sayi[sayi.index(i) + 1] == "b":
print "Girmis oldugunuz sayi 2'lik tabanda, 10'luk tabana donusecek"
for a in sayi[2:]:
a = int(a)
s.push(a)
while not s.isEmpty():
deger = deger + s.pop()*math.pow(2,x)
x +=1
return deger
else:
print "Girmis oldugunuz sayi 8 tabaninda, 10'luk tabana donusecek"
for a in sayi[1:]:
s.push(a)
while not s.isEmpty():
deger = deger + degerler.index(s.pop())*math.pow(8,x)
x += 1
return deger
elif i != "0":
print "Girmis oldugunuz sayi 10 taninda"
return sayi
print kontrolEtDonustur("0x11A")
print kontrolEtDonustur("0b11")
print kontrolEtDonustur("011")
print kontrolEtDonustur("11")
print "-----------------------------------------"
#Stack kullanilarak, parantezlerin dengeli olup olmadigi kontrolu.
def dengeliParantez(parantez):
s = Stack()
deger = True
sayac = 0
while sayac < len(parantez) and deger:
if parantez[sayac] in "{[(":
s.push(parantez[sayac])
else:
if s.isEmpty():
deger = False
else:
sonDeger = s.pop()
if not karsilastir(sonDeger,parantez[sayac]):
deger = False
sayac +=1
if deger and s.isEmpty():
return True
else:
return False
def karsilastir(acik,kapali):
aciklar = "([{"
kapalilar = ")]}"
return aciklar.index(acik) == kapalilar.index(kapali)
print dengeliParantez("([{()}])")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment