Skip to content

Instantly share code, notes, and snippets.

@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: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: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: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:f07689808e88a4fd125f
Last active August 29, 2015 14:14
Python count sort
def count_sort(A, K):
""" A is positive integer array each element is in range [0, K]"""
count = [0] * (K + 1)
for a in A:
count[a] += 1
for i in range(1, K + 1):
count[i] = count[i - 1] + count[i]
B = [None] * len(A)
@m00nlight
m00nlight / gist:b3f7a4e4fb9ee9ddd1dd
Last active April 18, 2018 10:13
Python Knuth shuffle algorithm
def knuth_shuffle(items):
"""
Fisher-Yates shuffle or Knuth shuffle which name is more famous.
See <http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle> for detail
Type : [a] -> None (shuffle inplace)
Post constrain: Should be list
Post constrain: return array of the same length of input
"""
for i in range(len(items)):
@m00nlight
m00nlight / gist:bfe54d1b2db362755a3a
Last active June 30, 2019 06:17
Python reservoir sampling algorithm
from random import randrange
def reservoir_sampling(items, k):
"""
Reservoir sampling algorithm for large sample space or unknow end list
See <http://en.wikipedia.org/wiki/Reservoir_sampling> for detail>
Type: ([a] * Int) -> [a]
Prev constrain: k is positive and items at least of k items
Post constrain: the length of return array is k
"""
@m00nlight
m00nlight / gist:61839e7007f95d8c13b3
Created February 3, 2015 11:26
Functional style of quick sort in python
def quick_sort(nums):
if len(nums) <= 1:
return nums
else:
less = filter(lambda x: x <= nums[0], nums[1:])
large = filter(lambda x: x > nums[0], nums[1:])
return quick_sort(less) + [nums[0]] + quick_sort(large)
; Quick miniKanren-like code
;
; written at the meeting of a Functional Programming Group
; (Toukyou/Shibuya, Apr 29, 2006), as a quick illustration of logic
; programming. The code is really quite trivial and unsophisticated:
; it was written without any preparation whatsoever. The present file
; adds comments and makes minor adjustments.
;
; $Id: sokuza-kanren.scm,v 1.1 2006/05/10 23:12:41 oleg Exp oleg $
@m00nlight
m00nlight / gist:7f3d477e541e47d53ec1
Created February 3, 2015 02:09
Python compact radix sort of numbers
class RadixSort:
def radix_sort(self, nums):
"""
Input an number list and return an sortd list
Type: [Int] -> [Int]
"""
max_len = max(map(lambda x: len(str(x)), nums))
tmp, base = 1, 10
for i in range(max_len):