본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 67

반응형

Q>

A median is a numerical value separating the upper half of a sorted array of numbers from the lower half.

(중앙값은 정렬 된 숫자 배열의 위쪽 절반을 아래쪽 절반에서 분리하는 숫자 값입니다.)

In a list where there are an odd number of entities, the median is the number found in the middle of the array.

(홀수의 엔티티가있는 목록에서 중앙값은 배열의 중간에있는 번호입니다.)

If the array contains an even number of entities, then there is no single middle value, instead the median becomes the average of the two numbers found in the middle.

(배열에 짝수 개의 엔티티가 포함 된 경우 중간 값이 하나도 없으며 중앙값은 중간에있는 두 숫자의 평균이됩니다.)

For this mission, you are given a non-empty array of natural numbers (X).

(이 임무를 위해 비어 있지 않은 자연수 (X)의 배열이 주어집니다.)

With it, you must separate the upper half of the numbers from the lower half and find the median.

(그것으로, 당신은 하위 절반에서 숫자의 상단 절반을 분리하고 중간을 찾아야합니다.

 

Input: An array as a list of integers.

        (정수리스트로 배열)

 

Output: The median as a float or an integer.

           (중간 값을 실수 또는 정수로 나타냅니다.)

 

Example:

checkio([1, 2, 3, 4, 5]) == 3
checkio([3, 1, 2, 5, 3]) == 3
checkio([1, 300, 2, 200, 1]) == 2
checkio([3, 6, 20, 99, 10, 15]) == 12.5

 

How it is used: The median has usage for Statistics and Probability theory, it has especially significant value for

                      skewed distribution.

                     (중앙값에는 통계 및 확률 이론에 대한 사용법 있으며 비뚤어진 분포에 특히 중요한 가치가 있습니다. )

                      For example: we want to know average wealth of people from a set of data -- 100 people earn

                      $100 in month and 10 people earn $1,000,000. If we average it out, we get $91,000.

                     (100 명이 한 달에 100 달러를 벌고 10 명이 100 만 달러를 벌었습니다.

                      평균을 내면 91,000 달러가됩니다.)

                      This is weird value and does nothing to show us the real picture.

                      (이것은 이상한 가치이며 실제 그림을 보여줄 수있는 게 없습니다.)

                      In this case the median would give to us more useful value and a better picture.

                      (이 경우 중간 값은 우리에게 더 유용한 가치와 더 좋은 그림을 줄 것입니다.)

                      The Article at Wikipedia.

 

Precondition: 1 < len(data) ≤ 1000
                   all(0 ≤ x < 10 ** 6 for x in data)

 

A>

# 중앙값 구하기
from typing import List

def checkio(data: List[int]) -> [int, float]:

    #replace this for solution
    # 정렬
    data.sort()
    # 전체 갯수를 2로 몫을 구하기
    half = len(data) // 2

    # 중앙값 판별
    return (data[half] + data[-half-1]) / 2

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

    assert checkio([1, 2, 3, 4, 5]) == 3, "Sorted list"
    assert checkio([3, 1, 2, 5, 3]) == 3, "Not sorted list"
    assert checkio([1, 300, 2, 200, 1]) == 2, "It's not an average"
    assert checkio([3, 6, 20, 99, 10, 15]) == 12.5, "Even length"
    print("Start the long test")
    assert checkio(list(range(1000000))) == 499999.5, "Long."
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

O>

Example:
3.0
Start the long test
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 69  (0) 2019.06.15
Python Learn the basics Quiz 68  (0) 2019.06.15
Python Learn the basics Quiz 66  (0) 2019.06.14
Python Learn the basics Quiz 65  (0) 2019.06.14
Python Learn the basics Quiz 64  (0) 2019.06.14