TIL - Recursion Practices

naseriansuzie·2019년 11월 28일
0

Immersive Course_TIL

목록 보기
9/27

Today What I Learned

Javascript를 배우고 있습니다. 매일 배운 것을 이해한만큼 정리해봅니다.


[Key Concepts When Coding Recursion Function]

  1. Break the problem I am trying to solve down into a problem that is one step simpler
  2. Assume that my function will work to solve the simpler problem — really believe it beyond any doubt
  3. Ask myself: Since I know I can solve the simpler problem, how would I solve the more complex problem?

My Practices

1. Example 1) a recursive function that reverses a string

  1. What is the One step simpler problem: I have to reverse the string from index 1 to index last.
  2. What is the Base case? if the given string is one character or less, then you don't need to reverse it but instead you show the string itself. The other part belongs to the recursive case.
  3. Let's believe the one step simpler problem will be resolved by my code somehow.

2. Example 2) a recursive function that prints all elements in an array

  1. What is the One step simpler problem: I have to print the rest of elements in the given array except the first one.
  1. What is the Base case? if the given array is one character or less and it's not an array, then you can just show the first index of it. The other part belongs to the recursive case.
  1. Now only the complex problem is left, let's focus on solving it.
  1. What is the Base case? if the given string is one or two characters, then it's true. The other part belongs to the recursive case.
  1. Now only the complex problem is left, let's focus on solving it. In this case, it's divided into two cases(odd number, even number).
  1. What is the Base case? if the given array is empty, it returns an empty array itself.
  1. Now only the complex problem is left, let's focus on solving it.
profile
선한 변화를 만드는 일에 기여하는 개발자가 되고 싶습니다.

0개의 댓글