코딩테스트/코딩테스트 Python

[CodeSignal] findProfession (Python)

sseozytank 2022. 9. 19.

https://app.codesignal.com/interview-practice/task/FwAR7koSB3uYYsqDp/solutions

 

findProfession | CodeSignal

Press space bar to start a drag. When dragging you can use the arrow keys to move the item around and escape to cancel. Some screen readers may require you to be in focus mode or to use your pass through key

app.codesignal.com

 

✔️문제 요약 

1.엔지니어의 첫째 아이는 엔지니어, 둘째 아이는 의사

2.의사의 첫째 아이는 의사, 둘째 아이는 엔지니어

3.모든 세대의 의사와 엔지니어는 엔지니어로 시작

4.level과 pos가 주어졌을 때 직업을 출력 

 

✔️풀이 방법 

1.선언해준 함수를 내부에서 한번 더 불러와 부모의 직업을 판별해준다 

2.%2를 통해 첫째 아이와 둘째 아이를 구분하기 

#엔지니어의 첫째 아이는 엔지니어, 둘째 아들은 닥터 
#닥터의 첫째 아이는 닥터, 둘째 아이는 엔지니어 
#----------위 로직으로 작성해 보자-------------# 
#모든 세대의 의사와 엔지니어는 엔지니어로 시작



def solution(level, pos):
    #모든 세대의 의사와 엔지니어는 엔지니어로 시작 
    if level==1:
        return 'Engineer'
    
    #부모가 의사인 경우     
    if (solution(level-1,(pos+1)//2) == 'Doctor'):
        #첫째 = 의사
        if (pos%2)==1:
            return 'Doctor'
        #둘째 = 엔지니어
        else:
            return 'Engineer'
    #부모가 엔지니어인 경우
    else:
        #첫째 = 엔지니어
        if (pos%2)==1:
            return 'Engineer'
        #둘째 = 의사 
        else:
            return 'Doctor'

 

✔️고찰

-함수안에서 본인 함수를 리턴하는 재귀말고 if절에 쓰는 이런 문법을 처음봐서, 스터디에서 물어보고 추가해두도록 한다. 

댓글