Хайруллина Алина AsyncPoly 11001
action22k AsyncPoly 10448
f1import unicodedataf1import unicodedata
2import asyncio2import asyncio
33
4class YesFuture:4class YesFuture:
55
6    def __init__(self, value=None):6    def __init__(self, value=None):
7        self._value = value7        self._value = value
88
9    def set(self, value):9    def set(self, value):
10        self._value = value10        self._value = value
1111
12    def __await__(self):12    def __await__(self):
1313
14        async def _inner():14        async def _inner():
15            return self._value15            return self._value
16        return _inner().__await__()16        return _inner().__await__()
1717
n18async def Sum(xy):n18async def Sum(ab):
19    return await x + await y19    return await a + await b
2020
n21async def Mul(xy):n21async def Mul(ab):
22    return await x * await y22    return await a * await b
2323
n24async def Pow(xy):n24async def Pow(ab):
25    return await x ** await y25    return await a ** await b
2626
n27def _parse_superscript(exp):n27def _parse_superscript(exp_str):
28    """exp_str: строка из символов-суперцифр '⁰¹²³⁴⁵⁶⁷⁸⁹'."""
28    m = 029    n = 0
29    for c in exp:30    for ch in exp_str:
30        m = m * 10 + unicodedata.digit(c)31        n = n * 10 + unicodedata.digit(ch)
31    return m32    return n
3233
n33def parse_poly(stryesFuture):n34def parse_poly(polyx_future):
35    """
36    poly: строка с многочленом (например, '3x⁵ + x² - 6x + 4')
37    x_future: объект YesFuture
38    Возвращает корутину, вычисляющую значение многочлена при текущем x_f
 >uture.
39    """
34    text = str.replace(' ', '')40    s = poly.replace(' ', '')
35    pos = 041    i = 0
36    items = []42    terms = []
37    polar = []43    signs = []
3844
n39    def read_num(src, idx):n45    def parse_int(s, i):
40        beg = idx46        """Возвращает (число или None, новая_позиция)."""
47        start = i
41        while idx < len(src) and src[idx].isdigit():48        while i < len(s) and s[i].isdigit():
42            idx += 149            i += 1
43        if beg == idx:50        if start == i:
44            return (None, idx)51            return (None, i)
45        return (int(src[beg:idx]), idx)52        return (int(s[start:i]), i)
46    while pos < len(text):53    while i < len(s):
47        flag = 154        sign = 1
48        if text[pos] == '+':55        if s[i] == '+':
49            pos += 156            i += 1
50        elif text[pos] == '-':57        elif s[i] == '-':
51            flag = -158            sign = -1
52            pos += 159            i += 1
53        coef, nxt = read_num(text, pos)60        coeff, i_new = parse_int(s, i)
54        if coef is None:61        if coeff is None:
55            coef = 162            coeff = 1
56        pos = nxt63        i = i_new
57        mark_x = False64        has_x = False
58        if pos < len(text) and text[pos] == 'x':65        if i < len(s) and s[i] == 'x':
59            mark_x = True66            has_x = True
60            pos += 167            i += 1
61        degree = 1 if mark_x else 068        power = 1 if has_x else 0
62        if mark_x and pos < len(text):69        if has_x and i < len(s):
63            beg = pos70            start = i
64            while pos < len(text):71            while i < len(s):
65                char = text[pos]72                ch = s[i]
66                try:73                try:
n67                    unicodedata.digit(char)n74                    unicodedata.digit(ch)
68                    pos += 175                    i += 1
69                except (TypeError, ValueError):76                except (TypeError, ValueError):
70                    break77                    break
n71            if pos > beg:n78            if i > start:
72                degree = _parse_superscript(text[beg:pos])79                power = _parse_superscript(s[start:i])
7380
n74        async def fixed(val):n81        async def const_term(val):
75            return val82            return val
n76        if not mark_x:n83        if not has_x:
77            term_obj = fixed(coef)84            term = const_term(coeff)
78        else:85        else:
7986
n80            async def raise_pow(x_vale_val):n87            async def make_pow(xfp):
8188
n82                async def e_future():n89                async def p_future():
83                    return e_val90                    return p
84                return await Pow(x_vale_future())91                return await Pow(xfp_future())
85            exponent_part = raise_pow(yesFuture, degree)92            pow_part = make_pow(x_future, power)
86            if coef == 1:93            if coeff == 1:
87                term_obj = exponent_part94                term = pow_part
88            else:95            else:
8996
n90                async def scale(v, inner):n97                async def make_coeff_mul(c, inner):
9198
n92                    async def v_future():n99                    async def c_future():
93                        return v100                        return c
94                    return await Mul(v_future(), inner)101                    return await Mul(c_future(), inner)
95                term_obj = scale(coef, exponent_part)102                term = make_coeff_mul(coeff, pow_part)
96        items.append(term_obj)103        terms.append(term)
97        polar.append(flag)104        signs.append(sign)
98105
n99    async def compute_poly():n106    async def eval_poly():
100107
n101        async def zero_val():n108        async def const_zero():
102            return 0109            return 0
n103        accum = zero_val()n110        result = const_zero()
104        for sg, piece in zip(polaritems):111        for signterm in zip(signs, terms):
105            if sg == 1:112            if sign == 1:
106                accum = Sum(accum, piece)113                result = Sum(result, term)
107            else:114            else:
108115
n109                async def neg_one():n116                async def minus_one():
110                    return -1117                    return -1
t111                inv = Mul(neg_one(), piece)t118                neg_term = Mul(minus_one(), term)
112                accum = Sum(accum, inv)119                result = Sum(result, neg_term)
113        return await accum120        return await result
114    return compute_poly()121    return eval_poly()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op