generated from pesto-students/PestoPlus
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Week 3 -> week 3 exercise with the details #4
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
59fd64f
week_3: added the exercise_3.1 final
vd89 f2c8849
week_3: added the exercise_3.2 final
vd89 6c81ab5
week_3: added the exercise_3.3 final
vd89 dd3b7da
week_3: added the exercise_3.4 final
vd89 d344171
week_3: Updated the comments solution and the output
vd89 1ffbd8b
week_3: with different conditions
vd89 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
# Week - 3 Assignment |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Exercise 3.1 | ||
|
||
## JS:Exercise 3.1: | ||
- Create a memoize function that remembers previous inputs and stores them in cache so that it won’t have to compute the same inputs more than once. | ||
- The function will take an unspecified number of integer inputs and a reducer method.(1.5hours) | ||
|
||
### Example: | ||
``` | ||
// Given reducer method: | ||
function add(a,b){ | ||
return a+b | ||
} | ||
//Create a method called memoize such that | ||
|
||
const memoizeAdd=memoize(add) | ||
//then calling... | ||
memoizeAdd(100,100); //returns 200 | ||
memoizeAdd(100) ;//returns 100 | ||
memoizeAdd(100,200) //returns 300 | ||
memoizeAdd(100,100) //returns 200 without computing | ||
``` | ||
|
||
### Guidelines: | ||
1. The memozie function should be written from scratch. | ||
2. 3rd party libraries such as loaddash or underscore should not have been used. | ||
3. The function should carry a name which should denote the functionality of it. | ||
4. The function should be able to take n number of arguments. | ||
|
||
### Outcomes: | ||
1. Understanding how caching works. | ||
2. Why is it important?3.The problem memoization solves. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// create memory function | ||
|
||
const { log, time,timeEnd } = require('console') | ||
|
||
const memorize = (_func) => { | ||
const cache = new Map() | ||
return (...args) => { | ||
const key = args.toString(); | ||
if (cache.has(key)) { | ||
return cache.get(key); | ||
} | ||
cache.set(key, _func(...args)); | ||
log(`Printing Cache`, cache); | ||
return cache.get(key); | ||
} | ||
} | ||
|
||
const add = (a, b) => { | ||
log(`Function called for Operation ${a + b}`) | ||
return a + b | ||
} | ||
|
||
const memorizeAdd = memorize(add) | ||
time("process_1"); | ||
memorizeAdd(100,200); | ||
timeEnd("process_1"); | ||
time("process_2"); | ||
memorizeAdd(100,100); | ||
timeEnd("process_2"); | ||
time("process_3"); | ||
memorizeAdd(100,100); | ||
timeEnd("process_3"); | ||
time("process_4"); | ||
memorizeAdd(100,200); | ||
timeEnd("process_4"); | ||
|
||
/* | ||
Function called for Operation 300 | ||
Printing Cache Map(1) { '100,200' => 300 } | ||
process_1: 4.671ms | ||
Function called for Operation 200 | ||
Printing Cache Map(2) { '100,200' => 300, '100,100' => 200 } | ||
process_2: 0.244ms | ||
process_3: 0.007ms | ||
process_4: 0.013ms | ||
*/ | ||
|
||
time("process_1"); | ||
memorizeAdd(200,100); | ||
timeEnd("process_1"); | ||
time("process_2"); | ||
memorizeAdd(100,100); | ||
timeEnd("process_2"); | ||
time("process_3"); | ||
memorizeAdd(100,100); | ||
timeEnd("process_3"); | ||
time("process_4"); | ||
memorizeAdd(200,100); | ||
timeEnd("process_4"); | ||
time("process_5"); | ||
memorizeAdd(200,100); | ||
timeEnd("process_5"); | ||
|
||
|
||
/* | ||
Function called for Operation 300 | ||
Printing Cache Map(1) { '200,100' => 300 } | ||
process_1: 4.955ms | ||
Function called for Operation 200 | ||
Printing Cache Map(2) { '200,100' => 300, '100,100' => 200 } | ||
process_2: 0.351ms | ||
process_3: 0.008ms | ||
process_4: 0.007ms | ||
process_5: 0.01ms | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
## Exercise 3.2: | ||
Create 3 simple functions where call, bind and apply are used. The intention of this exercise isto understand how they work and their differences.(0.5 hours) | ||
|
||
### Guidelines: | ||
1. The candidate should be able to explain what call, bind and apply in JS are and itsdifferences. | ||
2. Using live examples the candidate should be able to differentiate between them. | ||
### Outcome: | ||
1. Call, Bind, Apply are very important JS concepts. | ||
2. This exercise should help understand them and also understand the differences betweenthem. | ||
3. Writing examples for the same will help the candidate have a better understanding ofwhen and how they are used in real world scenarios |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const { log } = require('console') | ||
|
||
const pokemon = { | ||
firstName: 'Pika', | ||
lastName: 'Chu', | ||
getPokeName: function () { | ||
return this.firstName + " " + this.lastName; | ||
} | ||
} | ||
|
||
// make sure to use the old function method else will give an error | ||
const pokemonName = function(snack = '', hobby = '') { | ||
log(this.getPokeName() + ' I choose You!...........!!!') | ||
log(this.getPokeName() + ' loves ' + snack + ' and ' + hobby) | ||
} | ||
|
||
// The example of bind Method in the function Domain | ||
|
||
const logPokemon = pokemonName.bind(pokemon) | ||
|
||
// logPokemon() // Pika Chu I choose You!...........!!! | ||
// logPokemon('sushi' , 'algorithms') // Pika Chu loves sushi and algorithms | ||
|
||
|
||
// The example of call() and apply() | ||
|
||
pokemonName.call(pokemon, 'sushi', 'algorithms') | ||
pokemonName.apply(pokemon, ['sushi', 'algorithms']) | ||
|
||
/* | ||
Pika Chu I choose You!...........!!! | ||
Pika Chu loves sushi and algorithms | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
## Exercise 3.3: | ||
1. What is the output of the below problem and why: [30min] | ||
|
||
``` | ||
function createIncrement() { | ||
let count=0; | ||
function increment() { | ||
count++; | ||
} | ||
let message=`Count is ${count}`; | ||
function log() { | ||
console.log(message); | ||
} | ||
return[increment,log]; | ||
} | ||
const[increment,log] = createIncrement() | ||
|
||
increment(); | ||
increment(); | ||
increment(); | ||
log();// What is logged? | ||
|
||
``` | ||
|
||
### Guidelines: | ||
1. The candidate should be able to explain the code with the desired output. | ||
### Outcome: | ||
1. The candidates will understand how ‘closure’ works in JS. | ||
2. The candidates will understand how ‘encapsulation’ works in JS. | ||
|
||
|
||
### Out put | ||
log() a closure that the message variable. | ||
console.log(message) | ||
- So that's why 'Count is 0' to the console. | ||
- if we do increment many time then we get always a 'Count is 0' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
function createIncrement() { | ||
let count = 0; | ||
// console.log(count); here count is 0 | ||
function increment() { | ||
count++; | ||
/* console.log(count); | ||
here 1 | ||
2 | ||
3 | ||
count is 0 | ||
*/ | ||
} | ||
let message = `Count is ${count}`; // count is 0 | ||
function log() { | ||
console.log(message); // message store the count value at the starting it initialize that value after not change, console.log(message,count); it show count is 0 3 | ||
} | ||
|
||
return [increment, log]; | ||
|
||
} | ||
const [increment, log] = createIncrement(); | ||
increment(); // the first increment increase the count value it print 1 | ||
increment(); // the first increment increase the count value it print 1 | ||
increment(); // the first increment increase the count value it print 1 | ||
log(); // What is logged? | ||
|
||
|
||
/* log() a closure that the message variable. | ||
console.log(message) So that's why 'Count is 0' to the console. | ||
if we do increment many time then we get always a 'Count is 0' | ||
|
||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
## Exercise 3.4 | ||
- Refactor the above stack implementation, using the concept of closure, such that there is noway to access items array outside of createStack() function scope: (2-3 hours) | ||
|
||
``` | ||
function createStack() { | ||
return { | ||
items:[], | ||
push(item) { | ||
this.items.push(item); | ||
}, | ||
pop() { | ||
return this.items.pop(); | ||
} | ||
}; | ||
} | ||
const stack = createStack(); | ||
stack.push(10); | ||
stack.push(5); | ||
stack.pop(); // => 5 | ||
stack.items; // => [10] | ||
stack.items= [10,100,1000]; //Encapsulation broken! | ||
|
||
function createStack() { | ||
// Write your code here... | ||
} | ||
const stack = createStack(); | ||
stack.push(10); | ||
stack.push(5); | ||
stack.pop(); // => 5 | ||
stack.items;// => undefined | ||
|
||
|
||
``` | ||
|
||
### Guidelines: | ||
2. The candidate should be able to refactor the code and get the desired output. | ||
3. The candidate should be able to explain the code why it was not working before. | ||
4. The candidate should be able to explain the code why it is working now. | ||
|
||
### Outcome: | ||
3. The candidates will understand how ‘closure’ works in JS. | ||
4. The candidates will understand how ‘encapsulation’ works in JS. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function createStack() { | ||
const items = []; // we have declared the item before return and inside the createStack() | ||
return { | ||
push(item) { | ||
items.push(item); | ||
}, | ||
pop() { | ||
return items.pop(); | ||
} | ||
}; | ||
} | ||
|
||
const stack = createStack(); | ||
stack.push(10); | ||
stack.push(5); | ||
stack.pop(); // => 5 | ||
|
||
stack.items; // => undefined |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 we change the order of parameters as in memorizeAdd(200,100) will it return from cache for the second time? if not we have handle that case as well
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.
With the desired change it looks good