본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 48

반응형

Q>

You are given an array of integers.

(당신은 정수의 배열을받습니다.)

You should find the sum of the integers with even indexes (0th, 2nd, 4th...).

(짝수 인덱스 (0 번째, 2 번째, 4 번째 ...)가있는 정수의 합계를 찾아야합니다.)

Then multiply this summed number and the final element of the array together.

(그런 다음이 합계 된 수와 배열의 마지막 요소를 함께 곱하십시오.)

Don't forget that the first element has an index of 0.

(첫 번째 요소의 인덱스가 0임을 잊지 마십시오.)

For an empty array, the result will always be 0 (zero).

(빈 배열의 경우 결과는 항상 0입니다.)

 

Input: A list of integers.(정수 목록.)

 

Output: The number as an integer.(정수로 표시된 숫자입니다.)

 

Example:

checkio([0, 1, 2, 3, 4, 5]) == 30
checkio([1, 3, 5]) == 30
checkio([6]) == 36
checkio([]) == 0

 

How it is used: Indexes and slices are important elements of coding. This will come in handy down the road!

(인덱스와 슬라이스는 코딩의 중요한 요소입니다. 이것은 길 아래로 편리하게 올 것입니다!)

 

Precondition:

0 ≤ len(array) ≤ 20
all(isinstance(x, int) for x in array)
all(-100 < x < 100 for x in array)

 

A>

def checkio(array):
    """
        sums even-indexes elements and multiply at the last
    """
    # 리스트의 값을 가지고 판단(있으면 True / 없으면 False)
    if not array:
        return 0

    # 리스트의 첫번째 값부터 마지막까지 2 스탭씩 띄어서 더하고 array에 마지막 값이랑 곱함
    return sum(array[::2]) * array[-1]

# These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    print('Example:')
    print(checkio([0, 1, 2, 3, 4, 5]))

    assert checkio([0, 1, 2, 3, 4, 5]) == 30, "(0+2+4)*5=30"
    assert checkio([1, 3, 5]) == 30, "(1+5)*5=30"
    assert checkio([6]) == 36, "(6)*6=36"
    assert checkio([]) == 0, "An empty array = 0"
    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

 

O>

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

Process finished with exit code 0

 

S>

https://py.checkio.org/

반응형

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

Python Learn the basics Quiz 50  (0) 2019.06.10
Python Learn the basics Quiz 49  (0) 2019.06.10
Python Learn the basics Quiz 47  (0) 2019.06.10
Python Learn the basics Quiz 46  (0) 2019.06.10
Python Learn the basics Quiz 45  (0) 2019.06.10