Exemple #1
0
def test_constructor():
    s = Stack()  # creation of object via constructor
    # assert command results in a boolean where you compare your test results
    # with an expected output, if assert returns false, the test fails
    # assert 1 == 2  # try me
    assert isinstance(s, Stack)

    # in a test driven development, you will first write the tests, then modify
    # the functionality of your code such that it passes the tests
    assert len(s) == 0  # to pass this test, Stack needs to implement __len__()
Exemple #2
0
def check_balanced(expr):
    
    opening = '([{'
    closing = ')]}'
    
    opening_d = {opening[i] : closing[i] for i in range(len(opening))}
    closing_d = {closing[i] : opening[i] for i in range(len(opening))}
    
    s = Stack()
    for i, c in enumerate(expr):
        if c in opening_d:
            s.push(c)
        if c in closing_d:
            if not s.is_empty() and opening_d[s.peek()] == c:
                s.pop()
            else:
                print('parenthèse fermante en trop au caractère', i+1)
                return False
                
    return s.size() == 0
Exemple #3
0
def is_balanced(head: Node) -> bool:
    n = head
    s = Stack()
    balanced = True
    while (n != None and balanced):
        if n.record == "(":
            s.push(n.record)
        else:
            if (s.isEmpty()):
                balanced = False
            else:
                s.pop()
        n = n.next

    if balanced and s.isEmpty():
        return True
    else:
        return False
Exemple #4
0
def dec_to_bin(dec: int) -> str:
    """
    Converts decimal number to binary

    :param dec: decimal input to convert
    :return:
    """
    s = Stack()
    if dec == 0:
        s.push(0)
    else:
        while dec > 0:
            rem = dec % 2
            s.push(rem)
            dec = dec // 2  # floor division

    return __construct(s)
Exemple #5
0
def dec_to_n(input: int, base: int): #-> str:
    s = Stack()
    if (base < 2 or base > 16):
        print("Error: Invalid base.")
        return
    else:
        units = __units(base)
        while(input > 0):
            rem = input % base
            transform_rem = rem
            if base > 10 :
                transform_rem = units[rem]
            s.push(transform_rem)
            input = input // base
    
    # compose string
    acc = []
    while (not s.isEmpty()):
        acc.append(s.pop())

    return ''.join(str(i) for i in acc)
class Queue:
    def __init__(self):
        self.push_queue = Stack()
        self.pop_queue = Stack()

    def is_empty(self):
        return self.pop_queue.is_empty() and self.push_queue.is_empty()

    def enqueue(self, data):
        self.push_queue.push(data)

    def dequeue(self):
        if self.is_empty():
            raise Exception('Can\'t dequeue. Queue is empty')

        if self.pop_queue.is_empty():
            while (not self.push_queue.is_empty()):
                self.push_queue.push(self.push_queue.pop())

        return self.pop_queue.pop()

    def size(self):
        return self.push_queue.size() + self.pop_queue.size()
Exemple #7
0
def test_constructor():
    s = Stack()
    assert isinstance(s, Stack)
    assert len(s) == 0
Exemple #8
0
def test_pop():
    stack = Stack()
    stack.push(1)
    assert stack.pop() == 1
Exemple #9
0
class TestStack(unittest.TestCase):
    def setUp(self) -> None:
        self.list = Stack()

    def test_str_stack(self):
        self.list.push(1)
        self.list.push(2)
        self.assertEqual(str(self.list), '[2, 1]')

    def test_iter_stack(self):
        self.list.push(1)
        self.list.push(2)
        self.assertEqual([x for x in self.list], [2, 1])

    def test_len_stack(self):
        self.list.push(1)
        self.list.push(2)
        self.assertEqual(len(self.list), 2)
        _list = Stack([1, 2, 3, 4, 5])
        self.assertEqual(len(_list), 5)

    def test_stack_with_starting_data(self):
        _list = Stack([5, 4, 3])
        self.assertEqual([x for x in _list], [3, 4, 5])
        _list.push(1)
        self.assertEqual([x for x in _list], [1, 3, 4, 5])

    def test_is_empty(self):
        self.assertTrue(self.list.is_empty)
        self.list.push(1)
        self.assertFalse(self.list.is_empty)

    def test_push_stack(self):
        self.list.push(1)
        self.assertEqual([x for x in self.list], [1])
        self.list.push(2)
        self.assertEqual([x for x in self.list], [2, 1])

    def test_pop_stack(self):
        self.list.push(1)
        self.list.push(2)
        self.list.push(3)
        data = self.list.pop()
        self.assertEqual(data, 3)
        self.assertEqual([x for x in self.list], [2, 1])
        self.list.push(10)
        self.list.push(100)
        self.list.pop()
        data = self.list.pop()
        self.assertEqual(data, 10)
        self.assertEqual([x for x in self.list], [2, 1])

    def test_pop_empty(self):
        with self.assertRaises(RuntimeError):
            self.list.push(1)
            self.list.pop()
            self.list.pop()

    def test_peek(self):
        self.list.push(1)
        self.list.push(2)
        data = self.list.peek()
        self.assertEqual(data, 2)

    def test_peek_empty(self):
        with self.assertRaises(RuntimeError):
            self.list.peek()
