This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type FractionString = `${number}/${number}` | |
type Fraction = { numerator: number; denominator: number } | |
type Operation = 'add' | 'subtract' | 'multiply' | 'divide' | |
const simplify = ({ numerator, denominator }: Fraction) => { | |
const commonDivisor = (n: number, d: number) => (d === 0 ? n : commonDivisor(d, n % d)) | |
const divisor = commonDivisor(numerator, denominator) | |
return { numerator: numerator / divisor, denominator: denominator / divisor } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const scrambleLetters = (letters: string) => | |
letters | |
.split('') | |
.sort(() => Math.random() - 0.5) | |
.join('') | |
const scramble = (str: string) => | |
str | |
.split(' ') | |
.map(word => (word.length > 3 ? `${word[0]}${scrambleLetters(word.slice(1, word.length - 1))}${word[word.length - 1]}` : word)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const repeatedGroups = (numbers: number[]) => | |
numbers | |
.reduce<number[][]>( | |
(acc, num) => (acc.length === 0 || !acc[acc.length - 1].includes(num) ? [...acc, [num]] : [...acc.slice(0, acc.length - 1), [...acc[acc.length - 1], num]]), | |
[] | |
) | |
.filter(nums => nums.length > 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const numBalanced = (str: string) => { | |
const { open, closed } = str.split('').reduce(({ open, closed }, s) => ({ open: open + (s === '(' ? 1 : 0), closed: closed + (s === ')' ? 1 : 0) }), { open: 0, closed: 0 }) | |
return Math.abs(open - closed) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const colNamesToNumbers = (name: string) => name.split('').reduce((acc, ch) => acc * 26 + ch.charCodeAt(0) - 64, 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const generateArrays = (n: number) => Array.from({ length: n }, (_, index) => Array.from({ length: index + 1 }, (_, innerIndex) => innerIndex + 1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fillGap = (num: number, prev: number) => (num - prev === 2 ? `${num - 1},${num}` : `...,${num}`) | |
const missingBits = (numbers: number[]) => | |
`[${numbers | |
.map((num, index, arr) => ({ num, prev: arr[index - 1] })) | |
.map(({ num, prev }) => (num - prev === 1 ? num : fillGap(num, prev))) | |
.join(',')}]` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sumEveryOther = (num: number) => | |
num | |
.toString() | |
.split('') | |
.reduce((total, digit, index) => ((index + 1) % 2 === 0 ? total + parseInt(digit) : total), 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const maxSubArray = (arr: number[], len: number) => | |
arr.length < len | |
? [] | |
: arr | |
.map((_, index, a) => a.slice(index, index + len)) | |
.filter(a => a.length === len) | |
.sort((arr1, arr2) => Math.max(...arr2) - Math.max(...arr1))[0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const addZerosCount = ({ str, zeroCount }: { str: string; zeroCount: number }) => `${str}${zeroCount || ''}` | |
const replaceZeros = (strIncludingZeros: string) => | |
addZerosCount( | |
strIncludingZeros.split('').reduce((acc, ch) => (ch === '0' ? { ...acc, zeroCount: (acc.zeroCount += 1) } : { str: `${addZerosCount(acc)}${ch}`, zeroCount: 0 }), { | |
str: '', | |
zeroCount: 0, | |
}) | |
) |
NewerOlder