Skip to content

Commit e221ae7

Browse files
author
Imam Hossain Santho
committed
Added DS & Algo in JS
1 parent 8e8904d commit e221ae7

11 files changed

+414
-0
lines changed

DS & Algo/create-a-queue-class.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*Data Structures: Create a Queue Class */
2+
class Queue{
3+
constructor(){
4+
this.queue = [];
5+
this.back = 0;
6+
this.top = 0;
7+
}
8+
enqueue(val){
9+
this.queue[this.top++] = val;
10+
}
11+
dequeue(){
12+
return this.queue[this.back++];
13+
}
14+
front(){
15+
return this.queue[this.back];
16+
}
17+
size(){
18+
return this.top - this.back;
19+
}
20+
isEmpty(){
21+
return this.back>=this.top;
22+
}
23+
print(){
24+
console.log(this.queue);
25+
}
26+
}
27+
28+
let q = new Queue();
29+
console.log(q.isEmpty()); //true
30+
q.enqueue(20);
31+
console.log(q.isEmpty()); //false
32+
q.enqueue(30);
33+
console.log(q.dequeue()); //20
34+
console.log(q.front()); //30
35+
console.log(q.size());
36+
q.dequeue();
37+
console.log(q.isEmpty()); //true
38+

DS & Algo/create-a-stack-class.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//Data Structures: Create a Stack Class
2+
3+
class Stack{
4+
constructor(){
5+
this.stack = [];
6+
}
7+
print(){
8+
console.log(this.stack);
9+
}
10+
push(val){
11+
return this.stack.push(val);
12+
}
13+
pop(){
14+
return this.stack.pop();
15+
}
16+
peek(){
17+
return this.stack[this.stack.length -1];
18+
}
19+
isEmpty(){
20+
return this.stack.length === 0;
21+
}
22+
clear(){
23+
return (this.stack.length = 0);
24+
}
25+
}
26+
let st = new Stack();
27+
console.log(st);
28+
st.push(10);
29+
st.push(20);
30+
st.push(15);
31+
console.log(st); //{ stack: [ 10, 20, 15 ] }
32+
st.pop();
33+
console.log(st.peek()); //20
34+
st.clear();
35+
console.log(st.isEmpty()); //true
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Algorithms: Find the Symmetric Difference
3+
Create a function that takes two or more arrays and returns an array of the symmetric difference (△ or ⊕) of the provided arrays.
4+
5+
Given two sets (for example set A = {1, 2, 3} and set B = {2, 3, 4}), the mathematical term "symmetric difference" of two sets is the set of elements which are in either of the two sets, but not in both (A △ B = C = {1, 4}). For every additional symmetric difference you take (say on a set D = {2, 3}), you should get the set with elements which are in either of the two the sets but not both (C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}). The resulting array must contain only unique values (no duplicates).
6+
*/
7+
8+
function sym(...args) {
9+
//removing duplicates from each array
10+
args = args.map(item => [...new Set(item)]);
11+
//return intersection set of the two arrays
12+
const reducer = (acc, curr) => [
13+
...acc.filter(item => !curr.includes(item)),
14+
...curr.filter(item=> !acc.includes(item))
15+
];
16+
let result = args.reduce(reducer);
17+
18+
console.log(result);
19+
return result;
20+
}
21+
22+
sym([1, 2, 3], [5, 2, 1, 4, 5]);

DS & Algo/implement-bubble-sort.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*Algorithms: Implement Bubble Sort */
2+
function bubbleSort(arr) {
3+
// change code below this line
4+
for(let i=0; i<arr.length; i++){
5+
for(let j=i+1; j<arr.length; j++){
6+
if(arr[j]<arr[i]){
7+
[arr[j], arr[i]] = [arr[i], arr[j]]; //swap here
8+
}
9+
}
10+
}
11+
console.log(arr);
12+
return arr;
13+
// change code above this line
14+
}
15+
16+
bubbleSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);

