Skip to content

Instantly share code, notes, and snippets.

@qpwo
qpwo / johnson.py
Last active September 3, 2017 14:27
common python form of networkx's implementation of Johnson's cycle finding algorithm
from collections import defaultdict
def simple_cycles(G):
def _unblock(thisnode,blocked,B):
stack = set([thisnode])
while stack:
node = stack.pop()
if node in blocked:
blocked.remove(node)
stack.update(B[node])
@qpwo
qpwo / goodmorning.py
Created October 4, 2017 01:32
Solution to problem A in tonight's contest, with explanation of hashmaps
# A 'map' also known as a 'dictionary' also known as a 'hashmap':
# https://en.wikipedia.org/wiki/Hash_table
# It maps 'keys' to 'values'. Keys and values can be anything: strings, tuples, integers, floats...
# Here, the keys are integers and the values are lists of integers.
d = {
1: [1,2,3,4,5,6,7,8,9,0],
2: [2,3,5,6,8,9,0],
3: [3,6,9],
4: [4,5,6,7,8,9,0],
5: [5,6,8,9,0],
@qpwo
qpwo / test.py
Last active November 18, 2017 22:26
#! /usr/bin/python
# -*- coding: utf8 -*-
""" GAN-CLS """
import tensorflow as tf
import tensorlayer as tl
from tensorlayer.layers import *
from tensorlayer.prepro import *
from tensorlayer.cost import *
import numpy as np
@qpwo
qpwo / details.md
Created November 25, 2017 16:20
quora details

Real brains do not organize neurons into layers. It's a messy process where one neuron could connect directly to the input and the output, while another neuron is in a long chain, while a third neuron is connected to twenty different nodes at different depths. Who decided that artificial neural networks should be very constrained in terms of shape? Is it computational convenience, or does it actually work better?

@qpwo
qpwo / lukemath.js
Last active March 11, 2018 22:13
A minimal possible working example of a javascript library
var lukemath = new (function() {
var privatePi = 3.14;
this.tau = 2 * privatePi;
var privateSquare = function(n) {
return n * n;
}
this.fourth = function(n) {
return privateSquare(privateSquare(n));
}
})();
@qpwo
qpwo / ideal-conf-file.conf
Last active June 14, 2018 20:22
The conf file for a run of "mastml from the future" where all the important features have been added
# You run this with `$ python mastml.py settings.conf data.csv -o results/`
# Second example: `$ python mastml.py input.conf compositions.csv -o Desktop/model-results/`
# Or you open the website, upload a csv, upload a conf file, and download the resulting zip file
# Sections and subsections are in CamelCase; parameters are in snake_case
[GeneralSetup]
input_features = Auto # Defaults to all but last column (specifying Auto is same as omiting this option)
@qpwo
qpwo / Casino Game.ipynb
Created June 29, 2018 00:19
100 runs of the casino game where you double or half your money on a coin flip
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@qpwo
qpwo / halting_proof.py
Created August 6, 2018 22:09
Python proof of undecidability of the halting problem
# Proof of undecidability of halting problem:
from Turing import will_halt
# will_halt(program, input) -> does_halt
def loop_forever(garbage_input):
return loop_forever(garbage_input)
def square_root(n):
return n ** 0.5
@qpwo
qpwo / traveling salesman - simulated annealing.ipynb
Created September 21, 2018 19:23
A simulated annealing solution to traveling salesman problem, using python3 and numpy
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@qpwo
qpwo / monte_carlo_tree_search.py
Last active April 26, 2024 23:11
Monte Carlo tree search (MCTS) minimal implementation in Python 3, with a tic-tac-toe example gameplay
"""
A minimal implementation of Monte Carlo tree search (MCTS) in Python 3
Luke Harold Miles, July 2019, Public Domain Dedication
See also https://en.wikipedia.org/wiki/Monte_Carlo_tree_search
https://gist.github.com/qpwo/c538c6f73727e254fdc7fab81024f6e1
"""
from abc import ABC, abstractmethod
from collections import defaultdict
import math