예제 #1
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)
예제 #2
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)
예제 #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
예제 #4
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
예제 #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)
예제 #6
0
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()
예제 #7
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()
예제 #8
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])
예제 #9
0
파일: stack_test.py 프로젝트: alzed/dsalgo
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)
예제 #10
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='')
예제 #11
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)
예제 #12
0
def test_push_over_size():
    stack = Stack()
    with pytest.raises(AssertionError):
        for i in range(0, 11):
            stack.push(i)
예제 #13
0
def test_pop():
    stack = Stack()
    stack.push(1)
    assert stack.pop() == 1
예제 #14
0
def test_push():
    stack = Stack()
    for i in range(0, 10):
        stack.push(i)