Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 12153b7

Browse files
committed
added few more
1 parent a2b8643 commit 12153b7

File tree

10 files changed

+161
-0
lines changed

10 files changed

+161
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const firstOccurence = (array, searchElement, low, high) => {
2+
if (low > high) {
3+
return -1;
4+
}
5+
const mid = low + Math.floor((high - low) / 2);
6+
if (
7+
(mid === 0 || array[mid - 1] < searchElement) &&
8+
array[mid] === searchElement
9+
) {
10+
return mid;
11+
}
12+
if (array[mid] < searchElement) {
13+
return firstOccurence(array, searchElement, mid + 1, high);
14+
} else {
15+
return firstOccurence(array, searchElement, low, mid - 1);
16+
}
17+
};
18+
19+
const array = [0, 0, 1, 1, 2, 3, 5, 5];
20+
const searchElement = 5;
21+
console.log(firstOccurence(array, searchElement, 0, array.length - 1));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const justGreaterThan = (element, array, low, high) => {
2+
//
3+
while (low <= high) {
4+
const mid = low + Math.floor((high - low) / 2);
5+
6+
if (element < array[mid]) {
7+
high = mid - 1;
8+
} else {
9+
low = mid + 1;
10+
}
11+
}
12+
return low === array.length ? -1 : array[low];
13+
};
14+
15+
const array = [0, 1, 2, 3, 4, 5, 6];
16+
const element = 6;
17+
console.log(justGreaterThan(element, array, 0, array.length - 1));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const lastOccurence = (array, searchElement, low, high) => {
2+
if (low > high) {
3+
return -1;
4+
}
5+
const mid = low + Math.floor((high - low) / 2);
6+
if (
7+
(mid === array.length - 1 || array[mid + 1] > searchElement) &&
8+
array[mid] === searchElement
9+
) {
10+
return mid;
11+
}
12+
if (array[mid] > searchElement) {
13+
return lastOccurence(array, searchElement, low, mid - 1);
14+
} else {
15+
return lastOccurence(array, searchElement, mid + 1, high);
16+
}
17+
};
18+
19+
const array = [0, 0, 1, 1, 2, 3, 5, 5];
20+
const searchElement = 0;
21+
console.log(lastOccurence(array, searchElement, 0, array.length - 1));

‎tuts/warmup-questions/check-prime.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const isPrime = n => {
2+
for (let i = 2; i < n / 2; i++) {
3+
if (n % i === 0) {
4+
return false;
5+
}
6+
}
7+
return true;
8+
};
9+
10+
console.log(isPrime(237));

‎tuts/warmup-questions/gcd.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//a > b
2+
const gcd = (a, b) => {
3+
if (b < 2) {
4+
return 1;
5+
}
6+
if (a % b === 0) {
7+
return b;
8+
}
9+
const diff = Math.abs(a - b);
10+
11+
return gcd(Math.max(diff, b), Math.min(diff, b));
12+
};
13+
14+
// why this works?
15+
// if you compare this solution with the previous one you can see that
16+
// we were doing gcd(Math.max(diff, b), Math.min(diff, b))
17+
// we want the value of b to converge and have a constraint that b > a
18+
// b > a % b, so it works in our case
19+
const gcdEuclidean = (a, b) => {
20+
if (b === 0) {
21+
return a;
22+
}
23+
24+
return gcd(b, a % b);
25+
};
26+
27+
console.log(gcd(36, 24));

‎tuts/warmup-questions/nth-fibo.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const getFibo = n => {
2+
let a = 0,
3+
b = 1;
4+
5+
if (n === 1) {
6+
return 0;
7+
}
8+
if (n === 2) {
9+
return 1;
10+
}
11+
for (let i = 2; i <= n; i++) {
12+
const temp = b;
13+
b = a + b;
14+
a = temp;
15+
}
16+
17+
return b;
18+
};
19+
20+
console.log(getFibo(6));

‎tuts/warmup-questions/prime-factors.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const primeFactors = n => {
2+
let divisor = 2;
3+
const result = [];
4+
while (n >= 2) {
5+
if (n % divisor === 0) {
6+
result.push(divisor);
7+
n /= divisor;
8+
} else {
9+
divisor++;
10+
}
11+
}
12+
return result;
13+
};
14+
15+
console.log(primeFactors(2));

‎tuts/warmup-questions/prime-numbers.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//sieve of erasthenes
2+
const printPrimes = n => {
3+
const primes = [];
4+
5+
for (let i = 2; i < n; i++) {
6+
primes[i] = true;
7+
}
8+
9+
for (let i = 2; i < n / 2; i++) {
10+
if (primes[i]) {
11+
for (let j = i + i; j <= n; j = i + j) {
12+
primes[j] = false;
13+
}
14+
}
15+
}
16+
return primes.reduce((result, bool, index) => {
17+
if (bool) result.push(index);
18+
return result;
19+
}, []);
20+
};
21+
22+
console.log(printPrimes(10));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const reverse = (str, index = 0) => {
2+
if (index === str.length - 1) {
3+
return str[index];
4+
}
5+
return reverse(str, index + 1) + str[index];
6+
};
7+
console.log(reverse("varun"));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const removeDuplicatesUno

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /