Skip to content

Instantly share code, notes, and snippets.

@erhanyasar
Last active July 20, 2023 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erhanyasar/0d432f9b8c889fa4381beeaf86b316dc to your computer and use it in GitHub Desktop.
Save erhanyasar/0d432f9b8c889fa4381beeaf86b316dc to your computer and use it in GitHub Desktop.
// 1. Write a function for strings that return a string that's repeating the string
// for a given integer number for instance, given `"hello".repeatify(3)` expression
// should return "hello, hello, hello" as an output
String.prototype.repeatify = function (param) {
let result = "";
for (let i = 0; i < param; i++) result += `, ${this}`;
return result.slice(1).trim();
};
// console.log("hello".repeatify(3));
// 2. Write a function that finds occurences count of a given word as a whole inside given string
const findWholeWordInAPool = (word = "word", pool = "dworsdfwsodrwword") => {
let regexp = new RegExp(word);
return (pool.match(regexp) || []).length;
};
// console.log(findWholeWordInAPool());
// 3. Write a function that finds occurences count of a given word as a seperate letters inside given string
const findOccurencesOfWord = (
word = "word",
pool = "dworsdfwsodrwword"
) => {
let obj = {};
for (let i = 0; i < word.length; i++) obj[word[i]] = 0;
for (let i = 0; i < pool.length; i++) {
for (let j = 0; j < word.length; j++) {
if (pool[i] === word[j]) obj[word[j]] += 1;
}
}
return Object.values(obj).sort((a, b) => a - b)[0];
};
// console.log(findOccurencesOfWord());
// 4. Write a function that return a bool value whether any 3 values of given
// numbers array, include sum of numbers as an element within same array or not
function hasSumOfNumbers(array = [0, 1, 2, -1, 5, -3, 4]) {
for (let i = 0; i < array.length - 2; i++) {
for (let j = i + 1; j < array.length - 1; j++) {
for (let k = j + 1; k < array.length; k++) {
if (array[i] + array[j] + array[k] === 0) return true;
}
}
}
return false;
}
console.log(hasSumOfNumbers());
// O(n^3)
// 5. Given an array of integers, find the pair of adjacent elements that
// has the largest product and return that product. For instance, for a
// numbers array of inputArray = [3, 6, -2, -5, 7, 3], the output should
// be 21 since 7 and 3 produce the largest number.
function adjacentElementsProduct (inputArray) {
let max = 0,
sum = 0,
sums = new Array(),
sortedSums = new Array(),
negativesCount = 0;
for (let i = 0; i < inputArray.length - 1; i++) {
sum = inputArray[i] * inputArray[i + 1];
if (sum > max) max = sum;
else sums.push(sum);
}
if (max === 0) {
sums.forEach((value, index) => {
if (value < 0) negativesCount++;
if (index === sums.length - 1 && negativesCount === sums.length) {
sortedSums = sums.sort((a, b) => a - b);
max = sortedSums[sortedSums.length - 1];
}
})
}
return max;
}
// console.log(adjacentElementsProduct());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment