Code: Select all
'0123456789'
Code: Select all
from typing import List
DIGITS = {str(i): i for i in range(10)}
def get_bytes(s: str) -> List[int]:
byte_list = []
chunk = 0
for c in f'{s}0':
if (d := DIGITS.get(c)) is None:
raise ValueError(f"{c} is not a digit")
if (new := chunk * 10 + d) < 256:
chunk = new
else:
byte_list.append(chunk)
chunk = d
return byte_list[not byte_list[0]:]
< /code>
In [143]: get_bytes('0000')
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[143], line 1
----> 1 get_bytes('0000')
Cell In[142], line 14, in get_bytes(s)
11 byte_list.append(chunk)
12 chunk = d
---> 14 return byte_list[not byte_list[0]:]
IndexError: list index out of range
In [144]: get_bytes('100100100')
Out[144]: [100, 100, 100]
In [145]: get_bytes('12301230123')
Out[145]: [123, 123, 123]
< /code>
I tried to fix the code, I couldn't. Now there is only one problem remaining, and the exact problem I tried to address with this question.
In the first example, expected output is [0, 0, 0, 0]
Nach meinen Tests ist der Code aus der akzeptierten Antwort besser als der auf Regex basierende Ansatz, sodass ich: < /p>
besser als der auf Regex basierende Ansatz ausgeführt habe: < /p>
Code: Select all
In [384]: get_bytes1(string) == get_bytes2(string)
Out[384]: True
In [385]: %timeit get_bytes1(string)
179 ms ± 588 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [386]: %timeit get_bytes2(string)
161 ms ± 748 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [387]: len(string)
Out[387]: 1000001
< /code>
get_bytes2