| f | import unicodedata | f | import unicodedata |
| import asyncio | | import asyncio |
| | | |
| class YesFuture: | | class YesFuture: |
| | | |
| def __init__(self, value=None): | | def __init__(self, value=None): |
| n | self._val = value | n | self._value = value |
| | | |
| n | def set(self, val): | n | def set(self, value): |
| self._val = val | | self._value = value |
| | | |
| def __await__(self): | | def __await__(self): |
| | | |
| n | async def inner(): | n | async def _inner(): |
| return self._val | | return self._value |
| return inner().__await__() | | return _inner().__await__() |
| | | |
| n | async def Sum(a, b): | n | async def Sum(x, y): |
| return await a + await b | | return await x + await y |
| | | |
| n | async def Mul(a, b): | n | async def Mul(x, y): |
| return await a * await b | | return await x * await y |
| | | |
| n | async def Pow(a, b): | n | async def Pow(x, y): |
| return await a ** await b | | return await x ** await y |
| | | |
| n | def parse_superscript_number(sup_str): | n | def _parse_superscript(exp): |
| num = 0 | | m = 0 |
| for ch in sup_str: | | for c in exp: |
| num = num * 10 + unicodedata.digit(ch) | | m = m * 10 + unicodedata.digit(c) |
| return num | | return m |
| | | |
| n | def parse_poly(poly_str, x_future): | n | def parse_poly(str, yesFuture): |
| cleaned = poly_str.replace(' ', '') | | text = str.replace(' ', '') |
| idx = 0 | | pos = 0 |
| terms = [] | | items = [] |
| signs = [] | | polar = [] |
| | | |
| n | def parse_number(s, pos): | n | def read_num(src, idx): |
| start = pos | | beg = idx |
| while pos < len(s) and s[pos].isdigit(): | | while idx < len(src) and src[idx].isdigit(): |
| | | idx += 1 |
| | | if beg == idx: |
| | | return (None, idx) |
| | | return (int(src[beg:idx]), idx) |
| | | while pos < len(text): |
| | | flag = 1 |
| | | if text[pos] == '+': |
| pos += 1 | | pos += 1 |
| n | if start == pos: | n | |
| return (None, pos) | | |
| return (int(s[start:pos]), pos) | | |
| while idx < len(cleaned): | | |
| sign = 1 | | |
| if cleaned[idx] == '+': | | |
| idx += 1 | | |
| elif cleaned[idx] == '-': | | elif text[pos] == '-': |
| sign = -1 | | flag = -1 |
| idx += 1 | | pos += 1 |
| coeff, new_idx = parse_number(cleaned, idx) | | coef, nxt = read_num(text, pos) |
| if coeff is None: | | if coef is None: |
| coeff = 1 | | coef = 1 |
| idx = new_idx | | pos = nxt |
| x_present = False | | mark_x = False |
| if idx < len(cleaned) and cleaned[idx] == 'x': | | if pos < len(text) and text[pos] == 'x': |
| x_present = True | | mark_x = True |
| idx += 1 | | pos += 1 |
| power = 1 if x_present else 0 | | degree = 1 if mark_x else 0 |
| if x_present and idx < len(cleaned): | | if mark_x and pos < len(text): |
| start_pow = idx | | beg = pos |
| while idx < len(cleaned): | | while pos < len(text): |
| | | char = text[pos] |
| try: | | try: |
| n | unicodedata.digit(cleaned[idx]) | n | unicodedata.digit(char) |
| idx += 1 | | pos += 1 |
| except (TypeError, ValueError): | | except (TypeError, ValueError): |
| break | | break |
| n | if idx > start_pow: | n | if pos > beg: |
| power = parse_superscript_number(cleaned[start_pow:idx]) | | degree = _parse_superscript(text[beg:pos]) |
| | | |
| n | async def const_val(v): | n | async def fixed(val): |
| return v | | return val |
| if not x_present: | | if not mark_x: |
| term_awaitable = const_val(coeff) | | term_obj = fixed(coef) |
| else: | | else: |
| | | |
| n | async def power_term(xf, p): | n | async def raise_pow(x_val, e_val): |
| | | |
| n | async def p_fut(): | n | async def e_future(): |
| return p | | return e_val |
| return await Pow(xf, p_fut()) | | return await Pow(x_val, e_future()) |
| pow_await = power_term(x_future, power) | | exponent_part = raise_pow(yesFuture, degree) |
| if coeff == 1: | | if coef == 1: |
| term_awaitable = pow_await | | term_obj = exponent_part |
| else: | | else: |
| | | |
| n | async def coeff_mul(c, inner): | n | async def scale(v, inner): |
| | | |
| n | async def c_fut(): | n | async def v_future(): |
| return c | | return v |
| return await Mul(c_fut(), inner) | | return await Mul(v_future(), inner) |
| term_awaitable = coeff_mul(coeff, pow_await) | | term_obj = scale(coef, exponent_part) |
| terms.append(term_awaitable) | | items.append(term_obj) |
| signs.append(sign) | | polar.append(flag) |
| | | |
| n | async def compute(): | n | async def compute_poly(): |
| | | |
| async def zero_val(): | | async def zero_val(): |
| return 0 | | return 0 |
| n | acc = zero_val() | n | accum = zero_val() |
| for sgn, term in zip(signs, terms): | | for sg, piece in zip(polar, items): |
| if sgn == 1: | | if sg == 1: |
| acc = Sum(acc, term) | | accum = Sum(accum, piece) |
| else: | | else: |
| | | |
| async def neg_one(): | | async def neg_one(): |
| return -1 | | return -1 |
| t | neg_term = Mul(neg_one(), term) | t | inv = Mul(neg_one(), piece) |
| acc = Sum(acc, neg_term) | | accum = Sum(accum, inv) |
| return await acc | | return await accum |
| return compute() | | return compute_poly() |