Skip to content

Commit bf5a309

Browse files
committed
binary search in js
1 parent 4c04611 commit bf5a309

5 files changed

Lines changed: 279 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
process.stdin.resume();
6+
process.stdin.setEncoding('utf-8');
7+
8+
let inputString = '';
9+
let currentLine = 0;
10+
11+
process.stdin.on('data', inputStdin => {
12+
inputString += inputStdin;
13+
});
14+
15+
process.stdin.on('end', function() {
16+
inputString = inputString.replace(/\s*$/, '')
17+
.split('\n')
18+
.map(str => str.replace(/\s*$/, ''));
19+
20+
main();
21+
});
22+
23+
function readLine() {
24+
return inputString[currentLine++];
25+
}
26+
27+
// Complete the minimumAbsoluteDifference function below.
28+
function minimumAbsoluteDifference(arr) {
29+
let pointer = 0;
30+
let smallestDifference = Math.abs(arr[pointer] - arr[1]);
31+
while (arr.length > pointer) {
32+
for (let i = pointer; i < arr.length-1; i++) {
33+
const difference = Math.abs(arr[pointer] - arr[i + 1]);
34+
if (smallestDifference > difference) {
35+
smallestDifference = difference;
36+
}
37+
}
38+
pointer++;
39+
}
40+
return smallestDifference;
41+
}
42+
43+
function main() {
44+
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
45+
46+
const n = parseInt(readLine(), 10);
47+
48+
const arr = readLine().split(' ').map(arrTemp => parseInt(arrTemp, 10));
49+
50+
const result = minimumAbsoluteDifference(arr);
51+
52+
ws.write(result + '\n');
53+
54+
ws.end();
55+
}

momcenteral/addup/addup.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
3+
function getMax(x, y) {
4+
if (x > y) {
5+
return x;
6+
} else {
7+
return y;
8+
}
9+
}
10+
11+
function argmap(arr1, arr2) {
12+
var arrToReturn = []
13+
var i = 0
14+
var reset = 0
15+
var arr1Length = arr1.length
16+
var arr2Length = arr2.length
17+
// loop will run till the length of array of greater size
18+
while (i < getMax(arr1Length, arr2Length)) {
19+
// verify if i is less than length - 1 for both
20+
// if yes add the sum of values on the index from both array
21+
// if no add the index from the array which has that index
22+
if (i < arr1Length && i < arr2Length) {
23+
arrToReturn.push(arr1[i] + arr2[i])
24+
} else if (i < arr1Length) {
25+
if (reset >= arr2Length) {
26+
reset = 0
27+
}
28+
arrToReturn.push(arr1[i] + arr2[reset])
29+
reset = reset + 1
30+
} else {
31+
if (reset >= arr1Length) {
32+
reset = 0
33+
}
34+
arrToReturn.push(arr1[reset] + arr2[i])
35+
reset = reset + 1
36+
}
37+
i = i + 1
38+
}
39+
// printArray(arrToReturn)
40+
return arrToReturn
41+
}
42+
43+
44+
module.exports = argmap;

