Algorithm
[프로그래머스/파이썬(Python] 이진 변환 반복하기
kindof
2021. 7. 14. 11:24
코딩테스트 연습 - 이진 변환 반복하기
programmers.co.kr
- 문제에서 요구한 조건 그대로를 재귀적으로 반복해서 수행하면 되는 문제입니다.
[코드]
def binaryTranslate(x, tanslateCount, zeroCount):
# 종료조건 x == 1
if x == '1':
return tanslateCount, zeroCount
# 모든 0을 제거
numOfZeroes = x.count('0')
x = '1' * (len(x) -numOfZeroes)
zeroCount += numOfZeroes
# x는 x의 길이 c를 이진법으로 표현한 문자열
x = bin(len(x))[2:]
return binaryTranslate(x, tanslateCount+1, zeroCount)
def solution(s):
return list(binaryTranslate(s, 0, 0))