[Tutor] finding sum of digits in a string

Dennis Lee Bieber wlfraed at ix.netcom.com
Tue Mar 15 10:16:53 EDT 2022


On Tue, 15 Mar 2022 00:41:11 +0000, Alan Gauld via Tutor <tutor at python.org>
declaimed the following:

>Because it iterates over the list of numbers meaning, on
>average 5 tests for each character. Now isdigit() might
>do that too but I hope not!
>

	Naive implementations (done at the level of Python rather than C)(using
the OPs "ele" term)

	all([(ch in string.digits) for ch in ele])
	all([("0" <= ch <= "9") for ch in ele])

I'd hope C is using something like a while loop to break out on the first
False compare rather than scanning all characters in ele.

	Okay for .isdigits(), but the various upper/lower/alpha/printable etc
may be better implemented with a table look-up (ignoring unicode matters),
maybe with a bit-map indicating each category the character fills.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list