momcenteral/addup/test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var argmap = require('./addup');
2+
var assert = require('assert');
3+
4+
//add object to add custom tests
5+
let testSamples = [
6+
{
7+
arr1: [3, 2],
8+
arr2: [4, 3, 2, 9, 10, 12],
9+
output: [7, 5, 5, 11, 13, 14]
10+
},
11+
{
12+
arr1: [1,2],
13+
arr2: [1,2,3,4],
14+
output: [2, 4, 4, 6]
15+
},
16+
{
17+
arr1: [1,2,3,4],
18+
arr2: [5,6,7, 8],
19+
output: [6,8,10,12]
20+
},
21+
{
22+
arr1: [4, 3, 2, 1],
23+
arr2: [2, 1],
24+
output: [6, 4, 4, 2]
25+
},
26+
{
27+
arr1: [4, 3, 2, 9],
28+
arr2: [2, 1],
29+
output: [6, 4, 4, 10]
30+
},
31+
{
32+
arr1: [4, 3, 2, 9, 10, 12],
33+
arr2: [2, 1],
34+
output: [6, 4, 4, 10, 12, 13]
35+
},
36+
{
37+
arr1: [4, 3, 2, 9, 10, 12, 8, -1],
38+
arr2: [2, 1],
39+
output: [6, 4, 4, 10, 12, 13, 10, 0]
40+
},
41+
{
42+
arr1: [4, 3],
43+
arr2: [2, 1],
44+
output: [6, 4]
45+
}
46+
47+
];
48+
49+
//don't modify this
50+
testSamples.forEach((eachSample,index) => {
51+
let output = argmap(eachSample.arr1,eachSample.arr2);
52+
console.log(output, 'output: ')
53+
describe('Function output should be equal to test sample output', function() {
54+
it('Test # ' + index, function() {
55+
assert.deepStrictEqual(output,eachSample.output);
56+
});
57+
});
58+
});
59+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var maxDepth = function(arr) {
2+
if (arr == false || !arr) {
3+
return 0
4+
}
5+
if (arr[1] == false && arr[2] == false) {
6+
return 1
7+
} else {
8+
var l = maxDepth(arr[1])
9+
var r = maxDepth(arr[2])
10+
if (l > r) {
11+
return 1 + l
12+
} else {
13+
return 1 + r
14+
}
15+
}
16+
}
17+
18+
19+
var getLeafCountsAtDepth = function(arr, depth, count, n) {
20+
console.log("getLeafCountsAtDepth", n)
21+
count = count + 1
22+
n = n
23+
if (arr == false || !arr) {
24+
return n
25+
}
26+
if (arr[1] == false && arr[2] == false) {
27+
console.log(count, 'c', depth, 'd')
28+
if (count == depth) {
29+
n = n + 1
30+
console.log(n, 'iiii')
31+
}
32+
return n
33+
} else {
34+
getLeafCountsAtDepth(arr[1], depth, count, n)
35+
getLeafCountsAtDepth(arr[2], depth, count, n)
36+
return n
37+
}
38+
}
39+
let leafNodes = [];
40+
let treeDepth = 0;
41+
42+
let leafCount = (arr, branch) => {
43+
if (!arr || (arr.length && arr.length === 0)) {
44+
return;
45+
}
46+
if (arr[1] === false && arr[2] === false) {
47+
console.log('pushing', branch);
48+
leafNodes.push(branch);
49+
if (treeDepth < branch) {
50+
treeDepth = branch;
51+
}
52+
return;
53+
} else {
54+
branch = branch + 1;
55+
leafCount(arr[1], branch);
56+
leafCount(arr[2], branch);
57+
}
58+
}
59+
60+
const run = (tree1) => {
61+
leafCount(tree1, 1);
62+
console.log(leafNodes, treeDepth)
63+
totalLeaves = 0;
64+
leafNodes.forEach(x => {
65+
if (x === treeDepth) {
66+
totalLeaves ++;
67+
}
68+
});
69+
console.log('totalLeaves', totalLeaves);
70+
return totalLeaves
71+
}
72+
73+
module.exports = {
74+
maxDepth: maxDepth,
75+
getLeafCountsAtDepth: getLeafCountsAtDepth,
76+
run: run
77+
};

momcenteral/depthLeaves/test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var func = require('./depthLeaves');
2+
var assert = require('assert');
3+
// console.log(output.maxDepth, 'maxDepth')
4+
//add object to add custom tests
5+
6+
// 2
7+
// / \
8+
// 5 9
9+
// \
10+
// 7
11+
let testSamples = [
12+
{
13+
arr1: [2, [5, false, [7, false, false]], [9, false, false]],
14+
output: 1
15+
},
16+
{
17+
arr1: [2, [5,[6, false, false], [7, [1, false, false], false]], [8,[9, false, false], [12, [0, false, false]]]],
18+
output: 2
19+
}
20+
];
21+
22+
// 2
23+
// / \
24+
// 5 8
25+
// / \ / \
26+
// 6 7 9 12
27+
// / \
28+
// 1 0
29+
30+
31+
//don't modify this
32+
testSamples.forEach((eachSample, index) => {
33+
let depth = func.maxDepth(eachSample.arr1);
34+
console.log(depth, 'd');
35+
// let output = func.getLeafCountsAtDepth(eachSample.arr1, depth, 0, 0);
36+
// // output = func.run(eachSample.arr1);
37+
38+
// describe('Function output should be equal to test sample output', function () {
39+
// it('Test # ' + index, function () {
40+
// assert.deepStrictEqual(output, eachSample.output);
41+
// });
42+
// });
43+
});
44+

0 commit comments

Comments
 (0)