Skip to content

Instantly share code, notes, and snippets.

@ChrisDobby
ChrisDobby / fractionMath.ts
Last active March 13, 2023 08:24
Write a function that can do the 4 basic operations (add, subtract, multiply and divide) on two fractions. Return the most simplified form of the result.
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 }
}
@ChrisDobby
ChrisDobby / scramble.ts
Created March 6, 2023 17:11
Write a function that takes an input sentence, and mixes up the insides of words (anything longer than 3 letters)
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))
@ChrisDobby
ChrisDobby / repeatedGroups.ts
Created February 28, 2023 14:13
Given a list of numbers, return all groups of repeating consecutive numbers
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)
@ChrisDobby
ChrisDobby / numBalanced.ts
Created February 20, 2023 08:12
Given a string of parenthesis, return the number of parenthesis you need to add to the string in order for it to be balanced.
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)
}
@ChrisDobby
ChrisDobby / colNamesToNumbers.ts
Created February 6, 2023 19:08
Spreadsheet programs often use the A1 Reference Style to refer to columns. Given a column name in this style, return its column number.
const colNamesToNumbers = (name: string) => name.split('').reduce((acc, ch) => acc * 26 + ch.charCodeAt(0) - 64, 0)
@ChrisDobby
ChrisDobby / generateArrays.ts
Created January 31, 2023 19:19
Given a positive integer, generate an array in which every element is an array that goes from 1 to the index of that array.
const generateArrays = (n: number) => Array.from({ length: n }, (_, index) => Array.from({ length: index + 1 }, (_, innerIndex) => innerIndex + 1))
@ChrisDobby
ChrisDobby / missingBits.ts
Created January 25, 2023 09:19
You are given a list of positive integers which represents some range of integers which has been truncated. Find the missing bits, insert ellipses to show that that part has been truncated, and print it. If the consecutive values differ by exactly two, then insert the missing value.
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(',')}]`
@ChrisDobby
ChrisDobby / sumEveryOther.ts
Created January 9, 2023 07:41
Given a number, sum every second digit in that number.
const sumEveryOther = (num: number) =>
num
.toString()
.split('')
.reduce((total, digit, index) => ((index + 1) % 2 === 0 ? total + parseInt(digit) : total), 0)
@ChrisDobby
ChrisDobby / maxSubArray.ts
Last active January 2, 2023 10:10
Given an array of integers arr and an integer n, return a subarray of arr of length n where the sum is the largest. Make sure you maintain the order of the original array
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]
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,
})
)