DS & Algo/implement-insertion-sort.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Algorithms: Implement Insertion Sort
3+
This method works by building up a sorted array at the beginning of the list.
4+
It begins the sorted array with the first element. Then it inspects the next element and
5+
swaps it backwards into the sorted array until it is in sorted position.
6+
*/
7+
function insertionSort(arr) {
8+
9+
for(let i=1; i<arr.length; i++){
10+
let key = arr[i];
11+
let j = i-1;
12+
while(j>=0 && arr[j]>key){
13+
arr[j+1] = arr[j];
14+
j = j-1;
15+
}
16+
arr[j+1] = key;
17+
}
18+
console.log(arr);
19+
return arr;
20+
21+
}
22+
23+
insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);

DS & Algo/implement-quick-sort.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Algorithms: Implement Quick Sort
3+
4+
The key process in quickSort is partition().
5+
Target of partitions is, given an array and an element x of array as pivot,
6+
put x at its correct position in sorted array and put all smaller elements (smaller than x) before x,
7+
and put all greater elements (greater than x) after x.
8+
All this should be done in linear time.
9+
*/
10+
function partition(arr, left, right){
11+
let pivot = arr[right];
12+
let shift = left-1;
13+
for(let j=left; j<right; j++){
14+
if(arr[j]<pivot){
15+
shift++;
16+
[arr[shift], arr[j]] = [arr[j], arr[shift]];
17+
}
18+
}
19+
[arr[shift+1], arr[right]] = [arr[right], arr[shift+1]];
20+
return shift+1;
21+
}
22+
23+
function quickSort(arr, left=0, right=arr.length-1) {
24+
25+
if(left<right){
26+
/* pi is partitioning index, arr[p] is now
27+
at right place */
28+
let pi = partition(arr, left, right);
29+
quickSort(arr, left, pi-1);
30+
quickSort(arr, pi+1, right);
31+
}
32+
return arr;
33+
}
34+
35+
// test array:
36+
const array = quickSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
37+
38+
console.log(array);
39+
/*
40+
Illustration of partition() :
41+
42+
arr[] = {10, 80, 30, 90, 40, 50, 70}
43+
Indexes: 0 1 2 3 4 5 6
44+
45+
low = 0, high = 6, pivot = arr[h] = 70
46+
Initialize index of smaller element, i = -1
47+
48+
Traverse elements from j = low to high-1
49+
j = 0 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
50+
i = 0
51+
arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j
52+
// are same
53+
54+
j = 1 : Since arr[j] > pivot, do nothing
55+
// No change in i and arr[]
56+
57+
j = 2 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
58+
i = 1
59+
arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30
60+
61+
j = 3 : Since arr[j] > pivot, do nothing
62+
// No change in i and arr[]
63+
64+
j = 4 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
65+
i = 2
66+
arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped
67+
j = 5 : Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j]
68+
i = 3
69+
arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped
70+
71+
We come out of loop because j is now equal to high-1.
72+
Finally we place pivot at correct position by swapping
73+
arr[i+1] and arr[high] (or pivot)
74+
arr[] = {10, 30, 40, 50, 70, 90, 80} // 80 and 70 Swapped
75+
76+
Now 70 is at its correct place. All elements smaller than
77+
70 are before it and all elements greater than 70 are after
78+
it.
79+
*/

DS & Algo/implement-selection-sort.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Algorithms: Implement Selection Sort */
2+
function selectionSort(arr) {
3+
// change code below this line
4+
for(let i=0; i<arr.length; i++){
5+
let Min = i;
6+
for(let j=i+1; j<arr.length; j++){
7+
if(arr[Min]>arr[j]) Min = j;
8+
}
9+
[arr[i], arr[Min]] = [arr[Min], arr[i]];
10+
}
11+
console.log(arr);
12+
return arr;
13+
// change code above this line
14+
}
15+
16+
17+
selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);

