Fix(Core): Change append_bot to respect natural computing order

This commit is contained in:
Bertrand Benjamin 2018-11-19 15:44:02 +01:00
parent e37a8a5a2e
commit 4999e80ec3
2 changed files with 197 additions and 13 deletions

View File

@ -608,6 +608,10 @@ def str2(sink, convert_to_mo=True):
>>> t = str2nestedlist(exp)
>>> print(t)
[1, '+', '-', [3, '+', 4]]
>>> exp = "1+3x^2"
>>> t = str2nestedlist(exp)
>>> print(t)
[1, '+', 3, '*', 'x', '^', 2]
>>> from .tree import MutableTree
>>> str2tree = str2(MutableTree.sink)
@ -679,6 +683,41 @@ def str2(sink, convert_to_mo=True):
| > +
| | > 3
| | > 4
>>> exp = "2*4-1"
>>> t = str2tree(exp)
>>> print(t)
+
> *
| > 2
| > 4
> -1
>>> exp = "3+4x"
>>> t = str2tree(exp)
>>> print(t)
+
> 3
> *
| > 4
| > x
>>> exp = "3x^2"
>>> t = str2tree(exp)
>>> print(t)
*
> 3
> ^
| > x
| > 2
>>> exp = "3+4x^5"
>>> t = str2tree(exp)
>>> print(t)
+
> 3
> *
| > 4
| > ^
| | > x
| | > 5
"""
lfop = lookfor(is_operator)

View File

@ -15,7 +15,7 @@ from .tree_tools import (to_nested_parenthesis,
)
from .coroutine import coroutine, STOOOP
from .str2 import str2
from .operator import OPERATORS
from .operator import OPERATORS, is_operator
__all__ = ["Tree", "MutableTree"]
@ -683,17 +683,83 @@ class Tree(object):
| | > +
| | | > 8
| | | > 9
>>> t = Tree.from_str("1+2+3+4+5*6*7*8*9")
>>> t = Tree.from_str("0*1*2*3*4+5+6+7+8+9")
>>> print(t)
+
> +
| > +
| | > +
| | | > +
| | | | > *
| | | | | > *
| | | | | | > *
| | | | | | | > *
| | | | | | | | > 0
| | | | | | | | > 1
| | | | | | | > 2
| | | | | | > 3
| | | | | > 4
| | | | > 5
| | | > 6
| | > 7
| > 8
> 9
>>> bal_t = t.balance()
>>> print(bal_t)
+
> *
| > *
| | > *
| | | > 0
| | | > 1
| | > 2
| > *
| | > 3
| | > 4
> +
| > +
| | > 5
| | > 6
| > +
| | > 7
| | > +
| | | > 8
| | | > 9
>>> t = Tree.from_str("0+1+2+3+4+5*6*7*8*9")
>>> print(t)
+
> +
| > +
| | > +
| | | > +
| | | | > 0
| | | | > 1
| | | > 2
| | > 3
| > 4
> *
| > *
| | > *
| | | > *
| | | | > 5
| | | | > 6
| | | > 7
| | > 8
| > 9
>>> bal_t = t.balance()
>>> print(bal_t)
+
> +
| > +
| | > +
| | | > 0
| | | > 1
| | | > 2
| | > 2
| > +
| | > 3
| > 4
| | > 4
> *
| > *
| | > *
@ -703,14 +769,15 @@ class Tree(object):
| > *
| | > 8
| | > 9
>>> t = Tree.from_str("1+2+3+4+5/6/7/8/9")
>>> bal_t = t.balance(exclude_nodes=['/'])
>>> print(bal_t)
>>> t = Tree.from_str("0+1+2+3+4+5/6/7/8/9")
>>> print(t)
+
> +
| > +
| | > +
| | | > 1
| | | > +
| | | | > 0
| | | | > 1
| | | > 2
| | > 3
| > 4
@ -723,6 +790,28 @@ class Tree(object):
| | | > 7
| | > 8
| > 9
>>> bal_t = t.balance(exclude_nodes=['/'])
>>> print(bal_t)
+
> +
| > +
| | > +
| | | > 0
| | | > 1
| | > 2
| > +
| | > 3
| | > 4
> /
| > /
| | > /
| | | > /
| | | | > 5
| | | | > 6
| | | > 7
| | > 8
| > 9
"""
try:
l_depth = self.left_value.depth()
@ -819,6 +908,26 @@ class MutableTree(Tree):
> +
| > -2
| > 3
>>> t = MutableTree.from_str("1+2*3+4*5")
>>> print(t)
+
> +
| > 1
| > *
| | > 2
| | > 3
> *
| > 4
| > 5
>>> t = MutableTree.from_str("1+2*3*4")
>>> print(t)
+
> 1
> *
| > *
| | > 2
| | > 3
| > 4
"""
str_2_mut_tree = str2(cls.sink, convert_to_mo)
return str_2_mut_tree(expression)
@ -861,6 +970,19 @@ class MutableTree(Tree):
| > None
| > 1
> 2
>>> sink = MutableTree.sink()
>>> for i in ["1", "+", "2", "*", "x", "^", "3"]:
... sink.send(i)
>>> a = sink.throw(STOOOP)
>>> print(a)
+
> 1
> *
| > 2
| > ^
| | > x
| | > 3
"""
ans = cls()
@ -868,7 +990,7 @@ class MutableTree(Tree):
while True:
c = yield
if c is not None:
if c in OPERATORS.keys():
if is_operator(c):
try:
ans.set_node(c)
except ValueError:
@ -1043,14 +1165,37 @@ class MutableTree(Tree):
> +
| > 2
| > None
>>> t.append(3)
>>> print(t)
*
> 1
> +
| > 2
| > 3
>>> t.append_bot("+")
>>> print(t)
*
> 1
> +
| > +
| | > 2
| | > 3
| > None
"""
rv = self.right_value
nright_value = MutableTree()
nright_value.set_node(value)
nright_value.set_left_value(rv)
try:
if value == rv.node:
rv.append_top(value)
else:
rv.append_bot(value)
except AttributeError:
nright_value = MutableTree()
nright_value.set_node(value)
nright_value.set_left_value(rv)
self.right_value = nright_value
self.right_value = nright_value
class LeafTree(Tree):