Python_Matter/Solve

Python Learn the basics Quiz 60

AnKiWoong 2019. 6. 12. 15:12
반응형

Q>

In this mission you should check if all elements in the given list are equal.

(이 임무에서 주어진 목록의 모든 요소가 같은지 확인해야합니다.)

 

Input: List.

 

Output: Bool.

 

Example:

all_the_same([1, 1, 1]) == True
all_the_same([1, 2, 1]) == False
all_the_same(['a', 'a', 'a']) == True
all_the_same([]) == True

The idea for this mission was found on Python Tricks series by Dan Bader.

(이 임무에 대한 아이디어는 Dan Bader의 Python Tricks 시리즈에서 발견되었습니다.

 

Precondition: all elements of the input list are hashable

                   (입력 목록의 모든 요소는 해시 가능합니다.)

 

A>

# typing : 순환 참조 에러 방지
from typing import List, Any

def all_the_same(elements: List[Any]) -> bool:
    # your code here
    # set은 중복 허용이 안됨(중복이 안되므로 값이 같다는 걸 의미)
    # elements를 set으로 만드면 중복은 삭제 되고 그 갯수를 세어 1개 이하이면 모든 값이 같는것을 의미
    return len(set(elements)) <= 1

if __name__ == '__main__':
    print("Example:")
    print(all_the_same([1, 1, 1]))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert all_the_same([1, 1, 1]) == True
    assert all_the_same([1, 2, 1]) == False
    assert all_the_same(['a', 'a', 'a']) == True
    assert all_the_same([]) == True
    assert all_the_same([1]) == True
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

O>

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

Process finished with exit code 0

 

S>

https://py.checkio.org

반응형