[프로그래머스/Python] 이진 변환 반복하기

Sujin Lee·2022년 9월 23일
0

코딩테스트

목록 보기
116/172
post-thumbnail

문제

프로그래머스 - 이진 변환 반복하기

해결 과정

  • Python 문법 - 진수 변환

  • while문 돌기
  • result 가 우리가 찾는 '1'일 때 멈춤
  • '1' 이 아니라면result 를 빈 문자열로 초기화하고
  • s 에서 0을 제거한 문자열은 result
  • result의 문자열 길이를 이진수로 변환한 s

풀이

def solution(s):
    # 제거할 0의 개수
    zero = 0
    # 이진 변환한 횟수
    cnt = 0
    result = s
    while True:
        if result == '1':
            break
        result = ""    
        for i in s:
            if i == '1':
                result = result + i
            else:
                zero += 1
        s = bin(len(result))[2:]
        cnt += 1
    
    return [cnt,zero]
profile
공부한 내용을 기록하는 공간입니다. 📝

0개의 댓글