Count-Trailing-Zeros(x) ----- Can I use Log(x) to see if X is equal to ( Integer * 10 ^ k) ???

2 views
Skip to first unread message

henh...@gmail.com

unread,
Sep 18, 2022, 11:57:36 AMSep 18
to

--------- (in Python) is there a simple string function that counts the leftmost 0's ???



Count how many zeros are at the end of your int:

def end_zeros(num):
s = str(num)
return len(s) - len(s.rstrip("0"))


print(end_zeros(10)) # == 1
print(end_zeros(101)) # == 0
print(end_zeros(245)) # == 0
print(end_zeros(100100)) # == 2


---------------- Writing a loop (using % and // ) feels like [reinventing the wheel] and seems intrinsically wrong. --- (it must be unPythonic)


i like this code (above) because it's not reinventing the wheel --- the Python implementors have already done the loop (using % and // ), so i'm just using that efficient tool (routine).
Reply all
Reply to author
Forward
0 new messages