def test_peek(self):
        self.assertEqual(self.num_stack.peek(), 3)
        self.assertEqual(self.letter_stack.peek(), "b")

        empty_stack = Stack()
        with self.assertRaises(LookupError):
            empty_stack.peek()
    def setUp(self):
        self.num_stack = Stack()
        self.num_stack.push(1)
        self.num_stack.push(2)
        self.num_stack.push(3)

        self.letter_stack = Stack()
        self.letter_stack.push("a")
        self.letter_stack.push("b")
Exemplo n.º 3
0
 def push(self, value):
     if self.stacks_num == 0:
         self._stacks = [Stack([value])]
     else:
         top_stack = self._stacks[-1]
         if top_stack.size == self._stack_max_size:
             self._stacks.append(Stack([value]))
         else:
             top_stack.push(value)
    def test_pop(self):
        popped = self.num_stack.pop()
        self.assertEqual(len(self.num_stack), 2)
        self.assertEqual(popped, 3)
        popped = self.num_stack.pop()
        self.assertEqual(popped, 2)
        self.assertEqual(len(self.num_stack), 1)

        empty_stack = Stack()
        with self.assertRaises(LookupError):
            empty_stack.pop()
Exemplo n.º 5
0
def parens_balanced(string):
    open_chars = ["(", "{", "["]
    closed_chars = [")", "}", "]"]
    stack = Stack()
    for char in string:
        if char in open_chars:
            stack.push(char)
        if char in closed_chars:
            if len(stack) == 0:
                return False
            popped = stack.pop()
            if open_chars.index(popped) != closed_chars.index(char):
                return False
    return len(stack) == 0
class TestStack(unittest.TestCase):
    def setUp(self):
        self.num_stack = Stack()
        self.num_stack.push(1)
        self.num_stack.push(2)
        self.num_stack.push(3)

        self.letter_stack = Stack()
        self.letter_stack.push("a")
        self.letter_stack.push("b")

    def test_peek(self):
        self.assertEqual(self.num_stack.peek(), 3)
        self.assertEqual(self.letter_stack.peek(), "b")

        empty_stack = Stack()
        with self.assertRaises(LookupError):
            empty_stack.peek()

    def test_push(self):
        empty_stack = Stack()
        empty_stack.push(4)
        self.assertEqual(len(empty_stack), 1)

    def test_len(self):
        self.assertEqual(len(self.num_stack), 3)
        self.assertEqual(len(self.letter_stack), 2)

    def test_repr(self):
        self.assertEqual(repr(self.num_stack), "Stack(1, 2, 3)")
        self.assertEqual(repr(self.letter_stack), "Stack(a, b)")

    def test_pop(self):
        popped = self.num_stack.pop()
        self.assertEqual(len(self.num_stack), 2)
        self.assertEqual(popped, 3)
        popped = self.num_stack.pop()
        self.assertEqual(popped, 2)
        self.assertEqual(len(self.num_stack), 1)

        empty_stack = Stack()
        with self.assertRaises(LookupError):
            empty_stack.pop()
Exemplo n.º 7
0
    def __init__(self, stack_max_size, elements=[]):

        self._stacks = []
        self._stack_max_size = stack_max_size

        def chunck(l):
            n = 0
            l_len = len(l)
            while n < l_len:
                yield slice(n, min(n + stack_max_size, l_len), 1)
                n += stack_max_size

        for sl in chunck(elements):
            self._stacks.append(Stack(elements[sl]))
Exemplo n.º 8
0
from lib import Stack, HTMLNode, CSSNode, parseTagName

FILENAME = "source.html"

with open(FILENAME, 'r') as f:
    raw = f.readlines()
    code = []
    for line in raw:
        code.append(line.strip())
    code = ''.join(code)

stack = Stack()
parentsTracker = Stack()
parentsTracker.push(HTMLNode("root", "", "", [], {}))
nodes = []
notParents = ['img', 'link', 'input']
scripts, stylesheets = [], []
favicon = []

# parse HTML
startTag, endTag = False, False
innerText = []
for i in range(len(code)):
    char = code[i]
    if char == '<' and code[i + 1] == "/":
        parentsTracker.pop()
        startTag, endTag = False, True
        if len(innerText) > 0:
            nodes[len(nodes) - 1].innerText = ''.join(innerText)
            innerText = []
    elif char == '<':
Exemplo n.º 9
0
from lib import Stack

s = Stack.Stack()
count = 0


def dec_to_bin(dec):
    count = 0
    while (dec > 0):
        rem = dec % 2
        # print(rem)
        s.push(rem)
        dec = dec // 2
        count += 1
    while (count > 0):
        print(s.pop(), sep='', end='')
        count -= 1


dec_to_bin(100)
Exemplo n.º 10
0
from tkMessageBox import *
from FileDialog import *
from scapy.all import *
import sys
from lib import Stack

frame=Ether()
ippkt=IP()
arppkt=ARP()
tcpseg=TCP()
udpseg=UDP()
icmpseg=ICMP()
full=Ether()/IP()/TCP()
from tkFileDialog import askopenfilename

mystack=Stack.stack()

hand=1

#command action
def GenerateEther():
    global frame
    state=(InputCheck.MacCheck(macda.get()))and(InputCheck.MacCheck(macsa.get()))
    if state==0:
        showerror(title="ERROR",message="you have inputed wrong mac format")
    else:
        frame=Ether(src=macda.get(),dst=macsa.get(),type=ethertype.get())
        text1.delete('1.0',END)
        newstring="src: "+frame.src+'\n'+"dst: "+frame.dst+'\n'+"type: "+str(frame.type)+'\n'
        text1.tag_config('a',foreground = 'red')
        text1.insert(END,"####[ Ethernet ]####\n",'a')
Exemplo n.º 11
0
from lib import Stack, Queue

# Stack code

# uncomment when you've implemented "__init__"
s = Stack()
print(s._data,
      "should be []")  # note, you don't normally using the _underscore_data
# uncomment when you've implemented "push"
s.push(5)
s.push(6)
print(s._data, "should be [5, 6]")

# uncomment when you've implemented "__len__"
print(len(s), "should be 2")
# uncomment when you've implemented "__repr__"
print(s, "should be Stack(5, 6)")

# uncomment when you've implemented "pop"
x = s.pop()

# uncomment before *** the when you've implemented
# __init__, __repr__, pop, and push
print(len(s), "should be 1")
print(s, "should be Stack(5)")

y = s.pop()

print(len(s), "should be 0")
print(s, "should be Stack()")
 def test_push(self):
     empty_stack = Stack()
     empty_stack.push(4)
     self.assertEqual(len(empty_stack), 1)