コード例 #1
0
ファイル: postfix.py プロジェクト: xianyu2006/exercises
def infix2prefix(s: str):
    _priority = {
        '*': 3,
        '/': 3,
        '+': 2,
        '-': 2,
        '(': 1,
    }
    _ret = []
    _opstack = Stack()
    _srclst = s.split()
    _refer = string.ascii_uppercase + string.digits
    _index = 0
    while _index < len(_srclst):
        _item = _srclst[_index]
        if _item == '(':
            _opstack.push(_item)
        elif _item in _refer:
            _ret.append(_item)
        elif _item == ')':
            _top = _opstack.pop()
            # maybe exist multi operations rather than one.
            # so need to add continually until meet '('
            while _top != '(':
                _ret.append(_top)
                _top = _opstack.pop()
        else:
            while (not _opstack.is_empty()
                   ) and _priority[_opstack.peek()] >= _priority[_item]:
                _ret.append(_opstack.pop())
            _opstack.push(_item)
        _index += 1

    while not _opstack.is_empty():
        _ret.append(_opstack.pop())

    return ''.join(_ret)
コード例 #2
0
ファイル: app.py プロジェクト: zhanggh8023/exercises
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 6/14/18 11:17 AM
# @Author  : Miracle Young
# @File    : app.py

from python_data_structure.base_data_structure.stack import Stack

_stack = Stack()

print(_stack.is_empty())
_stack.push(4)
_stack.push('dog')
print(_stack.peek())
_stack.push(True)
print(_stack.size())
print(_stack.is_empty())
_stack.push(8.4)
print(_stack.pop())
print(_stack.pop())
print(_stack.size())

from python_data_structure.base_data_structure.queue import Queue

_q = Queue()
print(_q.is_empty())
print(_q.enqueue(1))
print(_q.enqueue(10))
print(_q.dequeue())
print(_q.size())