알고리즘/프로그래머스
[프로그래머스 | Lv2] 이진 변환 반복하기 (Python)
sssbin
2023. 2. 1. 14:34
https://school.programmers.co.kr/learn/courses/30/lessons/70129
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
# 프로그래머스 70129: 이진 변환 반복하기
def solution(s):
d = 0
t = 0
while s != '1':
d += s.count('0')
x = s.count('1')
temp = ''
while x != 1:
temp = str(x % 2) + temp
x //= 2
temp = '1' + temp
t += 1
s = temp
return [t, d]
멍청하게 이진법 풀고 있었는데 파이썬에 이진법 변환 함수가 있다..!
# 프로그래머스 70129: 이진 변환 반복하기
def solution(s):
d = 0
t = 0
while s != '1':
d += s.count('0')
s = bin(s.count('1'))[2:]
t += 1
return [t, d]