mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Fix bug and edit doctests
This commit is contained in:
parent
c850227bee
commit
d503bd86fa
@ -14,7 +14,6 @@ Enter an Infix Equation = a + b ^c
|
|||||||
a+b^c (Infix) -> +a^bc (Prefix)
|
a+b^c (Infix) -> +a^bc (Prefix)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def infix_2_postfix(infix):
|
def infix_2_postfix(infix):
|
||||||
"""
|
"""
|
||||||
>>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
|
>>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
|
||||||
@ -28,14 +27,30 @@ def infix_2_postfix(infix):
|
|||||||
| + | abc^
|
| + | abc^
|
||||||
| | abc^+
|
| | abc^+
|
||||||
'abc^+'
|
'abc^+'
|
||||||
>>> infix_2_postfix("1*((-a)*2+b)")
|
|
||||||
Traceback (most recent call last):
|
>>> infix_2_postfix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE
|
||||||
...
|
Symbol | Stack | Postfix
|
||||||
KeyError: '('
|
-------------------------------------------
|
||||||
|
1 | | 1
|
||||||
|
* | * | 1
|
||||||
|
( | *( | 1
|
||||||
|
( | *(( | 1
|
||||||
|
- | *((- | 1
|
||||||
|
a | *((- | 1a
|
||||||
|
) | *( | 1a-
|
||||||
|
* | *(* | 1a-
|
||||||
|
2 | *(* | 1a-2
|
||||||
|
+ | *(+ | 1a-2*
|
||||||
|
b | *(+ | 1a-2*b
|
||||||
|
) | * | 1a-2*b+
|
||||||
|
| | 1a-2*b+*
|
||||||
|
'1a-2*b+*'
|
||||||
|
|
||||||
>>> infix_2_postfix("")
|
>>> infix_2_postfix("")
|
||||||
Symbol | Stack | Postfix
|
Symbol | Stack | Postfix
|
||||||
----------------------------
|
----------------------------
|
||||||
''
|
''
|
||||||
|
|
||||||
>>> infix_2_postfix("(()") # doctest: +NORMALIZE_WHITESPACE
|
>>> infix_2_postfix("(()") # doctest: +NORMALIZE_WHITESPACE
|
||||||
Symbol | Stack | Postfix
|
Symbol | Stack | Postfix
|
||||||
----------------------------
|
----------------------------
|
||||||
@ -44,6 +59,7 @@ def infix_2_postfix(infix):
|
|||||||
) | ( |
|
) | ( |
|
||||||
| | (
|
| | (
|
||||||
'('
|
'('
|
||||||
|
|
||||||
>>> infix_2_postfix("())")
|
>>> infix_2_postfix("())")
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
@ -83,7 +99,11 @@ def infix_2_postfix(infix):
|
|||||||
if len(stack) == 0:
|
if len(stack) == 0:
|
||||||
stack.append(x) # If stack is empty, push x to stack
|
stack.append(x) # If stack is empty, push x to stack
|
||||||
else: # while priority of x is not > priority of element in the stack
|
else: # while priority of x is not > priority of element in the stack
|
||||||
while len(stack) > 0 and priority[x] <= priority[stack[-1]]:
|
while (
|
||||||
|
len(stack) > 0
|
||||||
|
and stack[-1] != "("
|
||||||
|
and priority[x] <= priority[stack[-1]]
|
||||||
|
):
|
||||||
post_fix.append(stack.pop()) # pop stack & add to Postfix
|
post_fix.append(stack.pop()) # pop stack & add to Postfix
|
||||||
stack.append(x) # push x to stack
|
stack.append(x) # push x to stack
|
||||||
|
|
||||||
@ -119,10 +139,23 @@ def infix_2_prefix(infix):
|
|||||||
| | cb^a+
|
| | cb^a+
|
||||||
'+a^bc'
|
'+a^bc'
|
||||||
|
|
||||||
>>> infix_2_prefix("1*((-a)*2+b)")
|
>>> infix_2_prefix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE
|
||||||
Traceback (most recent call last):
|
Symbol | Stack | Postfix
|
||||||
...
|
-------------------------------------------
|
||||||
KeyError: '('
|
( | ( |
|
||||||
|
b | ( | b
|
||||||
|
+ | (+ | b
|
||||||
|
2 | (+ | b2
|
||||||
|
* | (+* | b2
|
||||||
|
( | (+*( | b2
|
||||||
|
a | (+*( | b2a
|
||||||
|
- | (+*(- | b2a
|
||||||
|
) | (+* | b2a-
|
||||||
|
) | | b2a-*+
|
||||||
|
* | * | b2a-*+
|
||||||
|
1 | * | b2a-*+1
|
||||||
|
| | b2a-*+1*
|
||||||
|
'*1+*-a2b'
|
||||||
|
|
||||||
>>> infix_2_prefix('')
|
>>> infix_2_prefix('')
|
||||||
Symbol | Stack | Postfix
|
Symbol | Stack | Postfix
|
||||||
|
Loading…
Reference in New Issue
Block a user