본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 112

반응형

Q>

For the Robots the decimal format is inconvenient.

(로봇의 경우 10 진수 형식은 불편합니다.)

If they need to count to "1", their computer brains want to count it in the binary representation of that number.

("1"로 계산해야하는 경우, 컴퓨터의 두뇌는 해당 숫자의 이진 표현으로 계산하려고합니다.)

You can read more about binary here.

(여기서 바이너리에 대해 더 많이 읽을 수 있습니다.)

You are given a number (a positive integer).

(당신은 숫자 (양의 정수)를받습니다.)

You should convert it to the binary format and count how many unities (1) are in the number spelling.

(바이너리 형식으로 변환하고 번호 철자에있는 unities (1)의 수를 계산해야합니다.)

For example: 5 = 0b101 contains two unities, so the answer is 2.

(예 : 5 = 0b101에는 2 개의 단위가 포함되어 있으므로 대답은 2입니다.)

 

Input: A number as a positive integer.

        (숫자는 양의 정수입니다.)

 

Output: The quantity of unities in the binary form as an integer.

           (정수로 이진 형식의 unities의 양.)

 

Example:

checkio(4) == 1
checkio(15) == 4
checkio(1) == 1
checkio(1022) == 9

 

How it is used: How to convert a number to the binary form.

                     (숫자를 이진 형식으로 변환하는 방법.)

                     It can be useful for Computer Science purposes.

                     (컴퓨터 과학 목적에 유용 할 수 있습니다.)

 

Precondition: 0 < number ≤ 232

                  Guido van Rossum, the author of Python, is one of our most famous player.

                  (파이썬의 저자 인 귀도 반 로섬 (Guido van Rossum)은 우리의 가장 유명한 선수 중 하나입니다.)

                  He is writing some really wonderful code reviews for our player solutions.

                  (그는 우리 플레이어 솔루션을위한 훌륭한 코드 리뷰를 작성하고 있습니다.)

 

A>

def checkio(number: int) -> int:
    # str(bin()) : binary string을 string으로
    # bin : 정수를 "0b" 가 앞에 붙은 이진 문자열로 변환
    binary = str(bin(number))
    return binary.count(str(1))

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    assert checkio(4) == 1
    assert checkio(15) == 4
    assert checkio(1) == 1
    assert checkio(1022) == 9
    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

 

O>

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' 카테고리의 다른 글

2 ~ 20 까지 세는 코드  (0) 2021.05.08
Python Learn the basics Quiz 113  (0) 2019.07.03
Python Learn the basics Quiz 111  (0) 2019.07.02
Python Learn the basics Quiz 110  (0) 2019.07.02
Python Learn the basics Quiz 109  (0) 2019.06.28