Долгих Данила AsyncPoly 10624
Хайруллина Алина 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):
n7        self._val = valuen7        self._value = value
88
n9    def set(self, val):n9    def set(self, value):
10        self._val = val10        self._value = value
1111
12    def __await__(self):12    def __await__(self):
1313
n14        async def inner():n14        async def _inner():
15            return self._val15            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_number(sup_str):n27def _parse_superscript(exp):
28    num = 028    m = 0
29    for ch in sup_str:29    for c in exp:
30        num = num * 10 + unicodedata.digit(ch)30        m = m * 10 + unicodedata.digit(c)
31    return num31    return m
3232
n33def parse_poly(poly_str, x_future):n33def parse_poly(str, yesFuture):
34    cleaned = poly_str.replace(' ', '')34    text = str.replace(' ', '')
35    idx = 035    pos = 0
36    terms = []36    items = []
37    signs = []37    polar = []
3838
n39    def parse_number(s, pos):n39    def read_num(src, idx):
40        start = pos40        beg = idx
41        while pos < len(s) and s[pos].isdigit():41        while idx < len(src) and src[idx].isdigit():
42            idx += 1
43        if beg == idx:
44            return (None, idx)
45        return (int(src[beg:idx]), idx)
46    while pos < len(text):
47        flag = 1
48        if text[pos] == '+':
42            pos += 149            pos += 1
n43        if start == pos:n
44            return (None, pos)
45        return (int(s[start:pos]), pos)
46    while idx < len(cleaned):
47        sign = 1
48        if cleaned[idx] == '+':
49            idx += 1
50        elif cleaned[idx] == '-':50        elif text[pos] == '-':
51            sign = -151            flag = -1
52            idx += 152            pos += 1
53        coeff, new_idx = parse_number(cleaned, idx)53        coef, nxt = read_num(text, pos)
54        if coeff is None:54        if coef is None:
55            coeff = 155            coef = 1
56        idx = new_idx56        pos = nxt
57        x_present = False57        mark_x = False
58        if idx < len(cleaned) and cleaned[idx] == 'x':58        if pos < len(text) and text[pos] == 'x':
59            x_present = True59            mark_x = True
60            idx += 160            pos += 1
61        power = 1 if x_present else 061        degree = 1 if mark_x else 0
62        if x_present and idx < len(cleaned):62        if mark_x and pos < len(text):
63            start_pow = idx63            beg = pos
64            while idx < len(cleaned):64            while pos < len(text):
65                char = text[pos]
65                try:66                try:
n66                    unicodedata.digit(cleaned[idx])n67                    unicodedata.digit(char)
67                    idx += 168                    pos += 1
68                except (TypeError, ValueError):69                except (TypeError, ValueError):
69                    break70                    break
n70            if idx > start_pow:n71            if pos > beg:
71                power = parse_superscript_number(cleaned[start_pow:idx])72                degree = _parse_superscript(text[beg:pos])
7273
n73        async def const_val(v):n74        async def fixed(val):
74            return v75            return val
75        if not x_present:76        if not mark_x:
76            term_awaitable = const_val(coeff)77            term_obj = fixed(coef)
77        else:78        else:
7879
n79            async def power_term(xf, p):n80            async def raise_pow(x_val, e_val):
8081
n81                async def p_fut():n82                async def e_future():
82                    return p83                    return e_val
83                return await Pow(xfp_fut())84                return await Pow(x_vale_future())
84            pow_await = power_term(x_future, power)85            exponent_part = raise_pow(yesFuture, degree)
85            if coeff == 1:86            if coef == 1:
86                term_awaitable = pow_await87                term_obj = exponent_part
87            else:88            else:
8889
n89                async def coeff_mul(c, inner):n90                async def scale(v, inner):
9091
n91                    async def c_fut():n92                    async def v_future():
92                        return c93                        return v
93                    return await Mul(c_fut(), inner)94                    return await Mul(v_future(), inner)
94                term_awaitable = coeff_mul(coeff, pow_await)95                term_obj = scale(coef, exponent_part)
95        terms.append(term_awaitable)96        items.append(term_obj)
96        signs.append(sign)97        polar.append(flag)
9798
n98    async def compute():n99    async def compute_poly():
99100
100        async def zero_val():101        async def zero_val():
101            return 0102            return 0
n102        acc = zero_val()n103        accum = zero_val()
103        for sgn, term in zip(signs, terms):104        for sg, piece in zip(polar, items):
104            if sgn == 1:105            if sg == 1:
105                acc = Sum(acc, term)106                accum = Sum(accumpiece)
106            else:107            else:
107108
108                async def neg_one():109                async def neg_one():
109                    return -1110                    return -1
t110                neg_term = Mul(neg_one(), term)t111                inv = Mul(neg_one(), piece)
111                acc = Sum(acc, neg_term)112                accum = Sum(accuminv)
112        return await acc113        return await accum
113    return compute()114    return compute_poly()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op