Skip to content

Instantly share code, notes, and snippets.

@ceautery
ceautery / gist:4240087f1bfbcebc6cb7
Created December 23, 2015 13:19
Non-recursive Heap's Algorithm in JavaScript, with a callback
function iterate(arr, callback) {
var fac = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800];
function swap(a, b) {
var tmp = arr[a];
arr[a] = arr[b];
arr[b] = tmp;
}
callback(arr);
for (var iter = 1, last = fac[arr.length]; iter < last; iter++) {
@ceautery
ceautery / gist:5b32a46ac4ed6c1df5d63281df90ccd9
Created April 20, 2016 17:28
Mask the middle part of credit card numbers
function MiddleMasker(leadNumChars, trailNumChars, replaceChar) {
var re = new RegExp('(^.{' + leadNumChars + '})?.(?=.{' + trailNumChars + '})', 'g');
this.mask = function(str) {
return str.replace(re, '$1' + replaceChar)
}
}
var m = new MiddleMasker(6, 4, 'X');
m.mask('4111123456789012'); // "411112XXXXXX9012"
@ceautery
ceautery / test.html
Created October 21, 2016 19:41
Lookup latitude/longitude coordinates of cities using Google's Geocode API
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 400px;
width: 100%;
}
</style>
</head>
@ceautery
ceautery / pairwise.js
Last active May 21, 2017 14:07
Solution to the https://www.freecodecamp.com/challenges/pairwise with lengthy explanation
/*
* pairwise takes an array of numbers and a target number as arguments, and
* searches for pairs of array elements that sum to the target number. It sums
* the indices of each pair that add to the target, and returns that sum.
*
* Caveats:
* - Each index can only be used once
* - Given a choice between two indices that can pair to the target, the
* lowest index should be used. For example, passing ([5, 6, 6], 11) to
* pairwise, the resulting sum should be 1, since index 0 (the 5) and
@ceautery
ceautery / composite-counter.py
Last active January 2, 2018 23:23
Advent of Code 2017, Day 23
import math
(start, stop) = (109900, 126901)
composites = []
for pos in range(2, int(math.sqrt(stop))):
if not pos in composites:
composites.extend([i for i in range(pos * pos, stop, pos)])
filtered = [p for p in range(start, stop, 17) if p in composites]