soyeonland

이진 변환 반복하기 본문

Study/코딩테스트 연습

이진 변환 반복하기

soyeonland 2024. 7. 4. 20:51
cnt_remove_zero = 0
def get_nextlength(s):
    global cnt_remove_zero
    cnt = 0
    for i in s:
        if i=="0":
            cnt += 1
    next_length = len(s)-cnt
    cnt_remove_zero += cnt
    return next_length

def change_num(num):
    
    tmp_s = ""
    share = num//2
    res = num%2
    
    tmp_s += str(res)
    
    while(share!=0):
        res = share%2
        share = share//2
        tmp_s += str(res)
    
    new_s = ""
    for i in range(len(tmp_s)):
        new_s += tmp_s[len(tmp_s)-1-i]
    return new_s    
        
def solution(s):
    global cnt_remove_zero
    cnt_while = 0
    # print()

    #print(s)
    while(s != "1"):
        new_num = get_nextlength(s)
        s = change_num(new_num)
        cnt_while += 1
        
    answer = [cnt_while, cnt_remove_zero]
    return answer

'Study > 코딩테스트 연습' 카테고리의 다른 글

짝지어 제거하기 (시간초과 -> 통과)  (0) 2024.07.07
피보나치수  (0) 2024.07.04
JadenCase 문자열 만들기  (0) 2024.06.30
최솟값 만들기  (0) 2024.06.30
최댓값과 최솟값  (0) 2024.06.30