This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func emptyGenerator<T>() -> AnyGenerator<T> { | |
return anyGenerator{ | |
return nil | |
} | |
} | |
func generateOnce<T>(g: () -> T) -> AnyGenerator<T> { | |
var first = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
OlderNewer