Skip to content

Instantly share code, notes, and snippets.

View RahmatSaeedi's full-sized avatar

Rahmat Saeedi RahmatSaeedi

View GitHub Profile
@RahmatSaeedi
RahmatSaeedi / absoluteValuesSumMinimization.js
Created May 10, 2021 14:34
CodeSignal - Arcade - Intro - JS - absoluteValuesSumMinimization
function absoluteValuesSumMinimization(a) {
let sum = a.map( i => distanceCalculator(a, i));
let sumMin = sum.slice().sort((a,b) => a-b)[0];
return a[sum.indexOf(sumMin)];
}
function distanceCalculator(a, val){
return a.map(i => Math.abs(i-val)).reduce((sum, num) => sum + num);
@RahmatSaeedi
RahmatSaeedi / depositProfit.js
Created May 10, 2021 14:33
CodeSignal - Arcade - Intro - JS - depositProfit
function depositProfit(deposit, rate, threshold) {
return Math.ceil(Math.log(parseFloat(threshold)/parseFloat(deposit)) / Math.log(1 + parseFloat(rate)/100.0));
}
@RahmatSaeedi
RahmatSaeedi / Circle of Numbers.js
Created May 10, 2021 14:33
CodeSignal - Arcade - Intro - JS - Circle of Numbers
function circleOfNumbers(n, firstNumber) {
return (n/2+firstNumber) % n;
}
@RahmatSaeedi
RahmatSaeedi / chessBoardCellColor.js
Created May 10, 2021 14:32
CodeSignal - Arcade - Intro - JS - chessBoardCellColor
function chessBoardCellColor(cell1, cell2) {
return (cell1.charCodeAt(0) - cell2.charCodeAt(0) + cell1.charCodeAt(1) - cell2.charCodeAt(1)) %2 == 0;
}
@RahmatSaeedi
RahmatSaeedi / alphabeticShift.js
Created May 10, 2021 14:32
CodeSignal - Arcade - Intro - JS - alphabeticShift
function alphabeticShift(inputString) {
return inputString.split("").map( c => {
if(c == 'z')
c = 'a';
else
c = String.fromCharCode(c.charCodeAt(0) + 1);
return c;
}).join("");
}
@RahmatSaeedi
RahmatSaeedi / variableName.js
Created May 10, 2021 14:31
CodeSignal - Arcade - Intro - JS - variableName
function variableName(name) {
return name.match(/^[A-Za-z_][A-Za-z_0-9]*$/) != null ? true : false;
}
@RahmatSaeedi
RahmatSaeedi / evenDigitsOnly.js
Created May 10, 2021 14:30
CodeSignal - Arcade - Intro - JS - evenDigitsOnly
function evenDigitsOnly(n) {
return n.toString().split("").every((n) => (parseInt(n) % 2 == 0));
}
@RahmatSaeedi
RahmatSaeedi / Array Replace.js
Created May 10, 2021 14:30
CodeSignal - Arcade - Intro - JS - Array Replace
function arrayReplace(inputArray, elemToReplace, substitutionElem) {
return inputArray.map(i => i==elemToReplace? substitutionElem:i);
}
@RahmatSaeedi
RahmatSaeedi / Minesweeper.js
Created May 10, 2021 14:28
CodeSignal - Arcade - Intro - JS - Minesweeper
function minesweeper(matrix) {
let height = matrix.length;
let width = matrix[0].length;
let outArray = Array.from(Array(height), () => new Array(width));
let mines = 0;
for(let i = 0; i < height; i++) {
for(let j = 0; j < width; j++) {
@RahmatSaeedi
RahmatSaeedi / Box Blur.js
Created May 10, 2021 14:28
CodeSignal - Arcade - Intro - JS - Box Blur
function boxBlur(image) {
let bluredImage = Array.from(Array(image.length-2), () => new Array(image[0].length-2));
for(let i = 1; i < image.length-1; i++) {
for(let j = 1; j < image[i].length-1; j++){
bluredImage[i-1][j-1] = Math.floor((
image[i-1][j-1] + image[i-1][j] + image[i-1][j+1] +
image[i][j-1] + image[i][j] + image[i][j+1] +
image[i+1][j-1] + image[i+1][j] + image[i+1][j+1]