note: added the 11 and 1.1 test cases because the subtracting-from-both-ends solution can have edge cases around there
defsigfig(num):# ignore 0's on the far left
ldx=0whilenum[ldx]=='0'andldx<len(num):ldx+=1# ignore 0's on the far right if no decimal place
rdx=len(num)-1ifnum.find('.')>0:return(rdx-ldx)# the +1 from below is cancelled out by needing to subtract the '.'
else:whilenum[rdx]=='0'andrdx>-1:rdx-=1return(rdx-ldx)+1print(sigfig('11'),2)print(sigfig('1.1'),2)print(sigfig('1'),1)print(sigfig('003'),1)print(sigfig('3000'),1)print(sigfig('404'),3)print(sigfig('050030210'),7)print(sigfig('0.1'),1)print(sigfig('0.0'),1)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
in python
note: added the 11 and 1.1 test cases because the subtracting-from-both-ends solution can have edge cases around there