Skip to content

Instantly share code, notes, and snippets.

@mximenes88
Last active September 28, 2018 19:02
Show Gist options
  • Save mximenes88/c9e48ef62002a5a749b4ce8e1df09820 to your computer and use it in GitHub Desktop.
Save mximenes88/c9e48ef62002a5a749b4ce8e1df09820 to your computer and use it in GitHub Desktop.
Recursion Assignment for Web Development Track @ Bloc
#### Exercises
1. Define and compare recursion and iteration.
Recursion is a procedure that calls itself until a certain condition (base case) is reached. Recursive solutions can be slower and are subjected to system limitations
Iteration is a procedure that uses loop to repeat a process. Iterative solutions may be harder to implement.
2. Name five algorithms that are commonly implemented by recursion.
Recursive Binary Search
Fibonacci
Merge sort
Quick sort
Factorial
3. When should you use recursion, and when should you avoid recursion? Give examples for each.
An algorithm that deals with results starting with known cases, such as factorials are a perfect use of recursion. Algorithms that pass over a collection of data works better with an iterative solution, for example finding the maximum. Recursion has a tendency to repeat calculations what may introduce a performance issue
4. Compare the recursive and iterative solutions to the three algorithms from the checkpoint (factorial, maximum, and fibonacci). What is similar, and what is different?
For similarities, both procedures lead to the correct solution. For differences, the iterative solutions look cleaner and simpler when used to run through data while recursion looks simpler when calculating factorials .
5. Given a multi-dimensional collection (such as an array) where the number of dimensions is unknown, write a recursive algorithm to count the number of items in the entire collection.
var calculateArray = (array)=>{
let counter =0;
for (var i=0; i <array.length; i++){
if (typeof array[i] === "object"){
counter += calculateArray(array[i]);
}else{
counter++;
}
}
return counter;
}
calculateArray([1,2,3,[1,2,3]]);
6. A palindrome is a word or phrase whose spelling is the same either direction (e.g., racecar). Write a recursive algorithm to determine if a given word or phrase is a palindrome.
var isPalindrome= (str)=>{
if(str.length === 0){
return true;
}
if (str[0] !== str[str.length-1]){
return false;
}
return isPalindrome(str.slice(1,str.length-1))
}
isPalindrome(‘racecar’)
7. Google Easter Egg: Google the term "recursion". Google will prompt you with "Did you mean: recursion". Explain why this behaviour exhibits properties of recursion.
This behaviour exhibits properties of ‘recursion’ as it plays on the fact that a recursive function calls itself. In the same light, when ‘recursion’ is typed on google, it is a self-reference to itself. It is akin to this saying "To understand recursion, you must first understand recursion."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment