Tail Recursion vs Recursion 개인노트

hur-kyuh-leez·2019년 11월 25일
0

검색을 해보니
이게 가장 설명이 잘 되어 있다.
https://makefortune2.tistory.com/98
그리고 다른 웹사이트는 대부분 같은 예제를 쓴다...;

요약: Tail Recursion
return 할 때 바로 recursion할 함수 이외에 계산 할 걸 두지 않는다. 그래서 stack overflow가 생기지 않는다.

p.s.
사실...
개미수열을 좀 더 효율적으로 풀 수 있는 방법이 없을까 해서 찾아봄...
재귀함수 써서 쓰는게 몇 개 있는데 설명이 제대로 안나와 있다.
일반적인 해법은 여기가 잘 나와있다.
https://www.rosettacode.org/wiki/Look-and-say_sequence

예)

# Python 3 Solution
def lookandsay(number):
    result = ""
 
    repeat = number[0]
    number = number[1:]+" "
    times = 1
 
    for actual in number:
        if actual != repeat:
            result += str(times)+repeat
            times = 1
            repeat = actual
        else:
            times += 1
 
    return result
 
num = "1"
 
for i in range(10):
    print num
    num = lookandsay(num)
profile
벨로그에 생각을 임시로 저장합니다. 틀린건 틀렸다고 해주세요 :) 그래야 논리 학습이 강화됩니다.

0개의 댓글