action22k AsyncPoly 10448
Хайруллина Алина AsyncPoly 11001
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(ab):n18async def Sum(xy):
19    return await a + await b19    return await x + await y
2020
n21async def Mul(ab):n21async def Mul(xy):
22    return await a * await b22    return await x * await y
2323
n24async def Pow(ab):n24async def Pow(xy):
25    return await a ** await b25    return await x ** await y
2626
n27def _parse_superscript(exp_str):n27def _parse_superscript(exp):
28    """exp_str: строка из символов-суперцифр '⁰¹²³⁴⁵⁶⁷⁸⁹'."""
29    n = 028    m = 0
30    for ch in exp_str:29    for c in exp:
31        n = n * 10 + unicodedata.digit(ch)30        m = m * 10 + unicodedata.digit(c)
32    return n31    return m
3332
n34def parse_poly(poly, x_future):n33def parse_poly(str, yesFuture):
35    """34    text = str.replace(' ', '')
36    poly: строка с многочленом (например, '3x⁵ + x² - 6x + 4')
37    x_future: объект YesFuture
38    Возвращает корутину, вычисляющую значение многочлена при текущем x_f
>uture. 
39    """
40    s = poly.replace(' ', '')
41    i = 035    pos = 0
42    terms = []36    items = []
43    signs = []37    polar = []
4438
n45    def parse_int(s, i):n39    def read_num(src, idx):
46        """Возвращает (число или None, новая_позиция)."""40        beg = idx
47        start = i
48        while i < len(s) and s[i].isdigit():41        while idx < len(src) and src[idx].isdigit():
49            i += 142            idx += 1
50        if start == i:43        if beg == idx:
51            return (None, i)44            return (None, idx)
52        return (int(s[start:i]), i)45        return (int(src[beg:idx]), idx)
53    while i < len(s):46    while pos < len(text):
54        sign = 147        flag = 1
55        if s[i] == '+':48        if text[pos] == '+':
56            i += 149            pos += 1
57        elif s[i] == '-':50        elif text[pos] == '-':
58            sign = -151            flag = -1
59            i += 152            pos += 1
60        coeff, i_new = parse_int(s, i)53        coef, nxt = read_num(text, pos)
61        if coeff is None:54        if coef is None:
62            coeff = 155            coef = 1
63        i = i_new56        pos = nxt
64        has_x = False57        mark_x = False
65        if i < len(s) and s[i] == 'x':58        if pos < len(text) and text[pos] == 'x':
66            has_x = True59            mark_x = True
67            i += 160            pos += 1
68        power = 1 if has_x else 061        degree = 1 if mark_x else 0
69        if has_x and i < len(s):62        if mark_x and pos < len(text):
70            start = i63            beg = pos
71            while i < len(s):64            while pos < len(text):
72                ch = s[i]65                char = text[pos]
73                try:66                try:
n74                    unicodedata.digit(ch)n67                    unicodedata.digit(char)
75                    i += 168                    pos += 1
76                except (TypeError, ValueError):69                except (TypeError, ValueError):
77                    break70                    break
n78            if i > start:n71            if pos > beg:
79                power = _parse_superscript(s[start:i])72                degree = _parse_superscript(text[beg:pos])
8073
n81        async def const_term(val):n74        async def fixed(val):
82            return val75            return val
n83        if not has_x:n76        if not mark_x:
84            term = const_term(coeff)77            term_obj = fixed(coef)
85        else:78        else:
8679
n87            async def make_pow(xfp):n80            async def raise_pow(x_vale_val):
8881
n89                async def p_future():n82                async def e_future():
90                    return p83                    return e_val
91                return await Pow(xfp_future())84                return await Pow(x_vale_future())
92            pow_part = make_pow(x_future, power)85            exponent_part = raise_pow(yesFuture, degree)
93            if coeff == 1:86            if coef == 1:
94                term = pow_part87                term_obj = exponent_part
95            else:88            else:
9689
n97                async def make_coeff_mul(c, inner):n90                async def scale(v, inner):
9891
n99                    async def c_future():n92                    async def v_future():
100                        return c93                        return v
101                    return await Mul(c_future(), inner)94                    return await Mul(v_future(), inner)
102                term = make_coeff_mul(coeff, pow_part)95                term_obj = scale(coef, exponent_part)
103        terms.append(term)96        items.append(term_obj)
104        signs.append(sign)97        polar.append(flag)
10598
n106    async def eval_poly():n99    async def compute_poly():
107100
n108        async def const_zero():n101        async def zero_val():
109            return 0102            return 0
n110        result = const_zero()n103        accum = zero_val()
111        for sign, term in zip(signs, terms):104        for sg, piece in zip(polar, items):
112            if sign == 1:105            if sg == 1:
113                result = Sum(result, term)106                accum = Sum(accum, piece)
114            else:107            else:
115108
n116                async def minus_one():n109                async def neg_one():
117                    return -1110                    return -1
t118                neg_term = Mul(minus_one(), term)t111                inv = Mul(neg_one(), piece)
119                result = Sum(result, neg_term)112                accum = Sum(accum, inv)
120        return await result113        return await accum
121    return eval_poly()114    return compute_poly()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op