Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/python/bench/src/parse_int.py
1067 views
1
from bench import register, all
2
3
4
def parse_int(S: str = '1' * 5*10**5, B: int = 10) -> int:
5
"""
6
Parse string S as an integer in base B. This is from
7
8
https://discuss.python.org/t/int-str-conversions-broken-in-latest-python-bugfix-releases/18889/14?u=williamstein
9
10
which is a discussion that illustrates how some security researchers and core
11
Python developers view computational mathematics...
12
13
AUTHOR: Oscar Benjamin
14
15
NOTE: with pylang this gives the answer as Javascript float, so take that benchmark with a grain of salt.
16
"""
17
m = len(S)
18
l = list(map(int, S[::-1]))
19
b, k = B, m
20
while k > 1:
21
last = [l[-1]] if k % 2 == 1 else []
22
l = [l1 + b * l2 for l1, l2 in zip(l[::2], l[1::2])]
23
l.extend(last)
24
b, k = b**2, (k + 1) // 2
25
[l0] = l
26
return l0
27
28
register("parse_int", parse_int)
29
30
if __name__ == '__main__':
31
all()
32