Skip to content

Instantly share code, notes, and snippets.

@m00nlight
m00nlight / gist:d72f3913bab79e8e6e75
Created February 6, 2015 13:51
Python multiple consumer and producer problem
from Queue import Queue
from threading import Thread
from random import randrange
queue = Queue(10)
class Consumer(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
@m00nlight
m00nlight / gist:0f9306b4d4e61ba0195f
Last active December 5, 2022 21:13
Python naive implementation of lower_bound and upper_bound
def lower_bound(nums, target):
l, r = 0, len(nums) - 1
while l <= r:
mid = l + (r - l) / 2
if nums[mid] >= target:
r = mid - 1
else:
l = mid + 1
return l
@m00nlight
m00nlight / gist:d19f63005dc4f69445ee
Created February 14, 2015 03:18
Python kruskal algorithm for minimum spanning tree
import sys
from heapq import heappush, heappop
class UFSet:
def __init__(self, n):
self.fa = [i for i in range(n)]
def find(self, v):
if v != self.fa[v]:
self.fa[v] = self.find(self.fa[v])
@m00nlight
m00nlight / gist:36c3ecf6d3114338fb75
Created February 14, 2015 09:00
Python Euler's Phi function
import sys
import itertools
def sieve(n):
is_prime = [True] * (n + 1)
prime_list = []
is_prime[0] = is_prime[1] = False
prime_list.append(2)
@m00nlight
m00nlight / gist:56430f21456a23f19ed9
Created February 14, 2015 13:13
Python extend greatest common divisor
import sys
import itertools
def extend_gcd(a, b):
if b == 0:
return (1, 0, a)
else:
(a1, b1, g) = extend_gcd(b, a % b)
return (b1, a1 - (a // b) * b1, g)
@m00nlight
m00nlight / gist:1f226777a49cfc40ed8f
Last active March 7, 2022 12:24
Python range minimum query
import sys
import itertools
class RMQ:
def __init__(self, n):
self.sz = 1
self.inf = (1 << 31) - 1
while self.sz <= n: self.sz = self.sz << 1
self.dat = [self.inf] * (2 * self.sz - 1)
@m00nlight
m00nlight / gist:34b0f6a2d6a26fcef930
Created February 19, 2015 07:23
Python max sub-array problem
from operator import add, sub
import sys
def max_subarray(arr):
ret, cur = arr[0], 0 if arr[0] < 0 else arr[0]
for num in arr[1:]:
ret = max(ret, cur + num)
cur = max(0, cur + num)
return ret
@m00nlight
m00nlight / gist:a076d3995406ca92acd6
Last active October 21, 2020 21:31
Python merge sort in place, so space complexity is O(1)
import random
def merge_sort(xs):
"""Inplace merge sort of array without recursive. The basic idea
is to avoid the recursive call while using iterative solution.
The algorithm first merge chunk of length of 2, then merge chunks
of length 4, then 8, 16, .... , until 2^k where 2^k is large than
the length of the array
"""
@m00nlight
m00nlight / gist:0c378d9a7246a02b4650
Last active August 29, 2015 14:15
Python S-exp arithmetic evaluation
import operator
Symbol = str
def tokenize(exp):
return exp.replace('(', ' ( ').replace(')', ' ) ').split()
def parser(exp):
return read_from_tokens(tokenize(exp))
#!/usr/local/Gambit-C/bin/gsi
; Copyright (C) 2004 by Marc Feeley, All Rights Reserved.
; This is the "90 minute Scheme to C compiler" presented at the
; Montreal Scheme/Lisp User Group on October 20, 2004.
; Usage with Gambit-C 4.0:
;
; % ./90-min-scc.scm test.scm