-
Notifications
You must be signed in to change notification settings - Fork 21
1주차 과제 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
1주차 과제 #8
Conversation
hannut91
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
재귀와 반복문에 대해서는 익숙하신 것 같아요. 하지만 꼬리재귀로 바꾸는 것이 아직은 어려우신 것 같습니다.
만약 1번 과제를 일반적인 재귀로 그려보면 다음과 같습니다.
만약 꼬리재귀로 그려본다면 다음과 같습니다.
꼬리재귀는 계속해서 값을 가지고 가서 스택이 필요로 하지 않죠? 대신에 추가적인 파라미터가 늘어났습니다.
계속해서 누적되어야 할 값을 넘겨주어야 하기 때문이죠. 그렇지 않으면 스택에 남겨놓아야 하니까요.
이런식으로 그려보면서 연습하면 도움이 되실 수도 있을 것 같습니다.
이번 한 주도 고생 많으셨습니다.
주간회고 채널에 이번 주에 배운 점, 깨달은 점들을 남겨주세요!
| /* eslint-disable no-unused-vars */ | ||
| const solution1 = (numbers) => { | ||
| //편한 방법 | ||
| if (numbers.length === 0) return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if문에서 코드가 한 줄이면 중괄호를 생략할 수 있는 것을 이용해서 간단하게 작성해 주신 것 같아요. 하지만 때로는 중괄호를 생략한 코드는 한 눈에 알아보기 어려워서 실수를 할 가능성이 있습니다. 그래서 한 줄이라도 중괄호를 사용해보는 것도 좋아요
problem-3/problem-3.test.js
Outdated
| const solution = (n) => { | ||
| const flag = n; | ||
| if (n === 0) return "0"; | ||
| if (n === 1) return "1"; | ||
| if (flag > 0) { | ||
| return `${solution(Math.floor(flag / 2))}${n % 2}`; | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아마도 함수 파라미터로 받은 값은 변경하지 않기 위해서 별도로 flag 변수를 만드신 것 같아요. 여기서는 그렇게 쓰이지 않으니 flag없이 n을 사용해도 될 것 같아요


재귀까지는...어거지로 해보려 했는데 최적화 부분이 시간 투자 대비 너무 막막했습니다ㅜㅜ.
계단 오르기는 개념도 모르겠어서 못했습니다.