Python Forum
Division of an integer into sub-numbers - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Division of an integer into sub-numbers (/thread-19103.html)



Division of an integer into sub-numbers - Richard_SS - Jun-13-2019

Hello everyone, ive got an interesting problem.
I have a big number (for example 73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450) and i need to add a comma between each number and append every number into a list. How do i solve that?
p.s. i am using python 3


RE: Division of an integer into sub-numbers - perfringo - Jun-13-2019

(Jun-13-2019, 12:01 PM)Richard_SS Wrote: i need to add a comma between each number and append every number into a list

Do I understand correctly that you have two tasks:

(1) need to add comma between each digit like 1,2,3,4
(2) append every number into list

Or you just need list of numbers? (can be done appr with 26 characters of code)

From thread name I read 'division'. Does it mean that / operator should be used as well?


RE: Division of an integer into sub-numbers - Richard_SS - Jun-14-2019

(Jun-13-2019, 02:27 PM)perfringo Wrote:
(Jun-13-2019, 12:01 PM)Richard_SS Wrote: i need to add a comma between each number and append every number into a list
Do I understand correctly that you have two tasks: (1) need to add comma between each digit like 1,2,3,4 (2) append every number into list Or you just need list of numbers? (can be done appr with 26 characters of code) From thread name I read 'division'. Does it mean that / operator should be used as well?
You understood correctly, i basically need to wrote an "algorithm" that divides that number into sub-numbers adding commas between them, like 123 to 1,2,3. I already have a big digit number to which i should apply that "algorithm".


RE: Division of an integer into sub-numbers - snippsat - Jun-14-2019

(Jun-14-2019, 10:04 AM)Richard_SS Wrote: i basically need to wrote an "algorithm" that divides that number into sub-numbers adding commas between them, like 123 to 1,2,3.
There is already build in stuff you can look,at test if work for that task.
>>> n = 123
>>> d = list(str(n))
>>> d
['1', '2', '3']
>>> [int(i) for i in d]
[1, 2, 3]
>>> # Or if need string display
>>> ','.join(d)
'1,2,3'



RE: Division of an integer into sub-numbers - DeaD_EyE - Jun-14-2019

If you want to use math, you can do following:

  1. Get the digits of the number with math.log10
  2. Convert the result of digits to integer
  3. In a for-loop with reversed(range(digits + 1))
  4. Each iteration give you a integer (large > 0), which should be the position of the digt.
  5. Integer division: big_number // 10**digits
  6. Use modulo, to get only one digit: result = result % 10

The implementation, of what I described:
def split_numbers(number):
    digits = int(math.log10(number))
    for d in reversed(range(digits + 1)):
        digit = (number // 10 ** d) % 10
        yield digit
Much easier is following code:
def split_digits(number):
    return [int(d) for d in str(number)]
The number could be a int or a str.
Math is not used. I use the fact, that str is iterable. If you
iterate over a str you'll get char by char.



+If you want to check, if a number is really an integer, use exceptions:
try:
    big_number = int(big_number_as_str)
except ValueError:
    print('Is not a int')
else:
    print('Is a int')
    print(big_number)
In Python a complex consists of two floats. One float for the real part, one float for the imaginary part.
Floats do have the method is_integer(), so you can test also, each part of the complex if it's an integer.