Skip to content
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

Recursive Methods && Project: Recursion && Space complexity: Provided descriptive link texts and updated list with lazy numbering #27891

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions javascript/computer_science/project_recursion.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The [Fibonacci Sequence](http://en.wikipedia.org/wiki/Fibonacci_number), is a nu

You should already be thinking that perhaps this can be solved iteratively rather than recursively, and you would be correct. Nevertheless generating the sequence recursively is an excellent way to better understand recursion.

To understand how recursion works with Fibonacci watch this step-through video from [Khan Academy](https://www.youtube.com/watch?v=zg-ddPbzcKM).
You can watch this [video from Khan Academy on recursive Fibonacci](https://www.youtube.com/watch?v=zg-ddPbzcKM) to understand it further.

#### Merge sort

Expand All @@ -18,8 +18,8 @@ Merge sort is one such sorting algorithm, and can be much faster than other algo

To understand what the merge sort algorithm is doing check out these resources:

1. Watch this [introductory video from Harvard's CS50x course](https://youtu.be/Ns7tGNbtvV4).
1. Watch this more [detailed video explanation by David J. Malan](https://youtu.be/4oqjcKenCH8?t=6248) (watch only until 1:58:33).
1. Watch this introductory video on [merge sort from Harvard's CS50x course](https://youtu.be/Ns7tGNbtvV4).
1. Watch another explanation of [how merge sort works](https://youtu.be/4oqjcKenCH8?t=6248) (watch only until 1:58:33), this time from a part of a Harvard CS50x lecture.
1. [The concept of merging](https://youtu.be/6pV2IF0fgKY) and [Merge Sort -- How it Works part](https://youtu.be/mB5HXBb_HY8) on YouTube give you a more formal look at this problem if you're still unclear.
1. (Optional) Play with this [Merge Sort Visualizer](https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/visualize/) to get a better feel for exactly what is happening during a Merge Sort.

Expand Down Expand Up @@ -62,5 +62,5 @@ This section contains helpful links to related content. It isn't required, so co

- An extensive written JavaScript solution for [recursive Fibonacci](https://www.scaler.com/topics/fibonacci-series-in-javascript/)
- Another look at [merge sort](http://www.sorting-algorithms.com/merge-sort)
- Visualize and [understand](https://www.educative.io/courses/recursion-for-coding-interviews-in-javascript/NEZ7kKgMJKK) the memory allocation for recursive functions
- Visualize and understand the [memory allocation for recursive functions](https://www.educative.io/courses/recursion-for-coding-interviews-in-javascript/NEZ7kKgMJKK).
- For more attempts at recursion try the first 5 problems in [Project Euler](https://projecteuler.net/problems)
21 changes: 13 additions & 8 deletions javascript/computer_science/recursive_methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,31 @@
### Assignment

<div class="lesson-content__panel" markdown="1">
1. Read [this recursion resource](https://javascript.info/recursion) for a good intro to recursion.
2. Watch [this explanation of recursion](https://www.youtube.com/watch?v=6oDQaB2one8) by Web Dev Simplified and [this additional example of recursion](https://youtu.be/LteNqj4DFD8?t=340) by DevSage.
3. Watch this [Video on Recursion](https://www.youtube.com/watch?v=mz6tAJMVmfM) from CS50.
4. Read the ["Implementation Issues" section of the wiki article](http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm#Implementation_issues) to get an overview of some of the limitations of recursion.

1. Read this [intro to recursion](https://javascript.info/recursion).
1. Watch this [explanation of recursion by Web Dev Simplified](https://www.youtube.com/watch?v=6oDQaB2one8) and this additional [example of recursion by DevSage](https://youtu.be/LteNqj4DFD8?t=340).
1. Watch this [Video on Recursion](https://www.youtube.com/watch?v=mz6tAJMVmfM) from CS50.
1. Read the ["Implementation Issues" section of the wiki article](http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm#Implementation_issues) to get an overview of some of the limitations of recursion.

</div>

### Test yourself

Check failure on line 35 in javascript/computer_science/recursive_methods.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Required heading structure

javascript/computer_science/recursive_methods.md:35 TOP004/lesson-headings Required heading structure [Expected: h4 heading; Actual: h3 heading] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP004.md

<div class="lesson-content__panel" markdown="1">

1. Solve each of the questions from this [Code Quiz](https://www.codingame.com/playgrounds/5422/js-interview-prep-recursion) on Recursion. It is not important to have recursive algorithms committed to memory at this point; just understand how to create and use them.

<div class="lesson-note lesson-note--warning" markdown="1">

The solution for "Question 6: Search JS object" is incorrect. See the [corrected solution](https://gist.github.com/JoshDevHub/b00125f483d4a1ecc257eaa030916973) after you solve it.

</div>

</div>

### Knowledge check

This section contains questions for you to check your understanding of this lesson on your own. If you’re having trouble answering a question, click it and review the material it links to.
The following questions are an opportunity to reflect on key topics in this lesson. If you can't answer a question, click on it to review the material, but keep in mind you are not expected to memorize or master this knowledge.

- [How would you briefly define a recursive function?](#introduction)
- [What is the point of recursion? Is it more efficient than using a plain loop?](http://ruby.bastardsbook.com/chapters/recursion/)
Expand All @@ -52,10 +57,10 @@

### Additional resources

This section contains helpful links to other content. It isn't required, so consider it supplemental.
This section contains helpful links to related content. It isn't required, so consider it supplemental.

- [Efficient Recursion from U of Alberta](http://webdocs.cs.ualberta.ca/~holte/T26/efficient-rec.html)
- [A good resource of recursion by example](https://www.javascripttutorial.net/javascript-recursive-function/)
- [Visualize how recursion works on factorials](https://pythontutor.com/render.html#code=function%20calcFactorial%28num%29%20%7B%0A%20%20%20%20if%20%28num%20%3D%3D%3D%201%29%20%7B%0A%20%20%20%20%20%20%20%20return%201%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20num%20*%20calcFactorial%28num%20-%201%29%3B%0A%7D%0A%0AcalcFactorial%285%29%3B&cumulative=false&curInstr=2&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=js&rawInputLstJSON=%5B%5D&textReferences=false)
- [You may want to watch this nice freecodecamp course. It explains the logic of recursion very well.](https://www.youtube.com/watch?v=IJDJ0kBx2LM&t=2333s)
- [This video presents a few interesting problems with recursion](https://www.youtube.com/watch?v=ngCos392W4w)
- You may want to watch this nice [FreeCodeCamp course on recursion](https://www.youtube.com/watch?v=IJDJ0kBx2LM&t=2333s).
- [5 Simple Steps for Solving Any Recursive Problem](https://www.youtube.com/watch?v=ngCos392W4w)
14 changes: 7 additions & 7 deletions javascript/computer_science/space_complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Let's work through some examples. We won't go through every possible complexity

Consider this example

```js
```javascript
function multiply(num1, num2) {
return num1 * num2;
}
Expand All @@ -65,7 +65,7 @@ Here it should hopefully be clear that no matter the arguments we pass to the fu

Most data structures you come across will have a space complexity of O(N). That makes sense - when you increase the number of items in your data structure, it increases the space that data structure occupies in a linear way.

```js
```javascript
function sumArr(arr) {
const copyArr = arr.slice();
let sum = 0;
Expand All @@ -82,7 +82,7 @@ Why did we make a copy of the array? That will be discussed in a later section.

The complexity is replicated no matter the data structure:

```js
```javascript
function sumObjectValues(obj) {
const copyObject = { ...obj };
let sum = 0;
Expand All @@ -109,7 +109,7 @@ That's why we won't be diving into examples for other Big O notations with space

One of the common areas that causes confusion when considering space complexity is what constitutes using space in the context of an algorithm. In an earlier example we wrote methods that duplicated an array and object argument. We did that to be explicit. But what if we'd written the method as:

```js
```javascript
function sumArr(arr) {
let sum = 0;
arr.forEach((number) => {
Expand Down Expand Up @@ -137,14 +137,14 @@ On top of these considerations, you also need to balance the readability of your

<div class="lesson-content__panel" markdown="1">

1. Read [this article on big O and space complexity](https://dev.to/mwong068/big-o-space-complexity-lcm). It isn't detail heavy but does a good job explaining things clearly, and does lightly cover recursive functions. The code examples are in Ruby, but you should be able to follow along.
1. [This article on recursion and space complexity](https://dev.to/elmarshall/recursion-and-space-complexity-13gc) offers a little more context to recursive functions and their space complexity.
1. Read this [article on big O and space complexity](https://dev.to/mwong068/big-o-space-complexity-lcm). It isn't detail heavy but does a good job explaining things clearly, and does lightly cover recursive functions. The code examples are in Ruby, but you should be able to follow along.
1. This [article on recursion and space complexity](https://dev.to/elmarshall/recursion-and-space-complexity-13gc) offers a little more context to recursive functions and their space complexity.

</div>

### Knowledge check

This section contains questions for you to check your understanding of this lesson on your own. If you’re having trouble answering a question, click it and review the material it links to.
The following questions are an opportunity to reflect on key topics in this lesson. If you can't answer a question, click on it to review the material, but keep in mind you are not expected to memorize or master this knowledge.

- [What is space complexity?](#what-do-we-mean-by-space-complexity)
- [How do we measure space complexity?](#measuring-space-complexity)
Expand Down