Answer to Question #302717 in Python for Rahul

Question #302717

Product of the String

You are given an alphanumeric string S.Write a program to multiply all the charcters(except trailing zeroes) in S and print the output.


Rules for multiplication:

  • If the character is digit, multiply its value.
  • If the character is an alphabetic character, multiply the product of digits of its ASCII value.


Input

The first line contains a string S.


Output

The output should be a integer denoting the product of characters.


Explanation

For N = 123a4


ASCII value of a is 97.

Product of digits of ASCII value of a is 9 *7 = 63.

Therefore the product of characters is 1*2*3*4*63*4 = 1512.


Sample Input1

123a4

Sample Output1

1512



1
Expert's answer
2022-02-27T07:02:37-0500
S = input()
mult = 1
for i in range(len(S)):
    if S[i].isdigit() == True:
        if S[i] != '0':
            mult = mult * int(S[i])
    else:
        mult_ascii = 1
        num = ord(S[i])  # digits of ASCII value
        while (num != 0):
            mult_ascii = mult_ascii * (num % 10)  # multiply all digits
            num = num // 10
        if mult_ascii != 0:
            mult = mult * mult_ascii
print(mult)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS