The digits of num in the given base as ints, most significant first.
With reverse=True the least significant digit comes first, so digit i
is the coefficient of base**i. A negative int loses its sign. A str is
read one character at a time, so leading zeros are kept; base=2 gives
the bits.
Source code in pydsa/digits.mu
6 7 8 9101112131415161718192021
defto_digits(num:int|str,reverse:bool=false,base:int=10)->[int]"""The digits of num in the given base as ints, most significant first. With reverse=True the least significant digit comes first, so digit i is the coefficient of base**i. A negative int loses its sign. A str is read one character at a time, so leading zeros are kept; base=2 gives the bits. """ifisinstance(num,str)ds=[int(c,base)forcinnum.lstrip("-")]returnreverse?ds[::-1]:dsnum,ds=absnum,[]whilenumornotdsnum,d=divmod(num,base)ds<-dreverse?ds:ds[::-1]
8 91011121314151617181920212223
defto_digits(num:int|str,reverse:bool=False,base:int=10)->list[int]:"""The digits of num in the given base as ints, most significant first. With reverse=True the least significant digit comes first, so digit i is the coefficient of base**i. A negative int loses its sign. A str is read one character at a time, so leading zeros are kept; base=2 gives the bits. """ifisinstance(num,str):ds=[int(c,base)forcinnum.lstrip("-")]returnds[::-1]ifreverseelsedsnum,ds=abs(num),[]whilenumornotds:num,d=divmod(num,base)ds.append(d)returndsifreverseelseds[::-1]
The count of digits of num in the given base; 0 has one digit.
A negative int loses its sign. It does not convert num to a str, so it
is fast on big ints and has no int max str digits limit.
Source code in pydsa/digits.mu
232425262728293031323334
defnum_digits(num:int,base:int=10)->int"""The count of digits of num in the given base; 0 has one digit. A negative int loses its sign. It does not convert num to a str, so it is fast on big ints and has no int max str digits limit. """num=absnumretd=max(1,int(num.bit_length()/log2(base)))whiled>1andbase**(d-1)>numd-=1whilebase**d<=numd+=1
26272829303132333435363738
defnum_digits(num:int,base:int=10)->int:"""The count of digits of num in the given base; 0 has one digit. A negative int loses its sign. It does not convert num to a str, so it is fast on big ints and has no int max str digits limit. """num=abs(num)d=max(1,int(num.bit_length()/log2(base)))whiled>1andbase**(d-1)>num:d-=1whilebase**d<=num:d+=1returnd
The int whose digits in the given base are the list, most significant first.
With reverse=True the list is read least significant first, the inverse
of to_digits(num, reverse=True). An empty list is 0. base=2 reads bits.
Source code in pydsa/digits.mu
363738394041424344
defto_int(digits:[int],reverse:bool=false,base:int=10)->int"""The int whose digits in the given base are the list, most significant first. With reverse=True the list is read least significant first, the inverse of to_digits(num, reverse=True). An empty list is 0. base=2 reads bits. """retout=0fordinreverse?digits[::-1]:digitsout=out*base+d
41424344454647484950
defto_int(digits:list[int],reverse:bool=False,base:int=10)->int:"""The int whose digits in the given base are the list, most significant first. With reverse=True the list is read least significant first, the inverse of to_digits(num, reverse=True). An empty list is 0. base=2 reads bits. """out=0fordindigits[::-1]ifreverseelsedigits:out=out*base+dreturnout