Skip to content

Commit 9eaed9e

Browse files
committed
solved a few and added some new ques
1 parent b5dc350 commit 9eaed9e

File tree

14 files changed

+499
-0
lines changed

14 files changed

+499
-0
lines changed

CoderByte/firstFactorial.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// From CoderByte:
2+
// First Factorial:
3+
// Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it.For example: if num = 4, then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.
4+
// Examples:
5+
// Input: 4
6+
// Output: 24
7+
// Input: 8
8+
// Output: 40320
9+
10+
// Solution:
11+
function FirstFactorial (num) {
12+
// code goes here
13+
if (num < 1 || num > 18 || num !== parseInt(num)) {
14+
return 'Sorry! The range of your input will be between 1 and 18 and will always be an integer.';
15+
} else {
16+
let storeFactorialNum = num;
17+
for (let i = num - 1; i > 0; i--) {
18+
storeFactorialNum *= i;
19+
}
20+
return storeFactorialNum;
21+
}
22+
}
23+
24+
// keep this function call here
25+
console.log(FirstFactorial(11));
26+
27+
// resource of "How to check if a variable is an integer in JavaScript?": https://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript
28+
29+

CoderByte/firstReverse.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// CoderByte!
2+
// First Reverse:
3+
// Have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order.For example: if the input string is "Hello World and Coders" then your program should return the string sredoC dna dlroW olleH.
4+
// Examples
5+
// Input: "coderbyte"
6+
// Output: etybredoc
7+
// Input: "I Love Code"
8+
// Output: edoC evoL I
9+
10+
function FirstReverse (str) {
11+
// code goes here
12+
let store = '';
13+
for (let i = str.length - 1; i >= 0; i--) {
14+
store += str[i];
15+
}
16+
return store;
17+
}
18+
19+
// keep this function call here
20+
console.log(FirstReverse('Hello World and Coders'));
File renamed without changes.
91.5 KB
Binary file not shown.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// /*
2+
// To Subtract the numbers in the array OR to get the integer Sum of the elements in the array, use "reduce()" method.
3+
// It'll give you a result in a single output value.
4+
// Since "reduce()" is a method, so it requires a callback function that add or subtract, (to pass it through) as a param.
5+
// */
6+
7+
// Brute force approach:
8+
const array = [ 1001, 1002, 1003 ];
9+
function sumOfArr (arr) {
10+
let total = 0;
11+
for (let i = 0; i < arr.length; i++) {
12+
total += arr[i];
13+
}
14+
return total;
15+
}
16+
17+
console.log(sumOfArr(array));
18+
// Brute Force Algorithms refers to a programming style that does not include any shortcuts to improve performance, but instead relies on sheer computing power to try all possibilities until the solution to a problem is found. A classic example is the traveling salesman problem (TSP).
19+
20+
// Mild/Applicable approach:
21+
function aVeryBigSum0 (ar) {
22+
let storeArSum = ar.reduce(function (accumulator, currentValue) {
23+
return accumulator + currentValue;
24+
});
25+
return storeArSum;
26+
}
27+
console.log('aVeryBigSum0', aVeryBigSum0([ 13, 5, 7 ]));
28+
29+
// OR
30+
const reducer = function (accumulator, currentValue) {
31+
return accumulator + currentValue;
32+
};
33+
function aVeryBigSum1 (ar) {
34+
let storeArSum = ar.reduce(reducer);
35+
return storeArSum;
36+
}
37+
console.log('aVeryBigSum1', aVeryBigSum1([ 5, 5, 7 ]));
38+
39+
// More short-cut:
40+
const aVeryBigSum2 = (ar) => ar.reduce((a, b) => a + b, 0);
41+
// or ar.reduce((total, currentValue) => total + currentValue, 0);
42+
console.log('aVeryBigSum2', aVeryBigSum2([ 5, 5, 3 ]));
43+
44+
// **The reduce() method executes a reducer function (that you provide) on each element of the array (loop through it), resulting in a single output value.**
45+
46+
// For more: https://www.youtube.com/watch?v=g1C40tDP0Bk
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function compareTriplets (a, b) {
2+
let a1 = 0;
3+
let b1 = 0;
4+
let arr = [];
5+
6+
for (let i = 0; i < a.length; i++) {
7+
if (a[i] > b[i]) {
8+
a1++;
9+
arr[0] = a1;
10+
arr[1] = b1;
11+
} else if (a[i] < b[i]) {
12+
b1++;
13+
arr[0] = a1;
14+
arr[1] = b1;
15+
} else if (a[i] === b[i]) {
16+
a1 += 0;
17+
b1 += 0;
18+
arr[0] = a1;
19+
arr[1] = b1;
20+
}
21+
}
22+
return arr;
23+
}
24+
25+
compareTriplets([ 1, 2, 3 ], [ 1, 2, 3 ]);
80.1 KB
Binary file not shown.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// 3D Matrix/Array -> perfect square.
2+
// My Solution:
3+
function diagonalDifference0 (arr) {
4+
let n = arr.length;
5+
let diff = 0;
6+
for (let i = 0; i < (n - 1) / 2; i++) {
7+
let leftDSum = 0;
8+
let rightDSum = 0;
9+
leftDSum += arr[i][i] + arr[n - 2 - i][n - 2 - i] + arr[n - 1 - i][n - 1 - i];
10+
rightDSum += arr[i][n - 1 - i] + arr[n - 2 - i][n - 2 - i] + arr[n - 1 - i][i];
11+
diff += leftDSum - rightDSum;
12+
}
13+
return Math.abs(diff);
14+
}
15+
console.log(diagonalDifference0([ [ 11, 2, 4 ], [ 4, 5, 6 ], [ 10, 8, -12 ] ]));
16+
17+
// Dissection:
18+
function diagonalDifference1 (arr) {
19+
let n = arr.length;
20+
let diff = 0;
21+
// here, (n-1)/2 = (3-1)/2 = 2/2 = 1. So, i < 1. And that means, this loop runs only once while i = 0 and less than 1, of course.
22+
for (let i = 0; i < (n - 1) / 2; i++) {
23+
let leftDSum = 0;
24+
let rightDSum = 0;
25+
leftDSum += arr[i][i] + arr[n - 2 - i][n - 2 - i] + arr[n - 1 - i][n - 1 - i];
26+
// leftDSum += 11 + 5 + (-12) = 16 - 12 = 4
27+
console.log('leftDSum', leftDSum); // check
28+
rightDSum += arr[i][n - 1 - i] + arr[n - 2 - i][n - 2 - i] + arr[n - 1 - i][i];
29+
// rightDSum += 4 + 5 + 10 = 19
30+
console.log('rightDSum', rightDSum);
31+
diff += leftDSum - rightDSum;
32+
// diff += 4 - 19 = -15
33+
console.log('diff', diff);
34+
}
35+
return Math.abs(diff);
36+
}
37+
console.log(diagonalDifference1([ [ 11, 2, 4 ], [ 4, 5, 6 ], [ 10, 8, -12 ] ]));
38+
39+
// The second one is(Good one):
40+
function diagonalDiff0 (arr) {
41+
let n = arr.length;
42+
let leftDSum = 0;
43+
let rightDSum = 0;
44+
for (let i = 0, j = n - 1; i < n; i++, j--) {
45+
leftDSum += arr[i][i];
46+
rightDSum += arr[j][i];
47+
}
48+
return Math.abs(leftDSum - rightDSum);
49+
}
50+
console.log('diagonalDiff0', diagonalDiff0([ [ 11, 2, 4 ], [ 4, 5, 6 ], [ 10, 8, -12 ] ]));
51+
52+
// Dissecting:
53+
function diagonalDiff1 (arr) {
54+
let n = arr.length;
55+
let leftDSum = 0;
56+
let rightDSum = 0;
57+
for (let i = 0; i < n; i++) {
58+
leftDSum += arr[i][i];
59+
rightDSum += arr[n - 1 - i][i];
60+
}
61+
return Math.abs(leftDSum - rightDSum);
62+
}
63+
console.log('diagonalDiff1', diagonalDiff1([ [ 11, 2, 4 ], [ 4, 5, 6 ], [ 10, 8, -12 ] ]));
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Brute force approach:
2+
// function diagonalDifference(arr) {
3+
// return Math.abs((arr[0][0]+arr[1][1]+arr[2][2]) - (arr[0][2]+arr[1][1]+arr[2][0]))
4+
// }
5+
6+
// console.log(diagonalDifference([[11, 2, 4], [4, 5, 6], [10, 8, - 12]]));
7+
8+
// Good one!
9+
function diagonalDiff (arr) {
10+
let n = arr.length;
11+
let leftDSum = 0;
12+
let rightDSum = 0;
13+
for (let i = 0; i < n; i++) {
14+
leftDSum += arr[i][i];
15+
rightDSum += arr[n - 1 - i][i];
16+
}
17+
return Math.abs(leftDSum - rightDSum);
18+
}
19+
console.log('diagonalDiff', diagonalDiff([ [ 11, 2, 4 ], [ 4, 5, 6 ], [ 10, 8, -12 ] ]));
20+
21+
// 2:
22+
function diagonalDifference1 (arr) {
23+
let n = arr.length;
24+
let diff = 0;
25+
// here, (n-1)/2 = (3-1)/2 = 2/2 = 1. So, i < 1. And that means, this loop runs only once while i = 0 and less than 1, of course.
26+
for (let i = 0; i < (n - 1) / 2; i++) {
27+
let leftDSum = 0;
28+
let rightDSum = 0;
29+
leftDSum += arr[i][i] + arr[n - 2 - i][n - 2 - i] + arr[n - 1 - i][n - 1 - i];
30+
// leftDSum += 11 + 5 + (-12) = 16 - 12 = 4
31+
console.log('leftDSum', leftDSum); // check
32+
rightDSum += arr[i][n - 1 - i] + arr[n - 2 - i][n - 2 - i] + arr[n - 1 - i][i];
33+
// rightDSum += 4 + 5 + 10 = 19
34+
console.log('rightDSum', rightDSum);
35+
diff += leftDSum - rightDSum;
36+
// diff += 4 - 19 = -15
37+
console.log('diff', diff);
38+
}
39+
return Math.abs(diff);
40+
}
41+
console.log('diagonalDifference1', diagonalDifference1([ [ 11, 2, 4 ], [ 4, 5, 6 ], [ 10, 8, -12 ] ]));
42+
43+
// Another one(Don't like it but might be the best one so far):
44+
function diagonalDifference2 (arr) {
45+
let n = arr.length;
46+
let diff = 0;
47+
for (let i = 0; i < n / 2; i++) {
48+
diff += arr[i][i] + arr[n - 1 - i][n - 1 - i] - arr[i][n - 1 - i] - arr[n - 1 - i][i];
49+
}
50+
return Math.abs(diff);
51+
}
52+
console.log(diagonalDifference2([ [ 11, 2, 4 ], [ 4, 5, 6 ], [ 10, 8, -12 ] ]));
53+
54+
// Dissecting:
55+
function diagonalDifference3 (arr) {
56+
let n = arr.length;
57+
let diff = 0;
58+
// in the case of 3D Matrix, n/2 means the loop will run only once.
59+
for (let i = 0; i < n / 2; i++) {
60+
diff += arr[i][i] + arr[n - i - 1][n - i - 1] - arr[i][n - i - 1] - arr[n - i - 1][i];
61+
// diff += (i0 i0 + i2 i2 - i0 i2 - i2 i0);
62+
// diff += (11 + (-12) - 4 - 10);
63+
console.log('Level Zero Confusion', arr[i][i]);
64+
console.log('First Confusion 0', arr[n - i - 1]);
65+
console.log('First Confusion 1', arr[n - i - 1][n - i - 1]);
66+
console.log('Second Confusion', arr[i][n - i - 1]);
67+
console.log('Third Confusion', arr[n - i - 1][i]);
68+
console.log('WHOLE Confusion 0', arr[i][i] + arr[n - i - 1][n - i - 1]);
69+
console.log('WHOLE Confusion 1', arr[i][n - i - 1] - arr[n - i - 1][i]);
70+
console.log('WHOLE Confusion 2', arr[i][i] + arr[n - i - 1][n - i - 1] - arr[i][n - i - 1] - arr[n - i - 1][i]);
71+
}
72+
return Math.abs(diff);
73+
}
74+
console.log(diagonalDifference3([ [ 11, 2, 4 ], [ 4, 5, 6 ], [ 10, 8, -12 ] ]));

0 commit comments

Comments
 (0)