Python_Matter/Solve

Python Learn the basics Quiz 54

AnKiWoong 2019. 6. 11. 17:13
반응형

Q>

You are given a positive integer.

(양의 정수가 주어집니다.)

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

(함수는 0을 제외한 자릿수를 계산해야합니다.)

For example: The number given is 123405.

(예 : 주어진 숫자는 123405입니다.)

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

(결과는 1 * 2 * 3 * 4 * 5 = 120입니다 (0을 제외하는 것을 잊지 마십시오).)

 

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

 

A>

def checkio(number: int) -> int:
    # 기준점
    res = 1

    #  조건 판단 (0은 False 이외 True)
    for i in str(number):
        if int(i):
            # 0이 아니면 res에 누적
            res *= int(i)

    return res

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!")

 

O>

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

Process finished with exit code 0

 

S>

https://py.checkio.org/

반응형