Skip to content

Instantly share code, notes, and snippets.

View BenDMyers's full-sized avatar
🦖

Ben Myers BenDMyers

🦖
View GitHub Profile
@BenDMyers
BenDMyers / posts.json
Last active December 1, 2018 22:40
List of posts for the OSU Local Hack Day 2018 React codelab
[
{
"avatar": "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ad/Commodore_Grace_M._Hopper%2C_USN_%28covered%29.jpg/192px-Commodore_Grace_M._Hopper%2C_USN_%28covered%29.jpg",
"name": "Grace Hopper",
"user_id": 1906,
"status": "Life was simple before World War II. After that, we had systems."
},
{
"avatar": "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/Alan_Turing_Aged_16.jpg/176px-Alan_Turing_Aged_16.jpg",
"name": "Alan Turing",
@BenDMyers
BenDMyers / .bashrc
Last active November 26, 2019 04:06
Bash Scripts Bootstrap
# Create new directory and jump inside it
# USAGE: md <new directory name>
function md() {
mkdir "$1"
cd "$1"
}
# Modify and re-source your .bashrc
function bashrc() {
nano ~/.bashrc;
@BenDMyers
BenDMyers / add-lazy.js
Created August 18, 2020 17:34
Add lazy loading to images for markdown-it
const FIGURE_WITH_LAZY = /<img\s+.*lazy=.*>/g;
const addLazy = (token) => {
if (token.type === 'image') {
if (!token.attrs) {
token.attrs = [];
}
token.attrs.push(['loading', 'lazy']);
} else if (
token.type === 'html_block' &&
@BenDMyers
BenDMyers / splice.js
Created December 5, 2020 18:25
Declarative implementation of Array.prototype.splice
/**
* Simplified `splice` implementation that doesn't handle all the different use cases for `start`.
*
* Deleting items in-place without using `splice` is hard. Here's the plan:
* 1. Create an array (`deletedItems`) of items which the user WANTS to remove from the array.
* 2. Create an array (`reinsertedItems`) of all items that come AFTER the deleted items.
* 3. Truncate the whole array to everything UP TO `start`.
* 4. Insert any new items provided by the user.
* 5. Reinsert everything in `reinsertedItems`.
* 6. Return `deletedItems`.
@BenDMyers
BenDMyers / light.css
Created July 25, 2021 17:03
Twitch Chat Light Theme
:root {
--twitch-gray: rgb(173, 173, 184);
--twitch-light: rgb(239, 239, 241);
--twitch-dark-background: rgb(24, 24, 27);
--twitch-purple: rgb(180, 84, 255);
}
body {
background-color: var(--twitch-light);
}
@BenDMyers
BenDMyers / isOdious.js
Last active October 11, 2021 05:22
Determine whether a given number is odious (its binary expansion has an odd number of 1s)
/**
* Determines whether a given number is "odious" (its binary expansion has an odd number of ones)
* @param {number} num non-negative integer to test odiousness of
* @returns {boolean} whether `num` is odious
*/
function isOdious(num) {
let numberOf1Bits = 0;
// Binary expansions represent numbers as sums of powers of 2
// So we need to find how many powers of 2 can be subtracted from `num`
@BenDMyers
BenDMyers / group-anagrams.js
Last active November 22, 2021 05:37
RWC: Group Anagrams
/**
* Take an array of phrases and return a list of anagram groups
* @param {string[]} phrases list of strings which may or may not be anagrams of other strings in the same list
* @returns {string[][]} a list of arrays of strings, where every string in an inner array is an anagram of the others
*/
function groupAnagrams(phrases) {
/** @type {Object<string, string[]>} */
const anagramGroups = {};
// Create a map of anagram groups
@BenDMyers
BenDMyers / phone-number.js
Last active November 29, 2021 06:31
RWC: Phone Number Keypad Combinations
/**
* @type {Object<String, String[]>}
*/
const keypad = {
'2': ['a', 'b', 'c'],
'3': ['d', 'e', 'f'],
'4': ['g', 'h', 'i'],
'5': ['j', 'k', 'l'],
'6': ['m', 'n', 'o'],
'7': ['p', 'q', 'r', 's'],
@BenDMyers
BenDMyers / cap-permutations.js
Created January 10, 2022 07:27
RWC: Cap Permutations
/**
* Gets all possible capitalizations of the given string
* @param {string} str string to get capitalization permutations of
* @returns {string[]} list of all possible capitalization of the given string
*/
function capPermutations(str) {
let characters = str.split('');
let permutations = [''];
for (let character of characters) {
// Character isn't a capitalizable letter, so tack it on to all visited permutations so far
@BenDMyers
BenDMyers / path-between-points.js
Created January 3, 2022 06:40
RWC: Path Between Points
/**
* @typedef {object} Point
* @property {string} name
* @property {string[]} connections
*/
/** @type {Point[]} */
const listOfPoints = [
{ name: 'A', connections: ['B', 'C'] },
{ name: 'B', connections: ['A', 'E'] },