반응형
https://school.programmers.co.kr/learn/courses/30/lessons/70129
풀이
문자열에서 0을 없애야하기 때문에 replace 함수를 이용했다
0을 제거 후에 문자열 길이가 이진법으로 표현되기 때문에 bin 함수를 사용했다
이진법 구현을 위해서는 bin 함수나 format 함수를 사용하면 된다.
그후 while True로 무한 반복 시킨다음에 s가 1이 되면 break하기.
그 과정에서 count를 구해주면 답 완성
def solution(s):
answer = []
countZero = 0
count = 0
while True:
countZero += s.count("0")
count += 1
s = s.replace("0", "")
b = str(bin(len(s)))
s = b[2:]
if s == "1":
break
answer = [count, countZero]
return answer
728x90
반응형
'코딩 테스트 > Programmers' 카테고리의 다른 글
[Python] [Level 2] 카펫 (루트와 약수를 활용하기) (0) | 2022.10.08 |
---|---|
[Python] [Level 2] 다음 큰 숫자 (bin 함수, count 함수 활용하기) (0) | 2022.10.08 |
[Python] [Level 2] 피보나치 수 구현 (0) | 2022.10.04 |
[Python] [Level 2] 숫자의 표현 (약수 구하기, divmod로 나머지 활용하기) (0) | 2022.10.04 |
[Python] [Level 2] 올바른 괄호 (replace 함수, find 함수, count 함수 활용하기) (1) | 2022.09.30 |