Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const shortestWordLength = (sentence) => {
- return Math.min(
- ...sentence.trim()
- .split(' ')
- .map(item => item.length)
- );
- };
- console.log(shortestWordLength("bitcoin take over the world maybe who knows perhaphs"));
- const countAllElements = (arr) => {
- let count = arr.length;
- arr.forEach(item => {
- if(Array.isArray(item)) {
- count += countAllElements(item);
- }
- })
- return count;
- };
- console.log(countAllElements([1, 2, [3, 4, 6, [[], [1]]]]));
- const replaceString = (str) => {
- const charMap = new Map();
- str = str.toLowerCase();
- for(let letter of str) {
- if(charMap.get(letter) !== undefined) {
- charMap.set(letter, ')');
- continue;
- }
- charMap.set(letter, '(');
- }
- return str.split('')
- .map(letter => charMap.get(letter))
- .join('');
- };
- console.log(replaceString('rEceDe'));
- // 3(a)3(b3(c))
- // 2(a2(b2(c)))
- // 1(a2(b)3(c4(d2(e)f)))
- const solve = (str) => {
- const regex = /(\d+)\(([a-z]+)\)/;
- const match = str.match(regex);
- if(!match) {
- return str;
- }
- const [fullMatch, multiplier, subject] = match;
- const replaceWith = subject.repeat(parseInt(multiplier, 10));
- return solve(str.replace(fullMatch, replaceWith));
- };
- console.log(solve('3(ab)'));
- const rangeExtraction = (arr) => {
- for(let i = 0; i < arr.length - 2; i++) {
- let counter;
- for(counter = 2; counter < arr.length - i; counter++) {
- if(arr[i + counter] - arr[i] !== counter) {
- break;
- }
- }
- if(counter > 2) {
- arr.splice(i, counter, `${arr[i]}-${arr[i + counter - 1]}`);
- }
- }
- return arr.join(',');
- };
- console.log(rangeExtraction([-30, -20, -19, -18, -17, -16, -15, -10, 0, 1, 5, 6, 7, 8, 9, 10, 30]))
- const snail = (matrix) => {
- const finalArray = [];
- // directions change with this order
- // starting with {x: 1, y: 0}
- const directionValues = [1, 0, -1, 0];
- const directionIds = {
- x: 3,
- y: 0,
- };
- const current = {
- x: 0,
- y: 0,
- }
- const limits = {
- top: 0,
- left: 0,
- bottom: matrix.length - 1,
- right: matrix.length - 1,
- };
- const nextStep = () => {
- current.x += directionValues[directionIds.x];
- current.y += directionValues[directionIds.y];
- }
- const nextDirection = () => {
- directionIds.x = (directionIds.x + 1) % 4;
- directionIds.y = (directionIds.y + 1) % 4;
- }
- while (limits.top <= limits.bottom && limits.left <= limits.right) {
- finalArray.push(matrix[current.x][current.y]);
- nextStep();
- if (current.y > limits.right) {
- current.y = limits.right;
- current.x = ++limits.top;
- nextDirection();
- }
- if (current.x > limits.bottom) {
- current.x = limits.bottom;
- current.y = --limits.right;
- nextDirection();
- }
- if (current.y < limits.left) {
- current.y = limits.left;
- current.x = --limits.bottom;
- nextDirection();
- }
- if (current.x < limits.top) {
- current.x = limits.top;
- current.y = ++limits.left;
- nextDirection();
- }
- }
- return finalArray;
- };
- console.log(
- snail(
- [
- [1, 2, 3, 4, 5],
- [16, 17, 18, 19, 6],
- [15, 24, 25, 20, 7],
- [14, 23, 22, 21, 8],
- [13, 12 ,11 ,10 ,9],
- ]
- )
- );
- console.log(
- snail(
- [
- [1, 2, 3, 4],
- [12, 13, 14, 5],
- [11, 16, 15, 6],
- [10, 9, 8, 7],
- ]
- )
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement