Dick Moores wrote:
def sigDigits(n):
"""
Strips any real decimal (as string) to just its significant digits,
then returns its length, the number of significant digits.
Examples: "-345" -> "345" -> 3;
"3.000" -> "3000" -> 4
"0.0001234" -> "1234" -> 4;
"10.0001234" -> "100001234" -> 9
"7.2345e+543" -> "72345" -> 5
"""
s = str(n).lstrip("-+0")
s = s.lstrip(".")
s = s.lstrip("0")
s = s.lstrip(".")
Why the repeated strips? Couldn't this all just be done with lstrip('-+0.') ?
Yes. Thanks, Kent.
def sigDigits(n):
"""
Strips any real decimal (as string) to just its significant digits,
then returns its length, the number of significant digits.
Examples: "-345" -> "345" -> 3;
"3.000" -> "3000" -> 4
"0.0001234" -> "1234" -> 4;
"10.0001234" -> "100001234" -> 9
"7.2345e+543" -> "72345" -> 5
"""
s = n.lstrip('-+0.')
s = s.replace(".", "")
if "e" in s:
s = s.rstrip("+-0123456789")
s = s.rstrip("e")
return len(s)
How many significant digits are in 123000?
Function returns 6, but could be 3, 4, 5, or 6. That's OK for my purposes. Got a suggestion for these cases?
Dick
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor