Skip to content

Commit bfe14a4

Browse files
add few execices problem solving part2
1 parent 597fac5 commit bfe14a4

15 files changed

+384
-0
lines changed

FormingAMagicSquare.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Complete the 'formingMagicSquare' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts 2D_INTEGER_ARRAY s as parameter.
6+
*/
7+
8+
function formingMagicSquare(s) {
9+
// Write your code here
10+
const squares = ['618753294', '816357492', '834159672', '438951276', '672159834', '276951438', '294753618', '492357816'];
11+
let min = 100;
12+
let cost = (s, squares) => {
13+
14+
return [...s.map(value => value.join('')).join('')].reduce((target, item, index) => {
15+
target += Math.abs(+item - +squares[index])
16+
17+
return target;
18+
}, 0)
19+
};
20+
21+
squares.forEach((item, index) => {
22+
let value = cost(s, squares[index]);
23+
24+
(value < min) && (min = value);
25+
});
26+
27+
return min;
28+
}

PickingNumbers.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Complete the 'pickingNumbers' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts INTEGER_ARRAY a as parameter.
6+
*/
7+
8+
function pickingNumbers(a) {
9+
// Write your code here
10+
let sortedArray = a.sort(function(a,b){
11+
return(a - b);
12+
});
13+
14+
let currentArray = [];
15+
let longestArray = 0;
16+
let startNumber = 0;
17+
for(let i = 0; i < sortedArray.length; i++){
18+
let result = Math.abs(sortedArray[startNumber] - sortedArray[i]);
19+
if (result <= 1){
20+
currentArray.push(sortedArray[i]);
21+
if(currentArray.length > longestArray){
22+
longestArray = currentArray.length
23+
};
24+
}else {
25+
26+
startNumber = i;
27+
if(currentArray.length > longestArray){
28+
longestArray = currentArray.length
29+
}
30+
currentArray = [];
31+
currentArray.push(sortedArray[i]);
32+
}
33+
34+
}
35+
return longestArray;
36+
}

TheHurdleRace.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Complete the 'hurdleRace' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts following parameters:
6+
* 1. INTEGER k
7+
* 2. INTEGER_ARRAY height
8+
*/
9+
10+
function hurdleRace(k, height) {
11+
// Write your code here
12+
let maxValue = Math.max(...height);
13+
if(k > maxValue){
14+
return 0;
15+
}else{
16+
let res = maxValue - k;
17+
return res;
18+
}
19+
20+
}

backUp.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//additionne tout les éléments d'un tableau
2+
let sumTab = tab.reduce((a, b) => a+b, 0);
3+
4+
//sort un tableau dans l'ordre croissant
5+
let sortTab = tab.sort((a, b) => a-b);
6+
7+
//parcourir un objet
8+
const object = { a: 1, b: 2, c: 3 };
9+
for (const property in object) {
10+
console.log(`${property}: ${object[property]}`);
11+
}
12+
13+
//insert in object
14+
object.key1 = "value1";
15+
//OR
16+
object["key1"] = "value1";
17+
18+
//remove duplicate value in array js
19+
function onlyUnique(value, index, self) {
20+
return self.indexOf(value) === index;
21+
}
22+
var a = ['a', 1, 'a', 2, '1'];
23+
var unique = a.filter(onlyUnique);
24+
25+
//reverse number into string
26+
let a = 12;
27+
let aReverseString = a.toString().split("").reverse().join("");
28+
//transform this string into number
29+
let aNumber = Number(a);

beautifulDaysAtTheMovies.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Complete the 'beautifulDays' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts following parameters:
6+
* 1. INTEGER i
7+
* 2. INTEGER j
8+
* 3. INTEGER k
9+
*/
10+
11+
function beautifulDays(i, j, k) {
12+
// Write your code here
13+
let reverseVal;
14+
let tempResult;
15+
let count = 0;
16+
for(let l=i; l<=j; l++){
17+
reverseVal = Number(l.toString().split("").reverse().join(""));
18+
tempResult = l - reverseVal;
19+
if(tempResult%k == 0){
20+
count++;
21+
}else{
22+
continue;
23+
}
24+
}
25+
return count;
26+
}

catsAndMouse.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Complete the catAndMouse function below.
2+
function catAndMouse(x, y, z) {
3+
let resB = 0;
4+
let resA = 0;
5+
resA = Math.abs(z - x);
6+
resB = Math.abs(z - y);
7+
if(resA > resB){
8+
return 'Cat B';
9+
}else if(resA < resB){
10+
return 'Cat A';
11+
}else if(resA == resB){
12+
return 'Mouse C';
13+
}
14+
15+
}

circularArrayRotation.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Complete the 'circularArrayRotation' function below.
3+
*
4+
* The function is expected to return an INTEGER_ARRAY.
5+
* The function accepts following parameters:
6+
* 1. INTEGER_ARRAY a
7+
* 2. INTEGER k
8+
* 3. INTEGER_ARRAY queries
9+
*/
10+
11+
function circularArrayRotation(a, k, queries) {
12+
// Write your code here
13+
return queries.map(value => a.reduce((target, item, index) => {
14+
let focus = (index + k) % a.length;
15+
target[focus] = item;
16+
17+
return target;
18+
}, [])[value]);
19+
}

