Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Annotated Answers for "Beginning Zeros" solution in Clear category for Beginning Zeros by BootzenKatzen
# I finally did a single line answer!! And the provided solution was multiple lines!! I WIN!
# I was super proud of this initially, but it's very clunky looking back on it
def beginning_zeros(a: str) -> int:
return (len(a) - (len(str(int(a)))) if int(a) else len(a))
# I realize now, that this one isn't super easy to follow or particularly elegant, but it still works
# I forgot that there was a way to strip characters from the beginning of a string
# so I used int(a) to strip the 0's instead
# but then you can't get the length of an integer so I had to turn it back into a string
# so I'm subtracting the length of the stripped number from the length original number
# So if the original was 004 then the original length is 3 and the str(int(a)) length is 1, giving us 2 zeroes
# but I needed a way to catch if there were all 0s
# so if it's all 0's then int(a) is 0 which python also interprets as false
# so if it comes up as false, then it returns just len(a) so we have the number of 0s.
# Here's their solution:
def beginning_zeros2(a: str) -> int:
a_num = int(a) # storing the int version of our number, which strips the starting 0s
if not a_num: # if it is all 0s a_num will be '0' which python also sees as false
# so if it's all 0s
return len(a) # just return the length of the whole thing
# otherwise
return len(a) - len(str(a_num)) # we turn the int back into a string so we can pull the length
# and we subtract the second length from the first
# to get the number of leading 0s
# Bonus solution 1:
def beginning_zeros3(number: str) -> int:
return len(number) - len(number.lstrip('0'))
# personally, I think this is their best solution
# This one does the same math, just a little differently
# lstrip() removes the leading characters of a string based on the argument in the ()
# in this case, it's removing all the beginning 0s because we passed in '0'
# so in the case we have all 0s our string will end up being ''
# so it subtracts the length of the stripped string from the original length to get the leading 0s.
#Bonus solution 2:
def beginning_zeros4(number: str) -> int:
result = 0 # Establishing a counter
for n in number: # iterating through the string of numbers
if n != "0": # if the number is not a 0
break # stop here and go to the next part of the code (return result) skips result +=1
result += 1 # if it is 0 it won't break, and we add 1 to our result
return result # when it breaks or reaches the end of the number, return the result
print("Example:")
print(beginning_zeros("10"))
# These "asserts" are used for self-checking
assert beginning_zeros("100") == 0
assert beginning_zeros("001") == 2
assert beginning_zeros("100100") == 0
assert beginning_zeros("001001") == 2
assert beginning_zeros("012345679") == 1
assert beginning_zeros("0000") == 4
print("The mission is done! Click 'Check Solution' to earn rewards!")
May 15, 2023
Comments: