jeeyeonLIM / coding_test

Let's practice the coding test!
1 stars 0 forks source link

Level3. 숫자에 콤마 삽입하기 #15

Open jeeyeonLIM opened 4 years ago

jeeyeonLIM commented 4 years ago

문제설명

숫자 형태의 문자열을 콤마가 포함된 금액 표기식 문자열로 바꾸어주는 프로그램을 작성하시오. ※ 단, 프로그래밍 언어에서 지원하는 금액변환 라이브러리는 사용하지 말것

예) 숫자 금액
1000 1,000
20000000 20,000,000
-3245.24 -3,245.24

출처

jeeyeonLIM commented 4 years ago

생각한로직 1.

jeeyeonLIM commented 4 years ago

Code

def solution(num):
    result=[]
    if int(num) <0: # 음수일 때
        len_num = int((len(num)-1)/3) 
        num = num[1:] 
    elif int(num) > 0: # 양수일 때
        len_num = int((len(num)-1)/3) 

    for i in range(len_num+1,0,-1):
        if i ==1 : 
            result.append(num[-3*i:])
        else : 
            result.append(num[-3*i:-3*(i-1)])        
    return ",".join(result)

image