본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 105

Q>

Positive integers can be expressed as sums of consecutive positive integers in various ways.

(양의 정수는 여러 가지 방법으로 연속적인 양의 정수의 합으로 표현 될 수 있습니다.)

For example, 42 can be expressed as such a sum in four different ways:(a) 3+4+5+6+7+8+9, (b) 9+10+11+12, (c) 13+14+15 and (d) 42.

(예를 들어, 42는 (a) 3 + 4 + 5 + 6 + 7 + 8 + 9, (b) 9 + 10 + 11 + 12, (c) 13 + 14 +15 및 (d) 42.)

As the last solution (d) shows, any positive integer can always be trivially expressed as a singleton sum that consists of that integer alone.

(마지막 해결책 (d)에서 알 수 있듯이, 모든 양의 정수는 항상 그 정수만으로 구성된 단일자 합으로 쉽게 표현할 수 있습니다. )

Compute how many different ways it can be expressed as a sum of consecutive positive integers.

(연속적인 양의 정수의 합으로 표현할 수있는 여러 가지 방법을 계산합니다.)

 

Input: Int.

 

Output: Int.

 

Example:

count_consecutive_summers(42) == 4
count_consecutive_summers(99) == 6

 

Precondition: Input is always a positive integer.

                   (입력은 항상 양의 정수입니다.)

 

The mission was taken from Python CCPS 109 Fall 2018. It’s being taught for Ryerson Chang School of Continuing Education by Ilkka Kokkarinen

(선교사는 Python CCPS 109 Fall 2018에서 가져 왔습니다. Ryerson Chang 평생 교육 학교 (Ilkka Kokkarinen)가 가르치고 있습니다.)

 

A>

def count_consecutive_summers(num):
    # your code here
    c = 0
    for n in range(0, num):
        # 분모 계산
        d = num - n * (n + 1) // 2

        if d <= 0:
            break
        if d % (n + 1) ==0:
            c += 1

    return c

if __name__ == '__main__':
    print("Example:")
    print(count_consecutive_summers(42))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert count_consecutive_summers(42) == 4
    assert count_consecutive_summers(99) == 6
    assert count_consecutive_summers(1) == 1
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

O>

Example:
4
Coding complete? Click 'Check' to earn cool rewards!

Process finished with exit code 0

 

S>

https://py.checkio.org

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 107  (0) 2019.06.27
Python Learn the basics Quiz 106  (0) 2019.06.27
Python Learn the basics Quiz 105  (0) 2019.06.25
Python Learn the basics Quiz 104  (0) 2019.06.25
Python Learn the basics Quiz 103  (0) 2019.06.25
Python Learn the basics Quiz 102  (0) 2019.06.25