Skip to content

Commit 2418422

Browse files
committed
Merge branch 'master' of https://github.com/emon535/HackerRank
2 parents 6a5a7ed + fa465bd commit 2418422

File tree

12 files changed

+258
-0
lines changed

12 files changed

+258
-0
lines changed

Algorithm/Graph/problem.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
function findCapitalCity(robber_distance, cityNodes, cityFrom, cityTo, cityWeight) {
3+
let graph = Array(cityNodes).fill(null).map(()=>Array(cityNodes).fill(null));
4+
5+
const points = cityFrom.map(function(cityFromValue, index) {
6+
graph[cityFromValue][cityTo[index]] = cityWeight[index];
7+
console.log("to=>",cityFromValue,"from=>",cityTo[index]);
8+
9+
// graph[cityTo[index]][cityFromValue] = cityWeight[index];
10+
// return [cityFrom, cityTo[index]];
11+
});
12+
13+
console.log(graph,points);
14+
}

Algorithm/Sort/HeapSort.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var array_length;
2+
/* to create MAX array */
3+
function heap_root(input, i) {
4+
var left = 2 * i + 1;
5+
var right = 2 * i + 2;
6+
var max = i;
7+
8+
if (left < array_length && input[left] > input[max]) {
9+
max = left;
10+
}
11+
12+
if (right < array_length && input[right] > input[max]) {
13+
max = right;
14+
}
15+
16+
if (max != i) {
17+
swap(input, i, max);
18+
heap_root(input, max);
19+
}
20+
}
21+
22+
function swap(input, index_A, index_B) {
23+
var temp = input[index_A];
24+
25+
input[index_A] = input[index_B];
26+
input[index_B] = temp;
27+
}
28+
29+
function heapSort(input) {
30+
31+
array_length = input.length;
32+
33+
for (var i = Math.floor(array_length / 2); i >= 0; i -= 1) {
34+
heap_root(input, i);
35+
}
36+
37+
for (i = input.length - 1; i > 0; i--) {
38+
swap(input, 0, i);
39+
array_length--;
40+
41+
42+
heap_root(input, 0);
43+
}
44+
}
45+
46+
var arr = [3, 0, 2, 5, -1, 4, 1];
47+
heapSort(arr);
48+
console.log(arr);

Algorithm/Sort/MergeSort.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
let merge = (left, right) => {
2+
let result = [],
3+
leftlen = left.length,
4+
rightlen = right.length,
5+
l = 0,
6+
r = 0;
7+
8+
while (1 < leftlen && r < rightlen) {
9+
if (left[l] < right[r]) {
10+
result.push(left[l]);
11+
l++;
12+
}
13+
else {
14+
result.push(right[r])
15+
r++;
16+
}
17+
return result.concat(left.slice(l)).concat(right.slice(r));
18+
}
19+
}
20+
21+
let mergeSort = (arr) => {
22+
let length = arr.length;
23+
if (length < 2) {
24+
return arr;
25+
}
26+
27+
let mid = Math.floor(length / 2),
28+
left = arr.slice(0, mid),
29+
right = arr.slice(mid)
30+
31+
return merge(mergeSort(left), mergeSort(right));
32+
};
33+
34+
console.log("Merge and Sorted;", mergeSort([9, 3, 1, 6, 4, 110, 8, 110, 82]))

Algorithm/Sort/QuickSort.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://www.youtube.com/watch?v=7h1s2SojIRw
2+
3+
let swap = (arr, i, j) => {
4+
let tmp = arr[i];
5+
arr[i] = arr[j];
6+
arr[j] = tmp;
7+
};
8+
9+
let partition = (arr, low, high) => {
10+
let q = low, i;
11+
for (i = low; i < high; i++) {
12+
if (arr[i] < arr[high]) {
13+
swap(arr, i, q);
14+
q++;
15+
}
16+
}
17+
swap(arr, i, q);
18+
return q;
19+
};
20+
21+
let quickSort = (arr, low, high) => {
22+
if (low < high) {
23+
let pivot = partition(arr, low, high);
24+
quickSort(arr, low, pivot - 1);
25+
quickSort(arr, pivot + 1, high);
26+
return arr;
27+
}
28+
};

Codewars/AbbrevName.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// https://www.codewars.com/kata/abbreviate-a-two-word-name/javascript
2+
3+
function abbrevName(name) {
4+
return name.split(' ').map(x => x.substr(0, 1).toUpperCase()).join('.');
5+
}

Codewars/CountingDuplicates.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
// https://www.codewars.com/kata/counting-duplicates/train/javascript
3+
4+
function duplicateCount(text) {
5+
//...
6+
text = text.split("").map(v => v.toLowerCase());;
7+
var mapA = text.reduce(function (prev, cur) {
8+
prev[cur] = (prev[cur] || 0) + 1;
9+
return prev;
10+
}, {});
11+
12+
let count = 0;
13+
14+
for (key in mapA) {
15+
if (mapA[key] > 1) {
16+
count++;
17+
}
18+
}
19+
20+
return count;
21+
}

Codewars/camelCaseMethod.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
String.prototype.camelCase = function () {
2+
//your code here
3+
let str = this.valueOf().split(" ").filter(value => {
4+
return Boolean(value);
5+
}).map(value => {
6+
return value.charAt(0).toUpperCase() + value.slice(1);
7+
}).join("");
8+
9+
10+
11+
console.log(str)
12+
return str;
13+
}

Codewars/playingWithDigits.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
// https://www.codewars.com/kata/playing-with-digits/train/javascript
3+
4+
function digPow(n, p) {
5+
// ...
6+
let arrN = []
7+
let checkIntTypeRegex = /^-?[0-9]+$/;
8+
9+
arrN = n.toString().split("");
10+
let val = 0;
11+
12+
13+
arrN.forEach((item, index) => {
14+
console.log(p);
15+
val += Math.pow(item, p)
16+
console.log(val);
17+
p++;
18+
})
19+
20+
return er.test(val / n) ? val / n : -1
21+
22+
// Alternative Solutions:
23+
// var x = arrN.reduce((sum, value, index)=>sum+Math.pow(value. p+index), 0);
24+
}

Codewars/trippleDouble.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
//https://www.codewars.com/kata/triple-trouble-1/train/javascript
3+
4+
function tripledouble(num1, num2) {
5+
//code me
6+
let result = [];
7+
let regex = /(\w)\1{2,}/g;
8+
let matches = num1.toString().match(regex);
9+
if (matches) {
10+
result = matches.map(eachMatch => {
11+
let repeated = eachMatch.toString().split("").slice(0, 2).join("");
12+
let re = new RegExp(repeated, "g");
13+
14+
console.log("num1 = ", num1, "num2 =", num2, "match", eachMatch, "repeated", repeated, "pattern", re);
15+
if (re.test(num2)) {
16+
console.log("check=>", num2, "with", re);
17+
return 1;
18+
19+
}
20+
})
21+
}
22+
23+
result = result.filter(Boolean);
24+
if (result.length == 0) return 0
25+
else return 1;
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
// Complete the compareTriplets function below.
3+
function compareTriplets(a, b) {
4+
5+
let result = []
6+
let alice = 0;
7+
let bob = 0;
8+
9+
a.forEach((aVal, index) => {
10+
console.log(aVal, b[index])
11+
if (aVal > b[index]) {
12+
alice++;
13+
} else if (aVal < b[index]) {
14+
15+
bob++;
16+
}
17+
})
18+
return [alice, bob]
19+
}

0 commit comments

Comments
 (0)