Skip to content

Instantly share code, notes, and snippets.

View tannerdolby's full-sized avatar
🌀
lost in the context

Tanner Dolby tannerdolby

🌀
lost in the context
View GitHub Profile
@tannerdolby
tannerdolby / sort-tags.js
Last active February 10, 2023 19:52
11ty filter for returning a sorted list of tags from collections. Use the it in a template like {{ collections.foo | taglist }} to get the sorted tag list.
eleventyConfig.addFilter("taglist", function(collection) {
const tags = [];
collection.forEach(post => {
tags.push(...post.data.tags);
});
const sorted = [...new Set(tags)].sort((a, b) => a.localeCompare(b));
return sorted;
});
@tannerdolby
tannerdolby / BST.cpp
Last active January 22, 2023 05:24
Binary Search Tree implementation in C++
#include <iostream>
#include <queue>
// A node class representing nodes in a Binary Search Tree
class Node {
public:
int data;
Node *left;
Node *right;
// default constructor
@tannerdolby
tannerdolby / random-rgb.js
Last active January 7, 2022 21:04
Generate 3 random numbers between the inclusive range [0-255] representing an RGB value.
var [r, g, b] = new Array(3).fill(0).map((_, i) => Math.floor(Math.random() * 256);
console.log(r, g, b);
// 11 210 159
@tannerdolby
tannerdolby / bfs.cpp
Last active February 13, 2022 01:13
Simple level-order traversal (BFS) template
// Iterative Approach - traverse the tree level by level performing bfs
// O(n) time and O(n) space where n = number of nodes in the BST
vector<vector<int>> levelOrder(TreeNode* root) {
if (root == NULL) return {};
// list to store each level of nodes
vector<vector<int>> levels;
// queue holding node pointers which we process during the traversal
queue<TreeNode*> q;
// add "enqueue" the root node as that will be the first node we process
q.push(root);
@tannerdolby
tannerdolby / contains-duplicate.cpp
Last active March 18, 2022 05:51
A template function to check a std::vector<T> for duplicate values.
// O(n) time and O(n) space where n = length of the input sequence
template <typename T>
bool containsDuplicate(std::vector<T> &sequence) {
std::unordered_set<T> uset;
for (auto val : sequence) {
if (uset.count(val) > 0) {
return true;
}
uset.insert(val);
}
@tannerdolby
tannerdolby / binary-search.cpp
Last active March 24, 2022 06:30
A template for performing binary search.
// Iterative: O(log(n)) time and O(1) space
// Recursive: O(log(n)) time and O(log(n)) space
template <typename T>
int binarySearchIterative(std::vector<T> arr, T target) {
int left = 0, right = arr.size()-1;
while (left <= right) {
int mid = left + ((right-left) / 2);
if (target == arr[mid]) {
return mid;
@tannerdolby
tannerdolby / ChessCodeToCartesian.java
Last active May 16, 2022 00:56
Generate a mapping of chess code to cartesian coordinates.
package app;
import java.util.Map;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class ChessMapping {
public Map<String, int[]> chessCodeToCartesian;
@tannerdolby
tannerdolby / alphabet.js
Last active January 12, 2023 04:35
Alphabet class containing the english alphabet in both upper and lower casing.
class Alphabet {
constructor() {
this.lowercaseStr = '';
this.uppercaseStr = '';
this.lowercaseArr = [];
this.uppercaseArr = [];
this.letters = this.getAlphabet();
this._init();
}
function getAlphabet(lowercase) {
const alphabet = [];
const start = lowercase ? 65 : 97;
const end = lowercase ? 91 : 123;
for (let i=start, j=0; i < end; i++, j++) {
alphabet[j] = String.fromCharCode(i);
}
return alphabet;
}
@tannerdolby
tannerdolby / writeCiphers.js
Last active January 12, 2023 05:43
Write caesar cipher output from 'rotation-cipher' package to file. My decision to remove `writeCiphers` from the `rotation-cipher` package was because I wanted users to use the cipher utility on their own terms rather than forcing the package only to be used in a non-browser environment.
async function writeCiphers(cipherObj) {
if (!cipherObj) return
const allCiphers = []
const {input, folder, filename, customRotations, useAscii, randomRotations} = cipherObj;
let uniform = makeSectionHeader(input, 'Uniform Rotations')
let custom = makeSectionHeader(input, 'Custom Rotations', true)
let unique = makeSectionHeader(input, 'Random Rotations', true)
const uniformCiphers = getUniformCiphers(input, useAscii)
uniform += uniformCiphers.join('\n')