LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 2 weeks ago

transhumanist.liberation
Member

How to extract a digit from a number?

Like if we have an integer
a = 1234
b = a % 10 #extracts the final digit 4
what about If I wanna extract the 2nd to last digit 3? Similarly for 1 & 2
I am using Python 3

Last edited by transhumanist.liberation (2 weeks ago)

Offline

#2 2 weeks ago

m0ei
Member

Re: How to extract a digit from a number?

Loop, modulo 10 then divide 10, Keep doing it until you reach the "index" you want.

Other approaches exist, for example convert to an array/list... then you get the index you want.

Last edited by m0ei (2 weeks ago)

Offline

#3 2 weeks ago

potato
Member

Re: How to extract a digit from a number?

Offline

#4 2 weeks ago

jsaade
Member

Re: How to extract a digit from a number?

in most languages ->  int.Parse(nbr.ToString()[index])
or of course use %

Last edited by jsaade (2 weeks ago)

Offline

#5 2 weeks ago

transhumanist.liberation
Member

Re: How to extract a digit from a number?

Thx everyone for your help.

However I am writing a python program that adds the digits of a factorial product like 5! = 120 , sum = 1 + 2 + 0 = 3.
Here is my code:

import math
num = 10
fac = math.factorial(num)
list = []
n = 0
sum = 0
while fac > 0:
     digit = (fac // 10 ** (n-1)) % 10
     n += 1
     list.append(digit)
     for i in list:
         sum+=i
         break
print(sum)

Offline

#6 2 weeks ago

m0ei
Member

Re: How to extract a digit from a number?

1) don't name a variable sum, you'll overshadow the python built-in sum function, same for list.
2) fac value is always greater than 0, fac value has to change if you intend to exit that loop.
3) break will exit the nested for loop on the first iteration. You're also stuck in an infinite loop, because you have no ways to exist the while loop, the value of fac will always be greater than 0.
4) you don't need to use more than 1 loop if it's an assignment or if you're learning.
5) I have no idea what you're doing here (fac // 10 ** (n-1)) % 10 ... to get the last digit, just do `number % 10`

To solve it easily, you can simply do `sum([int(digit) for digit in str(fac)])`, this will convert the `fac` variable to a string (strings are iterable in Python), then it will loop on every digit and convert it to an integer. The built-in sum function will then loop on the list and calculate the sum for you.
You shouldn't do it this way if you're learning.

To solve it correctly, create a variable to hold that sum, loop while fac > 0, get the current digit by simply calling modulo 10 on that number, add that digit to that accumulating variable, change the value of fac to fac minus the last digit, basically fac // 10.

I didn't give you the answer directly, hopefully you got it.

Last edited by m0ei (2 weeks ago)

Offline

#7 2 weeks ago

transhumanist.liberation
Member

Re: How to extract a digit from a number?

I don't want to extract the last digit only, I want to extract every single digit ie 1234

Offline

#8 2 weeks ago

m0ei
Member

Re: How to extract a digit from a number?

I know. I already told you.

Here's a sample:

123 % 10 = 3
123 / 10 = 12
12 % 10 = 2
12 / 10 = 1
1 % 10 = 1
1 / 10 = 0 => you exit the loop

Everytime you modulo, you take that number and add it to a variable (the sum). Everytime you divide, you take that number and use it in the next modulo.

Offline

#9 2 weeks ago

Hybrid
Member

Re: How to extract a digit from a number?

You can run a recursive function with %10 to get every single digit

Or you can convert it to a string and read it character by character

Sorry I don't know python, so can't help with the code

Offline

#10 2 weeks ago

xterm
Moderator

Re: How to extract a digit from a number?

n = 98765
as_list = list(map(int, str(n)))

as_list[0] => 9
as_list[1] => 8
...

If this is homework, I doubt your professor will accept it.

Offline

#11 2 weeks ago

Joe
Member

Re: How to extract a digit from a number?

transhumanist.liberation wrote:

Like if we have an integer
a = 1234
b = a % 10 #extracts the final digit 4
what about If I wanna extract the 2nd to last digit 3? Similarly for 1 & 2
I am using Python 3

Your way
  • What's a % 100?

  • What's [(a % 100) - (a % 10)] / 10?

  • What's [(a % 1000) - (a % 100)] / 100?

  • What's [(a % 10**n) - (a % 10**(n-1))] / 10**(n-1)

This should get you where you want, but it's a convoluted way of doing it.

The better way

What others suggested, transform your number into a string, and access each element of the array by position. Remember to cast it back to a number before you do math on it.


The one-line way

DON'T DO THIS
This is bad coding style, but it's fun and it works. Here's how I sum the digits of the factorial of a number n:

import math
def f(n):
    return sum([int(d) for d in str(math.factorial(n))])

While it's bad style to write this code, it's important that you can read it and understand it. If something doesn't make sense, ask here.

Last edited by Joe (2 weeks ago)

Offline

Board footer