Skip to content

Instantly share code, notes, and snippets.

@basekays
Created November 25, 2018 03:33
Show Gist options
  • Save basekays/f6d1cfbf20a186d8a52816cf695ffc11 to your computer and use it in GitHub Desktop.
Save basekays/f6d1cfbf20a186d8a52816cf695ffc11 to your computer and use it in GitHub Desktop.
// rotate array
const rotateArray = function(array, number) {
let formerPart = array;
formerPart = formerPart.splice(0, number);
let latterPart = array;
return latterPart.concat(formerPart);
}
rotateArray([1, 2, 3, 4, 5], 4); // [5, 1, 2, 3, 4];
// (sum) pair in array
const pairInArray = function(array, target) {
const index = {};
let result = false;
for (let i = 0; i < array.length; i++) {
const difference = target - array[i];
if (difference in index) {
return true;
} else {
index[array[i]] = i;
}
}
return result;
}
pairInArray([1, 2, 3, 4, 5], 7); // true
//common element in arrays
const commonInArrays = function(array1, array2, array3) {
const commons = [];
for (let i = 0; i < array1.length; i++) {
if (array2.includes(array1[i]) && array3.includes(array1[i])) {
commons.push(array1[i]);
}
}
return commons;
}
commonInArrays([1, 5, 10, 20, 40], [6, 7, 20, 80, 100], [3, 4, 15, 20, 30, 70, 80]) // 20
// find duplicate in array
const findDuplicateInArray = function(array) {
const index = {};
let result;
for (let i = 0; i < array.length; i++) {
const target = array[i];
if (target in index) {
index[target].push(i);
} else {
index[target] = [i];
}
}
for (let number in index) {
if (index[number].length > 1) {
result = number;
}
}
return result;
}
findDuplicateInArray([1, 2, 3, 4, 4, 5]); // 4
//hourglass sum
const add = function(array1, array2, array3) {
let sum = 0;
let sums = [];
for (let i = 0; i < 4; i++) {
sum = array1[i] + array1[i+1] + array1[i+2];
sum += array2[i+1];
sum = sum + array3[i] + array3[i+1] + array3[i+2];
sums.push(sum);
}
return sums;
}
const hourglassSum = function(array) {
let sums = [];
for (let i = 0; i < 4; i++) {
const newSum = add(array[i], array[i+1], array[i+2]);
sums = sums.concat(newSum);
}
sums.sort(function(a, b) {return a-b});
return sums[sums.length-1];
}
hourglassSum([
[-9,-9,-9,1,1,1],
[0,-9,0,4,3,2],
[-9,-9,-9,1,2,3],
[0,0,8,6,6,0],
[0,0,0,-2,0,0],
[0,0,1,2,4,0],
]); // 28
//valid parentheses
const parentheses = {
'(': ')',
'{': '}',
'[': ']',
};
const validParentheses = function(string) {
const stack = [];
for (let i = 0; i < string.length; i++) {
if (string[i] in parentheses) {
stack.push(parentheses[string[i]]);
} else {
if (string[i] !== stack.pop()) {
return false;
}
}
}
return stack.length == 0;
}
validParentheses('{}'); //true
//valid parentheses with alphabet
const parentheses = {
'(': ')',
};
const validParenthesesWithAlphabet = function(string) {
const stack = [];
for (let i = 0; i < string.length; i++) {
if (string[i] == '(' || string[i] == ')') {
if (string[i] in parentheses) {
stack.push(parentheses[string[i]]);
} else {
if (string[i] !== stack.pop()) {
return false;
}
}
}
}
return stack.length == 0;
}
validParenthesesWithAlphabet('((((aaaa)a)))'); //true
//array flattener (recursive)
const arrayFlattener = function(array, flattenedArray=[]) {
for (let i = 0; i < array.length; i++) {
if (Array.isArray(array[i])) {
arrayFlattener(array[i], flattenedArray);
} else {
flattenedArray.push(array[i]);
}
}
return flattenedArray;
}
arrayFlattener([1, 2, 3, [4, [5, [6, 7, [8]]]]]);
//array flattener (iterative)
const arrayFlattener = function(array, flattenedArray=[]) {
while (array.length) {
const target = array.shift();
if (Array.isArray(target)) {
array = array.concat(target);
} else {
flattenedArray.push(target);
}
}
return flattenedArray;
}
arrayFlattener([1, 2, 3, [4, [5, [6, 7, [8]]]]]);
//roman to int
const index = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
const romanToInt = function(string) {
let int = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == 'I' && (string[i+1] == 'V' || string[i+1] == 'X')) {
int = int + (index[string[i+1]] - index[string[i]]);
i++;
} else if (string[i] == 'X' && (string[i+1] == 'L' || string[i+1] == 'C')) {
int = int + (index[string[i+1]] - index[string[i]]);
i++;
} else if (string[i] == 'C' && (string[i+1] == 'D' || string[i+1] == 'M')) {
int = int + (index[string[i+1]] - index[string[i]]);
i++;
} else {
int += index[string[i]];
}
}
return int;
}
romanToInt('IV'); // 4
romanToInt('LVIII'); // 58
romanToInt('CD'); // 400
//reverse a string
const reverseString = function(string) {
let newString = '';
for (let i = string.length - 1; i >= 0; i--) {
newString = newString.concat(string[i]);
}
return newString;
}
reverseString('hello');
//fizzBuzz
const fizzBuzz = function(number) {
const result = [];
for (let i = 1; i <= number; i++) {
if (i%3 == 0 && i%5 == 0) {
result.push('FizzBuzz');
} else if (i%3 == 0) {
result.push('Fizz');
} else if (i%5 == 0) {
result.push('Buzz');
} else {
result.push(i);
}
}
console.log(result);
}
fizzBuzz(15);
//remove zeroes
const moveZero = function(array) {
for (let i = array.length - 1; i >= 0; i--) {
if (array[i] == 0) {
array.splice(i, 1);
array.push(0);
}
}
return array;
}
moveZero([0, 0, 0, 1, 3, 1, 0, 1, 0, 0]);
//reverse integer
const reverseInteger = function(integer) {
let integerSplit = integer.toString().split('');
let reversedString;
if (integerSplit.includes('-')) {
integerSplit.shift();
reversedString = '-'.concat(integerSplit.reverse().join(''));
} else {
reversedString = integerSplit.reverse().join('');
}
return parseInt(reversedString);
}
reverseInteger(123); // 321
reverseInteger(-123); // -321
//minimum stack
const StackWithMinimum = function() {
this.stack = [];
this.minimumValueStack = [];
}
StackWithMinimum.prototype.push = function(number) {
const latestMinimumValue = this.minimumValueStack[this.minimumValueStack.length - 1];
this.stack.push(number);
if (!this.minimumValueStack.length) {
this.minimumValueStack.push(number);
} else {
if (number < latestMinimumValue) {
this.minimumValueStack.push(number);
} else {
this.minimumValueStack.push(latestMinimumValue);
}
}
}
StackWithMinimum.prototype.pop = function() {
this.stack.pop();
this.minimumValueStack.pop();
}
StackWithMinimum.prototype.getMinimumValue = function() {
return this.minimumValueStack[this.minimumValueStack.length - 1];
}
const newStack = new StackWithMinimum();
newStack.push(7);
newStack.push(3);
newStack.push(5);
newStack.push(2);
newStack.getMinimumValue();
newStack.pop();
newStack.getMinimumValue();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment