L2 : 이진 변환 반복하기 Python

jhyunn·2023년 1월 12일
0

Programmers

목록 보기
21/69

L2 : 이진 변환 반복하기 Python

https://school.programmers.co.kr/learn/courses/30/lessons/70129

def solution(s):
    cnt, zero_cnt = 0, 0
    while s != '1':
        cnt += 1
        zero_cnt += s.count('0')
        s = bin(int(s.count('1')))[2:] # 1개수를 int로 변환. 그 후 2진수로 변환

    return [cnt, zero_cnt]

0과 1의 갯수를 세는 것은 .count()를 활용한다.

#bin #이진수

profile
https://github.com/Sungjeonghyun

0개의 댓글