인천의 자유인

[Python] 다항식 더하기 - 프로그래머스 본문

Python/Python코딩테스트

[Python] 다항식 더하기 - 프로그래머스

Youngook 2024. 4. 30. 17:41
728x90
반응형

 

 

 

 

나의 문제 풀이

def solution(polynomial):
    answer = ''
    count1 = 0
    count2 = 0
    function = polynomial.split(' + ')
    for i in function:
        if 'x' in i:
            if len(i) == 1:
                count1 += 1
                continue
            a = i.replace('x','')
            count1 += int(a)
        else:
            count2 += int(i)
    if count1 == 1:
        count1 = "" 
    if count2==0:
        return str(count1) + "x"
    elif count1 == 0:
        return str(count2)
    else:
        return str(count1) + "x" + " + " + str(count2)
728x90
반응형