Skip to content

Instantly share code, notes, and snippets.

@Spuffynism
Spuffynism / ListenableFutureAdapter.java
Created August 12, 2017 23:07
ListenableFuture to CompletableFuture Adapter
package com.springmvc.model;
import org.springframework.util.concurrent.ListenableFuture;
import java.util.concurrent.CompletableFuture;
/**
* Converts a listenable future to a completable future, mapping the listenable futures' callbacks
* to the completable futures' equivalents.
*/
@Spuffynism
Spuffynism / binary_search.js
Last active October 19, 2017 18:12
JS binary search
var binarySearch = function (a, x) {
var low = 0,
high = a.length - 1,
mid;
while (low <= high) {
mid = Math.floor((low + high) / 2);
if (a[mid] < x)
low = mid + 1;
@Spuffynism
Spuffynism / find_complement.js
Last active October 20, 2017 19:17
Get if array item has complement
var hasComplement = function (array, sum) {
if (sum < 0)
throw "sum must be bigger than 0";
var complements = [];
for (var i = 0, complement; i < array.length; i++, complement = sum - array[i]) {
if (array.indexOf(complement) != -1) // if (complement.indexOf(array[i]) != -1)
return true;
else
@Spuffynism
Spuffynism / attacker_success_probability.js
Last active November 6, 2017 04:46
A bitcoin attacker's catching-up success probability
// converted from C code from p.6 of the btc whitepaper
// tested with p.7's provided sample inputs
// q being the probability the attacker finds the next block
// z being the number of blocks the attacker is behind
var AttackerSuccessProbability = function(q, z) {
var p = 1.0 - q;
var lambda = z * (q / p);
var sum = 1.0;
var poisson;
@Spuffynism
Spuffynism / tree_roadtrip.js
Last active November 14, 2017 17:52
tree visiting algorithms
function Node (name) {
this.name = name;
this.children = [];
}
function Tree (node) {
this.node = node;
this.visit = function(fn) {
return fn(this.node);
@Spuffynism
Spuffynism / playlist_shuffling.js
Last active November 18, 2017 00:28
better spotify playlist shuffling
let shuffle = function (playlist) {
let shuffledPlaylist = [],
song;
while (playlist.length !== 0) {
song = Math.floor(Math.random() * playlist.length);
shuffledPlaylist.push(playlist[song]);
playlist.splice(song, 1);
}
return shuffledPlaylist;
@Spuffynism
Spuffynism / binary_tree_roadtrip.js
Last active November 21, 2017 17:23
binary tree visiting algorithms
function Node (name) {
this.name = name;
this.left = null;
this.right = null;
}
function BinaryTree (node) {
this.node = node;
this.visit = function(fn) {
@Spuffynism
Spuffynism / str_to_brainfuck.js
Last active December 18, 2017 19:26
Converts a string to a brainfuck program that prints the said string
const COMMAND = {
next: '>',
prev: '<',
increment: '+',
decrement: '-',
print: '.',
accept: ',',
@Spuffynism
Spuffynism / BrainfuckCompiler.java
Created December 22, 2017 22:03
Brainfuck Java compiler
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BrainfuckCompiler {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("import java.util.Scanner;\n");
sb.append("public class BrainfuckProgram {\n");
@Spuffynism
Spuffynism / pomme-de-terre.py
Last active December 31, 2017 06:00
very quick and dirty pomodoro timer
import sys
import time
import datetime
from enum import Enum
class TimerType(Enum):
POMODORO = (30, )
PAUSE = (5, )