climbingLeaderboard.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Complete the 'climbingLeaderboard' function below.
3+
*
4+
* The function is expected to return an INTEGER_ARRAY.
5+
* The function accepts following parameters:
6+
* 1. INTEGER_ARRAY ranked
7+
* 2. INTEGER_ARRAY player
8+
*/
9+
10+
function climbingLeaderboard(ranked, player) {
11+
// Write your code here
12+
let temp=0;
13+
let res = [];
14+
15+
function onlyUnique(value, index, self) {
16+
return self.indexOf(value) === index;
17+
}
18+
let tab = ranked.filter(onlyUnique);
19+
20+
for(let i=0; i<player.length; i++){
21+
for(let j=0; j<tab.length; j++){
22+
if(player[i] == tab[j]){
23+
temp=tab.indexOf(tab[j]) +1;
24+
res.push(temp);
25+
break;
26+
}else if(player[i] >= tab[0]){
27+
temp = 1;
28+
res.push(temp);
29+
break;
30+
}else if(player[i] <= tab[tab.length -1]){
31+
temp = tab.length +1;
32+
res.push(temp);
33+
break;
34+
}else if(player[i] < tab[j] && player[i] > tab[j+1]){
35+
temp=tab.indexOf(tab[j+1]) +1;
36+
res.push(temp);
37+
break;
38+
}
39+
}
40+
}
41+
return res;
42+
}

countingValleys.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Complete the 'countingValleys' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts following parameters:
6+
* 1. INTEGER steps
7+
* 2. STRING path
8+
*/
9+
10+
function countingValleys(steps, path) {
11+
// Write your code here
12+
let point = 0;
13+
let tempFin = -1;
14+
let tempDebut = 0;
15+
let resTemp;
16+
let count = 0;
17+
for(let i=0; i<path.length; i++){
18+
if(path[i] == 'U'){
19+
point++;
20+
}
21+
if(path[i] == 'D'){
22+
point--;
23+
}
24+
if(point == 0){
25+
tempDebut = tempFin+1;
26+
tempFin = i;
27+
resTemp = path.substring(tempDebut, tempFin + 1);
28+
console.log(resTemp);
29+
if(resTemp[0] == 'D'){
30+
count++;
31+
}else continue;
32+
}
33+
}
34+
return count;
35+
}

designerPdfViewer.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Complete the 'designerPdfViewer' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts following parameters:
6+
* 1. INTEGER_ARRAY h
7+
* 2. STRING word
8+
*/
9+
10+
function designerPdfViewer(h, word) {
11+
// Write your code here
12+
word.toLowerCase();
13+
let wordLength = word.length;
14+
let index=0;
15+
let val=0;
16+
let tempTab = [];
17+
let valMax = 0;
18+
let res = 0;
19+
let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
20+
for(let i=0; i<word.length; i++){
21+
index = alphabet.indexOf(word[i]);
22+
val = h[index];
23+
tempTab.push(val);
24+
}
25+
valMax = Math.max(...tempTab);
26+
res = valMax * wordLength;
27+
return res;
28+
}

electronicsShop.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Complete the getMoneySpent function below.
3+
*/
4+
function getMoneySpent(keyboards, drives, b) {
5+
/*
6+
* Write your code here.
7+
*/
8+
let tempTab = [];
9+
let temp;
10+
for(let i=0; i<keyboards.length; i++){
11+
if(keyboards[i] >= b){
12+
continue;
13+
}
14+
for(let j=0; j<drives.length; j++){
15+
if(drives[j] >= b){
16+
continue;
17+
}
18+
temp = keyboards[i] + drives[j];
19+
if(temp <= b){
20+
tempTab.push(temp);
21+
}
22+
}
23+
}
24+
if(tempTab.length == 0)
25+
{
26+
return -1;
27+
}
28+
return Math.max(...tempTab);
29+
}

saveThePrisoner.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Complete the 'saveThePrisoner' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts following parameters:
6+
* 1. INTEGER n
7+
* 2. INTEGER m
8+
* 3. INTEGER s
9+
*/
10+
//n: number of prisoner
11+
//m: number of candy
12+
//s: the position where to start distribution
13+
14+
function saveThePrisoner(n, m, s) {
15+
// Write your code here
16+
let lastPrisoner = (s + m - 1) % n;
17+
18+
return lastPrisoner === 0 ? n : lastPrisoner;
19+
}

sequenceEquation.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Complete the 'permutationEquation' function below.
3+
*
4+
* The function is expected to return an INTEGER_ARRAY.
5+
* The function accepts INTEGER_ARRAY p as parameter.
6+
*/
7+
8+
function permutationEquation(p) {
9+
// Write your code here
10+
let temp;
11+
let res;
12+
let resTab = [];
13+
for(let i=1; i<=p.length; i++){
14+
temp = p.indexOf(i) +1;
15+
res = p.indexOf(temp) + 1;
16+
resTab.push(res);
17+
}
18+
return resTab;
19+
}

utopianTree.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Complete the 'utopianTree' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts INTEGER n as parameter.
6+
*/
7+
8+
function utopianTree(n) {
9+
// Write your code here
10+
let k=1;
11+
for(let i=1; i<=n; i++){
12+
if(i%2 == 0){
13+
k+=1;
14+
}else{
15+
k*=2;
16+
}
17+
}
18+
return k;
19+
}

0 commit comments

Comments
 (0)