Exemple #10
0
 def test_len_stack(self):
     self.list.push(1)
     self.list.push(2)
     self.assertEqual(len(self.list), 2)
     _list = Stack([1, 2, 3, 4, 5])
     self.assertEqual(len(_list), 5)
Exemple #11
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.test_stack = Stack()
Exemple #12
0
class StackTest(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.test_stack = Stack()

    def test_to_array(self):
        self.test_stack.push('4')
        self.test_stack.push('5')

        self.assertEqual(self.test_stack.to_array(), ['5', '4'])

    def test_push(self):
        self.test_stack.push(4)
        self.test_stack.push(5)

        self.assertEqual(self.test_stack.to_array(), [5, 4])

    def test_pop(self):
        self.test_stack.push(4)
        self.test_stack.push(5)

        self.assertEqual(self.test_stack.pop(), 5)
        self.assertEqual(self.test_stack.to_array(), [4])

    def test_length(self):
        self.test_stack.push(4)
        self.test_stack.push(5)
        self.test_stack.push(4)
        self.test_stack.pop()

        self.assertEqual(len(self.test_stack), 2)

    def test_peek(self):
        self.test_stack.push(4)
        self.test_stack.push(5)
        self.test_stack.push(3)

        self.assertEqual(self.test_stack.peek(), 3)

    def test_is_empty(self):
        self.assertEqual(self.test_stack.is_empty(), True)
Exemple #13
0
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from ds.stack import Stack

if __name__ == "__main__":
    string = 'This will be reversed'
    stack = Stack()

    for s in string:
        stack.push(s)

    stack.print(sep='', top_marker='')
Exemple #14
0
from ds.stack.Stack import *
# myStack = Stack()
myStack = Stack([40, 50, 60])
myStack.printStack()
myStack.push(10)
myStack.push(40)
myStack.printStack()
print(s)
foo(15)
Exemple #15
0
def test_push_over_size():
    stack = Stack()
    with pytest.raises(AssertionError):
        for i in range(0, 11):
            stack.push(i)
 def __init__(self):
     self.push_queue = Stack()
     self.pop_queue = Stack()
Exemple #17
0
    def test_stack(self):
        stack = Stack()
        for i in [23, 4, 2, 56]:
            stack.push(i)

        self.assertEqual(stack.size(), 4)
        self.assertEqual(stack.peek(), 56)
        self.assertEqual(stack.pop(), 56)
        stack.pop()
        stack.pop()
        stack.pop()
        self.assertEqual(stack.size(), 0)
        self.assertRaises(RuntimeError, stack.pop)
Exemple #18
0
def test_constructor():
    # Create object s from Stack class:
    s = Stack()
    assert isinstance(s, Stack)
    # Call len() will call def __len__(self)
    assert len(s) == 0
Exemple #19
0
def stack():
    return Stack()
Exemple #20
0
def __construct(s: Stack) -> str:
    # compose string
    acc = ""
    while not s.isEmpty():
        acc = acc + str(s.pop())
    return acc
Exemple #21
0
 def test_stack_with_starting_data(self):
     _list = Stack([5, 4, 3])
     self.assertEqual([x for x in _list], [3, 4, 5])
     _list.push(1)
     self.assertEqual([x for x in _list], [1, 3, 4, 5])
def test_constructor():
    s = Stack()
    assert len(s) == 0
Exemple #23
0
 def setUp(self) -> None:
     self.list = Stack()
Exemple #24
0
def test_push():
    stack = Stack()
    for i in range(0, 10):
        stack.push(i)