Multiplying all the digits of a number in python

Viewed 392

If i have a number 101, i want to multiply all the digits of the number (1*0*1) but this result will become Zero. Instead how to split the number into 10 and 1 and multiply(10*1).

Similar examples, 3003033 -> 300*30*3*3 or 2020049 -> 20*200*4*9

3 Answers

That's kind of odd, but this code will work:

    a = '3003033'
    num = ''
    last_itr = 0
    tot=1

    for i in range(len(a)-1):
        if a[i]=='0' and a[i+1]<='9' and a[i+1]>'0':
            tot*=int(a[last_itr:i+1])
            last_itr=i+1

        elif a[i]>'0' and a[i]<='9' and a[i+1]<='9' and a[i+1]>'0':
            tot*=int(a[i])
            last_itr=i+1


tot*=int(a[last_itr:len(a)])
print(tot)

Just put your number at a

You could use a negative look behind to check its not the start of the list and a positive look ahead for nums that are not 0 as your split point.

REGEX: Essentially this says split where the next num is not a 0 and it not the start of the line

/
(?<!^)(?=[^0])
/
gm
Negative Lookbehind (?<!^)
Assert that the Regex below does not match
^ asserts position at start of a line
Positive Lookahead (?=[^0])
Assert that the Regex below matches
Match a single character not present in the list below [^0]
0 matches the character 0 literally (case sensitive)

CODE

import re
from functools import reduce

def sum_split_nums(num):
    nums = re.split(r'(?<!^)(?=[^0])', str(num))
    total = reduce((lambda x, y: int(x) * int(y)), nums)
    return " * ".join(nums), total

nums = [3003033, 2020049, 101, 4040]
for num in nums:
    expression, total = sum_split_nums(num)
    print(f"{expression} = {total}")

OUTPUT

300 * 30 * 3 * 3 = 81000
20 * 200 * 4 * 9 = 144000
10 * 1 = 10
40 * 40 = 1600

Let a and b be two integer numbers. Let c be a new number made by putting n zeros in the right side of b. Then multiplying a and c is equal to multiplying a and b and 10^n.

Now you can simplify what you want to do to the following: Multiply digits of your number to each other with the agreement that instead of 0, you will put 10. So actually you don't need to split your number.

Here I defined two functions. In both of them the idea is to convert your number to a string, run a for-loop on its digits and by an if condition in the case

1) multiply the previous result to the new digit if it is not 0, otherwise multiply to 10.

def multio1(x):
s = str(x)
ans = 1
for i in range(len(s)):
    if s[i] != '0':
        ans *= int(s[i])
    else:
        ans *= 10
return(ans)

2) multiply the previous result to the new digit if it is not 0, otherwise add one unit to the number of zeros. Then at the end put as many as number of zeros, zeros at the right side of your final result.

def multio2(x):
s = str(x)
ans = 1
number_of_zeros = 0
for i in range(len(s)):
    if s[i] != '0':
        ans *= int(s[i])
    else:
        number_of_zeros += 1
if number_of_zeros != 0:
    ans = str(ans)
    for i in range(number_of_zeros):
        ans += '0'
    ans = int(ans)
return(ans)

Now the multio1(x) and multio2(x) for x=101,3003033,2020049, both gives equal results shown in below.

10,81000,144000
Related