programmers | Lv2. 이진 변환 반복하기 [Python]

yeonk·2022년 3월 14일
0

algorithm

목록 보기
69/88
post-thumbnail

💡 Python 3






🔗 문제

이진 변환 반복하기 [Link]






💻 코드

def solution(s):
    import re
    answer = [0,0]
    while True:
        answer[0] += 1
        answer[1] += s.count('0')
        s = re.sub(r'[0]', '', s)
        if s == '1': break
        s = bin(len(s))[2:]
    return answer






💥 다른 사람 코드

헐랭 1을 카운트하는게 더 간단하고 좋은 것 같다. 복잡하게 생각하지 말기..!!

def solution(s):
    a, b = 0, 0
    while s != '1':
        a += 1
        num = s.count('1')
        b += len(s) - num
        s = bin(num)[2:]
    return [a, b]

0개의 댓글