프로그래머스/LV2
[프로그래머스 Lv2, python]- 이진 변환 반복하기
chyam_eun
2024. 12. 26. 11:17
def solution(s):
cntz=0 #0의 개수
cnt=0 #반복 횟수
while(s!="1"):
cnto=0 #1의 개수
for a in s:
if a!="0":
cnto+=1
else:
cntz+=1
s="" #문자열 초기화
while(cnto>0): #새로운 2진수 저장
s+=str(cnto%2)
cnto//=2
cnt+=1
return [cnt,cntz]
bin과 count를 사용하지 않고 구했었다. count를 사용하면 더 간단하게 바꿀수있다.
def solution(s):
cntz=0 #0의 개수
cnt=0 #반복 횟수
while(s!="1"):
cnto=s.count('1')
cntz+=s.count('0')
s=bin(cnto)[2:] #2부터 하는 이유는 앞에 0b가 붙음
cnt+=1
return [cnt,cntz]