Exemplo n.º 1
0
class Queue:
    def __init__(self):
        self.enqueue_stack = ArrayStack()
        self.dequeue_stack = ArrayStack()
        self.size = 0

    def __len__(self):
        return self.size

    def is_empty(self):
        return len(self) == 0

    def enqueue(self, e):
        self.enqueue_stack.push(e)
        self.size += 1

    def first(self):
        if self.is_empty():
            raise Empty('Queue is empty.')
        if self.dequeue_stack.is_empty():
            return self.enqueue_stack.data[0]
        return self.dequeue_stack.top()

    def dequeue(self):  # dequeue runs in linear time :(
        if self.is_empty():
            raise Empty('Queue is empty.')
        if self.dequeue_stack.is_empty():
            while not self.enqueue_stack.is_empty():
                item = self.enqueue_stack.pop()
                self.dequeue_stack.push(item)
        self.size -= 1
        return self.dequeue_stack.pop()

    def __str__(self):
        s_ls = []
        for i in range(len(self.enqueue_stack) - 1, -1, -1):
            s_ls.append(self.enqueue_stack.data[i])
        for i in range(len(self.dequeue_stack)):
            s_ls.append(self.dequeue_stack.data[i])
        return str(s_ls)

    def __repr__(self):
        return str(self)
Exemplo n.º 2
0
def permutations(lst):
    holder = ArrayStack()
    perms_set = ArrayQueue()
    for elem in lst:
        holder.push(elem)
    for i in range(len(lst)):
        curr = holder.pop()
        if i == 0:
            perms_set.enqueue([curr])
        else:
            for j in range(len(perms_set)):
                sub = perms_set.dequeue()
                for k in range(len(sub) + 1):
                    temp = sub.copy()
                    temp.insert(k, curr)
                    perms_set.enqueue(temp)
    final_lst = []
    while not perms_set.is_empty():
        final_lst.append(perms_set.dequeue())

    return final_lst
Exemplo n.º 3
0
class MaxStack:
    def __init__(self):
        self.data = ArrayStack()
        self.max_val = 0

    def is_empty(self):
        return len(self.data) == 0

    def __len__(self):
        return len(self.data)

    def push(self, e):
        if e > self.max_val:
            self.max_val = e
        tup = (e, self.max_val)
        self.data.push(tup)

    def top(self):
        if self.is_empty():
            raise Empty("MaxStack is empty")
        return self.data.top()[0]

    def pop(self):
        if self.is_empty():
            raise Empty("MaxStack is empty")
        top = self.top()
        self.data.pop()
        return top

    def max(self):
        if self.is_empty():
            raise Empty("MaxStack is empty")
        return self.data.top()[1]
Exemplo n.º 4
0
def balanced_equation(input_st):
    symbol_stack = ArrayStack()
    open_symb = ['{', '(', '[']
    close_symb = {'}': '{', ')': '(', ']': '['}
    for symb in input_st:
        if symb in open_symb:
            symbol_stack.push(symb)
        else:
            if symbol_stack.is_empty():
                return False
            top = symbol_stack.pop()
            if top != close_symb[symb]:
                return False
    return symbol_stack.is_empty()
Exemplo n.º 5
0
def eval_postfix_eq(exp_str):
    exp = exp_str.split()
    arg_stack = ArrayStack()
    for token in exp:
        if token.isdigit():
            arg_stack.push(float(token))
        else:
            arg_2 = arg_stack.pop()
            arg_1 = arg_stack.pop()
            res = None
            if token == '+':
                res = arg_1 + arg_2
            elif token == '-':
                res = arg_1 - arg_2
            elif token == '*':
                res = arg_1 * arg_2
            elif token == '/':
                res = arg_1 / arg_2
            arg_stack.push(res)
    return arg_stack.pop()
Exemplo n.º 6
0
class MidStack:
	def __init__(self):
		self.front_to_mid = ArrayStack()
		self.back_end = ArrayDeque()
		self.size = 0

	def is_empty(self):
		return self.size == 0

	def __len__(self):
		return self.size

	def push(self, e):
		if len(self.back_end) > len(self.front_to_mid):
			switch = self.back_end.delete_first()
			self.front_to_mid.push(switch)
		self.back_end.add_last(e)
		self.size += 1

	def top(self):
		if self.is_empty():
			raise Empty("MidStack is empty")
		if self.back_end.is_empty():
			return self.front_to_mid.top()
		return self.back_end.last()

	def pop(self):
		if self.is_empty():
			raise Empty("MidStack is empty")
		if len(self.back_end) < len(self.front_to_mid):
			switch = self.front_to_mid.pop()
			self.back_end.add_first(switch)
		val = self.back_end.delete_last()
		self.size -= 1
		return val

	def mid_push(self, e):
		if len(self.back_end) > len(self.front_to_mid):
			self.front_to_mid.push(e)
		else:
			self.back_end.add_first(e)
		self.size += 1
Exemplo n.º 7
0
 def __init__(self):
     self.enqueue_stack = ArrayStack()
     self.dequeue_stack = ArrayStack()
     self.size = 0
Exemplo n.º 8
0
def eval_postfix_eq():
    exp_str = input("--> ")
    arg_stack = ArrayStack()
    var_assignments = dict()
    while exp_str != 'done()':
        exp = exp_str.split()
        if len(exp) == 1:
            if exp[0].isdigit():
                print(int(exp[0]))
            else:
                out = var_assignments[exp[0]]
                if isinstance(out, str):
                    out = var_assignments[out]
                if out % 1 == 0:
                    print(int(out))
                else:
                    print(out)
        else:
            for i in range(len(exp)):
                if i == 0 and exp[0].isalpha() and exp[1] == '=':
                    continue
                if exp[i] == '=':
                    continue
                elif exp[i].isdigit():
                    arg_stack.push(float(exp[i]))
                elif exp[i].isalpha():
                    arg_stack.push(exp[i])
                else:
                    arg_2 = arg_stack.pop()
                    arg_1 = arg_stack.pop()
                    if not isinstance(arg_1, float):
                        arg_1 = var_assignments[arg_1]
                    if not isinstance(arg_2, float):
                        arg_2 = var_assignments[arg_2]
                    if exp[i] == '+':
                        res = arg_1 + arg_2
                    elif exp[i] == '-':
                        res = arg_1 - arg_2
                    elif exp[i] == '*':
                        res = arg_1 * arg_2
                    else:
                        res = arg_1 / arg_2
                    arg_stack.push(res)

            if isinstance(exp[0], str) and exp[1] == '=':
                var_assignments[exp[0]] = arg_stack.pop()
                print(exp[0])
            else:
                out = arg_stack.pop()
                if out % 1 == 0:
                    print(int(out))
                else:
                    print(out)

        exp_str = input("--> ")
Exemplo n.º 9
0
 def __init__(self):
     self.data = ArrayStack()
     self.max_val = 0
Exemplo n.º 10
0
	def __init__(self):
		self.front_to_mid = ArrayStack()
		self.back_end = ArrayDeque()
		self.size = 0