Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using `str` the correct idiom for working with digits in Python

I understand that one way to work with the digits of a number in Python is to convert the number to a string, and then use string methods to slice the resulting "number" into groups of "digits". For example, assuming I have a function prime that tests primality, I can confirm that an integer n is both a left and right truncatable prime with

all(prime(int(str(n)[:-i])) and prime(int(str(n)[i:])) for i in range(1, len(str(n))))

This method involves first converting n to a string so that it can be sliced, and converting that slice back to an integer so that its primality can be checked. Perhaps it's my history with statically typed languages, or some vague idea that strings are "expensive", or experience with languages that include builtin functionality for similar manipulations (like Mathematica's IntegerDigits and FromDigits); but I'm left wondering whether this is the right way to go about such tasks.

Is conversion back and forth between stings and numbers the correct — or even only — approach for accessing digits in Python. Are there more efficient approaches?

like image 707
orome Avatar asked Nov 15 '13 23:11

orome


People also ask

How do you represent a digit in Python?

digits is a pre-initialized string used as string constant. In Python, string. digits will give the lowercase letters '0123456789'. Parameters : Doesn't take any parameter, since it's not a function.

Can numbers be a string in Python?

We can convert numbers to strings through using the str() method. We'll pass either a number or a variable into the parentheses of the method and then that numeric value will be converted into a string value. The quotes around the number 12 signify that the number is no longer an integer but is now a string value.

What does str mean in Python?

Python has a built-in string class named "str" with many handy features (there is an older module named "string" which you should not use). String literals can be enclosed by either double or single quotes, although single quotes are more commonly used.


1 Answers

In your example code, you could get away using divmod rather than string slicing the digits. divmod(x, y) returns the tuple x//y, x%y, which for y values that are 10**i is exactly what you want for the left and right pieces of your number. This isn't necessarily more Pythonic, though it might be a bit faster.

sn = str(n)
all(prime(int(sn[:i])) and prime(int(sn[i:])) for i in range(1, len(sn))) # original
all(all(map(prime, divmod(n, 10**i))) for i in range(1, len(sn))) # variant using divmod

I think for more general digit operations, using str is probably pretty sensible, as doing lots of math on powers of your numerical base is likely to be harder to understand than doing stuff directly on the digits in a string.

Write code to be read, unless it's really performance sensitive.

like image 83
Blckknght Avatar answered Oct 06 '22 01:10

Blckknght