K

@kenbitanga

JS: Implement Foldl (Reduce Left) - Recursive

NodeJS
2 years ago
// implement foldl (reduceLeft) function foldl(arr, f, acc) { if (arr.length === 0) return acc const [x, ...xs] = arr return foldl(xs, f, f(acc, x)) } # driver code arr = [1,2,3,4,5]

Python: Implement Foldl

Python
2 years ago
# foldl implementation def foldl(li, f, acc): if len(li) == 0: return acc x, *xs = li return foldl(xs, f, f(acc, x)) # partition callback function def odd_even(acc, x): a, b = acc

JS: Partition Array using Predicate - Recursive

NodeJS
2 years ago
function partition(arr, f) { return arr.reduce((acc, x) => { const [a, b] = acc if (f(x)) return [[...a, x], b] return [a, [...b, x]] }, [[], []]) } arr = [1,2,3,4,5,6,7] f = (x) => x % 2 == 1

Python: Reverse List - Recursive

Python
2 years ago
def reverse(li): if len(li) == 0: return [] x, *xs = li acc = reverse(xs) return [*acc, x] def reverse2(li): match li: case []:

Python: Partition List - Recursive - 2 Ways

Python
2 years ago
# works from last element to first def partition1(li, f): if len(li) == 0: return [], [] x, *xs = li ys, zs = partition1(xs, f) print(ys, zs) if f(x): return [x, *ys], zs return ys, [x, *zs]

Python: Partition List Based on Predicate

Python
2 years ago
def partition(li, f, a=[], b=[]): if len(li) == 0: return a, b x, *xs = li if f(x): return partition(xs, f, [*a, x], [*b]) return partition(xs, f, [*a], [*b, x]) li = [1,2,3,4,5] print(partition(li, lambda x: x % 2))

JS: Partition Array Based on Predicate - Recursive

NodeJS
2 years ago
const partition = (arr, f, a=[], b=[]) => { if (arr.length === 0) return [a, b] const [x, ...xs] = arr return f(x) ? partition(xs, f, [...a, x], [...b]) : partition(xs, f, [...a], [...b, x]) } let xs = [1,2,3,4,5]

JS: Quick Sort, Merge Sort, Insertion Sort - Recursive

NodeJS
2 years ago
// quick sort function qsort(arr) { if (arr.length === 0) return [] const [pivot, ...tail] = arr const [smaller, bigger] = partition(tail, x => x < pivot) return [...qsort(smaller), pivot, ...qsort(bigger)] // helper function function partition(arr, f, a=[], b=[]) { if (arr.length === 0) return [a, b]

Python: Insertion Sort - Recursive - Pattern Matching

Python
2 years ago
def insert(x, li): match li: case []: return [x] case [hd, *tl]: if x < hd: return [x, *li] return [hd, *insert(x, tl)] def isort(li): match li: case []: return []

Python: Insertion Sort - Recursive

Python
2 years ago
def insert(x, li): if not len(li): return [x] hd, *tl = li if x < hd: return [x, *li] return [hd, *insert(x, tl)] def isort(li): if not len(li): return [] hd, *tl = li return insert(hd, isort(tl))

Python: Negative Fibonacci

Python
2 years ago
def fib(n): negative = False # check if n is negative if n < 0: negative = True n = n * -1 # base case: n is 0 or 1 if n <= 1:

Python: Fibonacci - Profiling using timeit.timeit()

Python
2 years ago
import timeit n = 35 # naive recursive def fib(n): if n <= 1: f = n else: f = fib(n-1) + fib(n-2)

Python: Fibonacci - Infinite Generator

Python
2 years ago
# infinite generator def fib(): a, b = 0, 1 while True: yield a a, b = b, a + b # generate first 5 fib numbers fibs = fib() f = [next(fibs) for _ in range(5)]

Go: Fibonacci - For Loop

Go
2 years ago
package main import "fmt" func fib(n int) int { if n <= 1 { return n } a, b := 0, 1

JS: Fibonacci - For Loop

NodeJS
2 years ago
function fib(n) { if (n <= 1) { return n } let [a, b] = [0, 1] for (let k = 0; k < n-1; k++) { ;[a, b] = [b, a + b] }

Python: Fibonacci - For Loop vs While Loop

Python
2 years ago
# for loop version def fib(n): if n <= 1: return n a, b = 0, 1 for _ in range(n-1): a, b = b, a + b return b print([fib(n) for n in range(10)])

Python: Fibonacci - Naive Recursive, Memoized Recursive (DP), Bottom-up Iterative (DP), Generators

Python
2 years ago
# naive recursive def fib(n): if n <= 1: f = n else: f = fib(n-1) + fib(n-2) return f print([fib(n) for n in range(10)])

Python: Fibonacci - Dynamic Programming

Python
2 years ago
# naive recursive def fib(n): if n <= 2: f = 1 else: f = fib(n-1) + fib(n-2) return f print([fib(n) for n in range(1, 10)])

Ruby: Fibonacci Enumerator

Ruby
2 years ago
fibs = Enumerator.new do |yielder| a, b = 0, 1 loop do yielder << a a, b = b, a + b end end puts fibs.take(10).inspect

Python: Profiling functions using timeit

Python
2 years ago
import random import timeit TAX_RATE = 0.08 PRICES = [random.randrange(100) for _ in range(100_000)] def get_price(price): return price * (1 + TAX_RATE) def get_prices_with_map():