DS & Algo/inventory-update.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Algorithms: Inventory Update
3+
Compare and update the inventory stored in a 2D array against a second 2D array of a fresh delivery.
4+
Update the current existing inventory item quantities (in arr1).
5+
If an item cannot be found, add the new item and quantity into the inventory array.
6+
The returned inventory array should be in alphabetical order by item.
7+
*/
8+
9+
function updateInventory(arr1, arr2) {
10+
// All inventory must be accounted for or you're fired!
11+
arr2.forEach(elem => {
12+
let flag = false;
13+
for(let i in arr1){
14+
if(arr1[i][1]===elem[1]){
15+
arr1[i][0] += elem[0];
16+
flag = true;
17+
}
18+
}
19+
if(flag==false){
20+
arr1.push(elem);
21+
}
22+
});
23+
arr1.sort((a,b)=> a[1].localeCompare(b[1]));
24+
console.log(arr1);
25+
return arr1;
26+
}
27+
28+
// Example inventory lists
29+
var curInv = [
30+
[21, "Bowling Ball"],
31+
[2, "Dirty Sock"],
32+
[1, "Hair Pin"],
33+
[5, "Microphone"]
34+
];
35+
36+
var newInv = [
37+
[2, "Hair Pin"],
38+
[3, "Half-Eaten Apple"],
39+
[67, "Bowling Ball"],
40+
[7, "Toothpaste"]
41+
];
42+
43+
updateInventory(curInv, newInv);
44+
45+
46+
/*
47+
The function updateInventory should return an array.
48+
Passed
49+
updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])
50+
should return an array with a length of 6.
51+
Passed
52+
updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])
53+
should return [[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]].
54+
Passed
55+
updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [])
56+
should return [[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]].
57+
Passed
58+
updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])
59+
should return [[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]].
60+
Passed
61+
updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]])
62+
should return [[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]].
63+
*/

DS & Algo/no-repeats-please.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Algorithms: No Repeats Please
3+
Return the number of total permutations of the provided string that don't have repeated consecutive letters. Assume that all characters in the provided string are each unique.
4+
5+
For example, aab should return 2 because it has 6 total permutations (aab, aab, aba, aba, baa, baa), but only 2 of them (aba and aba) don't have the same letter (in this case a) repeating.
6+
*/
7+
8+
function permAlone(str) {
9+
let arr = str.split("");
10+
11+
let result = [];
12+
function permute(arr, l, r){
13+
if(l==r){
14+
result.push(arr.join(""));
15+
}
16+
else{
17+
for(let i=l; i<=r; i++){
18+
[arr[i], arr[l]] = [arr[l], arr[i]]; //swap position
19+
permute(arr, l+1, r); //recursive call
20+
21+
[arr[i], arr[l]] = [arr[l], arr[i]]; //swap back
22+
}
23+
}
24+
};
25+
permute(arr, 0, arr.length-1);
26+
27+
let regex = /(.)\1+/; //matches the same text as most recently matched by the 1st capturing group
28+
29+
result = result.filter(item => !regex.test(item));
30+
31+
return result.length;
32+
}
33+
34+
permAlone('a');
35+
36+
/*
37+
permAlone("aab") should return a number.
38+
Passed
39+
permAlone("aab") should return 2.
40+
Passed
41+
permAlone("aaa") should return 0.
42+
Passed
43+
permAlone("aabb") should return 8.
44+
Passed
45+
permAlone("abcdefa") should return 3600.
46+
Passed
47+
permAlone("abfdefa") should return 2640.
48+
Passed
49+
permAlone("zzzzzzzz") should return 0.
50+
Passed
51+
permAlone("a") should return 1.
52+
Passed
53+
permAlone("aaab") should return 0.
54+
Passed
55+
permAlone("aaabb") should return 12.
56+
*/

0 commit comments

Comments
 (0)