Programmers - 이진 변환 반복하기

SJ0000·2022년 5월 11일

문제 링크

bin()으로 진법 변환하는 방법을 사용하면 쉽게 풀 수 있다.

def solution(s):

    def convert(binary):
        zero_removed = binary.replace('0', '')
        converted = bin(len(zero_removed))[2:]
        return [converted, len(binary)-len(zero_removed)]

    zero_count = 0
    loop_count = 0
    while True:
        if s == '1':
            break
        loop_count += 1
        [s, removed] = convert(s)
        zero_count += removed

    return [loop_count, zero_count]
profile
잘하고싶은사람

0개의 댓글