Skip to content

Instantly share code, notes, and snippets.

View ZacharyGodfrey's full-sized avatar

Zachary Godfrey ZacharyGodfrey

View GitHub Profile
@ZacharyGodfrey
ZacharyGodfrey / Permutations.js
Created June 12, 2019 16:43
Returns an array of permutations of a set of values
function permutations(set){
if (set.length == 0) return [];
if (set.length == 1) return set;
var values = set.filter((value, index) => set.lastIndexOf(value) == index);
return values.reduce((result, value) => {
var withoutCurrent = values.filter(x => x !== value);
var permutationsStartingWithCurrent = permutations(withoutCurrent).map(x => [value].concat(x));
return result.concat(permutationsStartingWithCurrent);
@ZacharyGodfrey
ZacharyGodfrey / Encryption.js
Last active September 22, 2018 13:59
Simple functions for encrypting and decrypting messages in JavaScript.
function createPad(key, length){
var index = 0;
var output = "";
while (index < length){
var k = key.charCodeAt(index % key.length);
var charCode = ((((k * k) ^ 101) << 4) - index) % 256;
output += String.fromCharCode(charCode);
index++;
@ZacharyGodfrey
ZacharyGodfrey / Primality.js
Last active October 4, 2019 20:35
Determines whether a given number is prime.
function isPrime(number){
switch (true){
// Non-integers are not prime
case !Number.isInteger(number): return false;
// Negatives, 0, and 1 are not prime
case number < 2: return false;
// Single-digit prime factor checks to reduce looping
case number % 2 == 0: return number == 2;
@ZacharyGodfrey
ZacharyGodfrey / Hash.js
Created July 23, 2018 01:48
Hashes a message as 16 characters in base-36.
function hash(message){
// Seed each output character with the first 16 prime numbers
var seed = [
2, 3, 5, 7,
11, 13, 17, 19,
23, 29, 31, 37,
41, 43, 47, 53
];
// Convert each message character into its ASCII character code
@ZacharyGodfrey
ZacharyGodfrey / Stack.cs
Last active August 14, 2018 16:37
An implementation of the Stack in C#
using System;
namespace StackImplementation
{
public interface IStack<T>
{
int Depth();
bool IsEmpty();
@ZacharyGodfrey
ZacharyGodfrey / RomanNumerals.js
Last active June 21, 2018 20:08
Convert a decimal number to a string of Roman numerals
// Convert a decimal number to a string of Roman numerals
function toRomanNumerals(number){
var repeat = (times, str) => Array(times).fill(str).join("");
return repeat(number, "I")
// 1e1
.replace(new RegExp(repeat(10, "I"), "gi"), "X") // 10
.replace(new RegExp(repeat(9, "I"), "gi"), "IX") // 9
.replace(new RegExp(repeat(5, "I"), "gi"), "V") // 5
.replace(new RegExp(repeat(4, "I"), "gi"), "IV") // 4
@ZacharyGodfrey
ZacharyGodfrey / window.close.js
Last active March 16, 2018 21:24
Close a tab after opening it from another window
// On the first tab, we need to hijack any links that would open in a new tab
// and open them with JavaScript so that we can store a reference to the newly opened tab
// Vanilla JavaScript
Array.from(document.querySelectorAll("a[target='_blank']")).forEach(function(a){
a.addEventListener("click", function(e){
e.preventDefault();
e.stopPropagation();
window.newTabReference = window.open(a.attributes.href, "_blank");
@ZacharyGodfrey
ZacharyGodfrey / Debounce.js
Created February 22, 2018 13:34
An alternative debounce method in JS.
// Runs <fn> immediately if not run in the last <ms> milliseconds
// Skips execution of <fn> otherwise
function debounce(fn, ms) {
var lastRun = 0;
return function () {
var now = new Date().getTime();
if (now - ms >= lastRun) {
lastRun = now;
@ZacharyGodfrey
ZacharyGodfrey / TryCatchWrap.cs
Last active February 6, 2018 22:42
Wraps an action in a Try/Catch block for you.
private T TryCatchWrap<T>(Func<T> action, T defaultValue)
{
try
{
return action();
}
catch (Exception e)
{
// Log the exception somewhere
return defaultValue;
@ZacharyGodfrey
ZacharyGodfrey / Immutable.js
Last active January 30, 2018 03:13
JS implementation of immutable objects.
// Factory Function pattern of object creation
function newUser(id, first, last){
// Validation of inputs happens in only one place - the constructor
if (typeof id !== "number") throw new Error("Invalid ID");
if (typeof first !== "string") throw new Error("Invalid FirstName");
if (typeof last !== "string") throw new Error("Invalid LastName");
return {
// Getters return the values used to create the object
get ID(){ return id },