본문 바로가기

PS/프로그래머스

[프로그래머스 LV 2] 이진 변환 반복하기 (파이썬/python)

728x90

https://programmers.co.kr/learn/courses/30/lessons/70129

 

코딩테스트 연습 - 이진 변환 반복하기

 

programmers.co.kr

 

단순하게 2진법으로 변환하며 문제를 구현하면 되는 쉬운 문제였다.

trans 함수 안에 이진법으로 변환하는 함수를 넣어놨다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def solution(s):
    answer = []
    x = s
    cnt = 0
    num = 0
 
    def trans(n):
        arr = "01"
        result = ''
        num1 = int(n)
 
        while num1 >= 1:
            result = arr[num1 % 2+ result
            num1 = int(num1) // 2
 
        return result
 
    while x != '1':
 
        temp = ''
        for c in x:
            if c == '1':
                temp += '1'
                continue
            num += 1
 
        x = trans(len(temp))
        cnt += 1
 
    answer.append(cnt)
    answer.append(num)
 
    return answer
cs

 

728x90