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

Week 3 -> week 3 exercise with the details #4

Merged
merged 6 commits into from
Aug 17, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Week-3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

# Week - 3 Assignment
1 change: 0 additions & 1 deletion Week-3/Week-3

This file was deleted.

31 changes: 31 additions & 0 deletions Week-3/exercise_3.1/Readme.md
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.
75 changes: 75 additions & 0 deletions Week-3/exercise_3.1/index.js
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);

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

Copy link
Collaborator Author

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

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
*/
10 changes: 10 additions & 0 deletions Week-3/exercise_3.2/Readme.md
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
33 changes: 33 additions & 0 deletions Week-3/exercise_3.2/index.js
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
*/
38 changes: 38 additions & 0 deletions Week-3/exercise_3.3/Readme.md
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'

33 changes: 33 additions & 0 deletions Week-3/exercise_3.3/index.js
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'

*/

42 changes: 42 additions & 0 deletions Week-3/exercise_3.4/Readme.md
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.
18 changes: 18 additions & 0 deletions Week-3/exercise_3.4/index.js
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