[프로그래머스/파이썬] Level 2 이진 변환 반복하기

bye9·2021년 4월 21일
0

알고리즘(코테)

목록 보기
121/130

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


문제풀이

문자열 s가 "1"이 될때까지 반복문을 수행한다.
반복문이 돌때마다 cnt+1을 해주고, 0의 개수를 더해준다.
그리고 문자열을 1의 길이를 2진수로 변환한 문자열로 변환한다.

소스코드

def solution(s):
    cnt=0
    zero_value=0
    while s!="1":
        cnt+=1
        zero_value+=s.count("0")
        s=format(s.count("1"),"b")
    
    return [cnt,zero_value]

0개의 댓글