Skip to content

Instantly share code, notes, and snippets.

@fferri
fferri / graph.py
Last active August 29, 2015 14:26
class DirectedGraph:
def __init__(self, edges={}):
# copy/cast constructor
if isinstance(edges, DirectedGraph): edges = edges.label
self.label = {}
self.vertex_in = {}
self.vertex_out = {}
for edge, label in edges.items():
self[edge] = label
from graph import *
def kosaraju(g):
def dfs(g, start, s, visited=set()):
for n in g.vertex_out[start]:
if n in visited: continue
visited.add(n)
dfs(g, n, s, visited)
s.append(start)
from random import sample
import Foundation
public protocol KDPoint {
func distance(other: Self) -> Double
func dimension() -> Int
func lessThan(other: Self, dim: Int) -> Bool
}
public enum BinaryTreeVisitOrder {
case InOrder
import Foundation
func emptyGenerator<T>() -> AnyGenerator<T> {
return anyGenerator{
return nil
}
}
func generateOnce<T>(g: () -> T) -> AnyGenerator<T> {
var first = true
@fferri
fferri / execv_with_timeout.cc
Created February 18, 2016 15:16
execv with timeout
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
void exec_with_timeout(char * const *argv, int timeout, int kill_signal = SIGKILL)
{
pid_t intermediate_pid = fork();
if(intermediate_pid == 0) {
pid_t worker_pid = fork();
We couldn’t find that file to show.
#include <QtCore>
class SignalSpy {
static QThreadStorage<bool> entered;
static void signalBegin(QObject *caller, int signalIndex, void **);
static void slotBegin(QObject *caller, int index, void **);
public:
static void start();
};
def dist(x, y):
return sum((xi-yi)**2 for xi,yi in zip(x,y))
class KDTree:
class Node:
def __init__(self, x, payload=None, axis=0):
self.x, self.axis, self.payload = x, axis, payload
self.l, self.r = None, None
def insert(self, x, payload=None):
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
import json
import urllib
import urllib.request
import urllib.parse
from bs4 import BeautifulSoup
import re
from unidecode import unidecode
import youtube_dl
# solution to puzzle https://www.youtube.com/watch?v=LyyHt7NfBxI
# "can you make 24 from 3 3 8 8 using only + - * / ( ) ?"
class Add:
def __init__(self, a, b):
self.a, self.b = a, b
def eval(self):
return self.a.eval() + self.b.eval()
def __repr__(self):
return '({!r} + {!r})'.format(self.a, self.b)