Python_Matter/[Check_IO]Home

# Digits Multiplication

반응형

Quiz>

We have prepared a set of Editor's Choice Solutions.

You will see them first after you solve the mission. In order to see all other solutions you should change the filter.

juggler You are given a positive integer.

Your function should calculate the product of the digits excluding any zeroes.

For example:

The number given is 123405. The result will be 1*2*3*4*5=120 (don't forget to exclude zeroes).

Input:

A positive integer.

Output:

The product of the digits as an integer.

Example:

``````checkio(123405) == 120
checkio(999) == 729
checkio(1000) == 1
checkio(1111) == 1``````

How it is used:

This task can teach you how to solve a problem with simple data type conversion.

Precondition:

0 < number < 106

``````def checkio(number: int) -> int:

return 1

if __name__ == '__main__':
print('Example:')
print(checkio(123405))

# These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(123405) == 120
assert checkio(999) == 729
assert checkio(1000) == 1
assert checkio(1111) == 1
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")``````

Solve>

1. 초기값 설정

``````def checkio(number: int):
initial_value = 1``````

2. 0은 False 이외에는 True 조건을 판단 하여 초기값에 누적한다.

``````def checkio(number: int):
for i in str(number):
if int(i):
initial_value *= int(i)``````

3. 초기값을 반환한다.

``````def checkio(number: int):
return initial_value``````

Code>

``````def checkio(number: int):
initial_value = 1

for i in str(number):
if int(i):
initial_value *= int(i)

return initial_value``````

Example>

``````if __name__ == '__main__':
print('Example:')
print(checkio(123405))

# These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(123405) == 120
assert checkio(999) == 729
assert checkio(1000) == 1
assert checkio(1111) == 1
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")``````

Result>

Example:

120

Coding complete? Click 'Check' to review your tests and earn cool rewards!

반응형

#### 'Python_Matter > [Check_IO]Home' 카테고리의 다른 글

Pawn Brotherhood  (0) 2020.04.15 2020.04.15 2020.04.14 2020.04.14 2020.04.14