views:

2078

answers:

5

I'm writing a program which calculates the check digit of an ISBN number. I have to read the user's input (nine digits of an ISBN) into an integer variable, and then multiply the last digit by 2, the second last digit by 3 and so on. How can I "split" the integer into its constituent digits to do this? As this is a basic exercise I am not supposed to use a list.

+15  A: 

Just create a string out of it.

myinteger = 212345
number_string = str(myinteger)

That's enough. Now you can iterate over it:

for ch in number_string:
    print ch # will print each digit in order

Or you can slice it:

print number_string[:2] # first two digits
print number_string[-3:] # last three digits
print number_string[3] # forth digit


Or better, don't convert the user's input to an integer (the user types a string)

isbn = raw_input()
for pos, ch in enumerate(reversed(isbn)):
    print "%d * %d is %d" % pos + 2, int(ch), int(ch) * (pos + 2)

For more information read a tutorial.

nosklo
+3  A: 
list_of_ints = [int(i) for i in str(ISBN)]

Will give you a sorted list of ints. Of course, given duck typing, you might as well work with str(ISBN).

Edit: As mentioned in the comments, this list isn't sorted in the sense of being ascending or descending, but it does have a defined order (sets, dictionaries, etc in python in theory don't, although in practice the order tends to be fairly reliable). If you want to sort it:

list_of_ints.sort()

is your friend. Note that sort() sorts in place (as in, actually changes the order of the existing list) and doesn't return a new list.

mavnn
or: list_of_ints = map(int, str(ISBN))
paffnucy
yay, paffnucy! Let's not forget map/reduce/zip et al.!
Daren Thomas
"sorted list of ints"? in what sense is it going to be "sorted"?
SilentGhost
@Daren - not always faster, but usually more readable (excluding reduce() ^^)
paffnucy
Sorted in the sense that it has a defined order, as opposed to say a tuple or a set. I'll edit that into the answer...
mavnn
tuple has an order alright.
SilentGhost
Yeah, I realised as soon as I submitted it. You'll notice I didn't mention them in the edit.
mavnn
+7  A: 
while number:
    digit = number % 10
    # do whatever with digit
    number /= 10

On each iteration of the loop, it removes the last digit from number, assigning it to $digit. It's in reverse, starts from the last digit, finishes with the first

Alexandru Nedelcu
what does it do?
SilentGhost
I added an explanation. If you don't see how it works, take a piece of paper and go through the process step-by-step for a number of choice.
Alexandru Nedelcu
It extracts the digits from the given number, assuming base 10, starting with the least significant digit. This is a mathematical approach instead of a Pythonic one. An approach that will work in any language. And an approach that will also work when one is interested in the digits of a number written in another base (unlike the other solutions posted here). Hence: +1.
Stephan202
this is absolutely insane and is not working in py3k (fix is tiny though)
SilentGhost
what is that? php? This solution is very unpythonic.
nosklo
@Stephan202: int is working with any base, not only base 10.
SilentGhost
@SilentGhost: the examples above apply int to single characters resulting from iterating over the result of applying str. It is str that returns a base 10 representation of the number.
Stephan202
@Stephan202: as it's fairly clear from the question OP starts with a string, so he doesn't need to convert to integer; ISBN is using base 10 digits; if you know the base there are enough buil-in function that let you convert resulting base-10 int to you base-X int.
SilentGhost
@nosklo: it's not php, it's basic arithmetic you're supposed to learn in the fifth grade. I don't see how a math or a computer science concept can be "unpythonic". Jesus :)
Alexandru Nedelcu
@Alexander: When you can just iterate over the string already entered by the user, instead of converting it to an integer and then doing arithmetic on it. From "Zen of Python": Simple is better than complex.
nosklo
Why not use a generator? yield the `digit`
st0le
A: 

How about a one-liner list of digits...

ldigits = lambda n, l=[]: not n and l or l.insert(0,n%10) or ldigits(n/10,l)
Lord British
@Lord British, use `//` for int division...on Py3k `/` returns a float.
st0le
Lord British
A: 

On Older versions of Python...

map(int,str(123))

On New Version 3k

list(map(int,str(123)))
st0le
@st0le: Which one-liner is faster?
Lord British
@Lord British, hard to say, since both are designed for different versions, i'd suppose Py3k should be faster (hopefully,hehe)
st0le