Skip to content

Instantly share code, notes, and snippets.

@FlameWolf
FlameWolf / merge-sort.js
Created January 19, 2024 14:11
MergeSort implementation in JavaScript
function mergeSort(input) {
const merge = (left, right) => {
const merged = [];
while (left.length && right.length) {
if (left[0] < right[0]) {
merged.push(left.shift());
} else {
merged.push(right.shift());
}
}
@FlameWolf
FlameWolf / snail.js
Created January 12, 2024 11:28
Snail Algorithm (CodeWars Challenge)
/*
input:
[
[01, 02, 03, 04, 05, 06],
[07, 08, 09, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30],
[31, 32, 33, 34, 35, 36]
];
@FlameWolf
FlameWolf / getContent.js
Last active May 4, 2024 09:42
Get content from KKS
copy((function() {
const contentParagraphs = [
...document.querySelectorAll("#main > #content > article > .entry-content > p"),
...document.querySelectorAll("div.entry-content > div:not(#widgets-wrap-before-post-content)")
];
return contentParagraphs
.filter(x => x.getAttribute("role") !== "navigation")
.reduce((acc, value) => acc + "\n" + value.textContent, "");
})());
@FlameWolf
FlameWolf / ratios.js
Last active November 29, 2023 11:51
Get the ratios of consecutive elements from an array
[1, 2, 3, 4, 5].reduce((accumulator, current, index, { [index + 1]: next }) => next ? accumulator.concat(current / next) : accumulator, []); // [ 0.5, 0.6666666666666666, 0.75, 0.8 ]
@FlameWolf
FlameWolf / fibonacci.js
Created November 3, 2023 06:38
JavaScript generator function to get Fibonacci numbers
function* fibonacci(max = Number.MAX_SAFE_INTEGER) {
let current = 1;
let previous = 0;
while (current <= max) {
[current, previous] = [previous, current + previous];
if (current <= max) {
yield current;
} else {
return;
}
@FlameWolf
FlameWolf / UnicodeStringSplitRegEx.md
Last active November 6, 2023 14:05
Regular expression to split a Unicode string into approximate constituent graphemes
/\p{L}\p{M}?|\S|\s/gu

Usage:

"മാസങ്ങളിൽ മേടം പ്രധാനം".match(/\p{L}\p{M}?|\S|\s/gu); // ["മാ", "സ", "ങ്", "ങ", "ളി", "ൽ", " ", "മേ", "ടം", " ", "പ്", "ര", "ധാ", "നം"]
@FlameWolf
FlameWolf / BigIntegerExtensions.cs
Created October 19, 2023 13:34
BigInteger.ToBase62String
using System;
using System.Collections.Generic;
using System.Numerics;
public static class BigIntegerExtensions
{
private static readonly char[] digits = new char[]
{
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
@FlameWolf
FlameWolf / getRandomIdString_v3.js
Last active October 26, 2023 05:30
Generate random ID string V3
Object.defineProperty(BigInt.prototype, "toBase62String", {
value: function () {
const base = 62n;
const digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const sign = this < 0n ? "-" : "";
let [result, quotient] = ["", sign ? 0n - this : this];
do {
result = `${digits[Number(quotient % base)]}${result}`;
} while (quotient /= base);
return `${sign}${result}`;
@FlameWolf
FlameWolf / BigInt.prototype.toBase62String.js
Last active October 26, 2023 05:29
Convert a BigInt to a base-62 number representation where the digits include (in the ascending order of value): 0-9, a-z, and A-Z
Object.defineProperty(BigInt.prototype, "toBase62String", {
value: function () {
const base = 62n;
const digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const sign = this < 0n ? "-" : "";
let [result, quotient] = ["", sign ? 0n - this : this];
do {
result = `${digits[Number(quotient % base)]}${result}`;
} while (quotient /= base);
return `${sign}${result}`;
@FlameWolf
FlameWolf / BigInt.prototype.toBase64String.js
Last active October 26, 2023 05:28
Convert a BigInt to a base-64 number representation where the digits include (in the ascending order of value): 0-9, a-z, A-Z, &, and #
Object.defineProperty(BigInt.prototype, "toBase64String", {
value: function () {
const base = 64n;
const digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#";
const sign = this < 0n ? "-" : "";
let [result, quotient] = ["", sign ? 0n - this : this];
do {
result = `${digits[Number(quotient % base)]}${result}`;
} while (quotient /= base);
return `${sign}${result}`;