Skip to content

Instantly share code, notes, and snippets.

@mkowoods
mkowoods / merge_sort.py
Created January 26, 2015 18:25
recursive merge sort from scratch
def merge(lhs, rhs):
lhs, rhs = lhs[:], rhs[:]
merged_list = []
while bool(lhs) and bool(rhs):
if lhs[0] <= rhs[0]:
merged_list.append(lhs.pop(0))
else:
merged_list.append(rhs.pop(0))
return merged_list + lhs + rhs
def bruteForce(A):
counter = 0
for i in range(len(A)):
for j in range(i, len(A)):
if A[i] > A[j]:
counter += 1
return counter
def countSplitInversions(B, C):
i,j,D = 0,0,[]
@mkowoods
mkowoods / BinarySearchWithIndexing.py
Created February 3, 2015 18:52
Binary Search With Indexing
def BinarySearch(val, A, idx = None):
"""Recursive algorithm for performing binary search"""
#idx: the rightmost index based on the numbering of the original array
n = len(A)
if idx == None:
idx = (n - 1)
if n == 1:
@mkowoods
mkowoods / quick-sort.py
Created February 9, 2015 05:53
Quick Sort Algorithm
import random
def Partition(A, pivot_idx, l, r):
#Choose a random value to be the pivot
pivot = A[pivot_idx]
#Move pivit to first position in subarray
A[l], A[pivot_idx] = A[pivot_idx], A[l]
#stores the idx of the last value less than or equal to the pivot
@mkowoods
mkowoods / mincut.py
Created February 16, 2015 19:32
Randomized MinCut Algorithm
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 15 18:35:50 2015
@author: mwoods
"""
import random
toy_graph = {1: [2, 4],
2: [1, 4, 3],
@mkowoods
mkowoods / subspace.R
Created March 5, 2015 01:10
subspace vs space in R
library(rgl)
x <- rep(NA, 20000)
y <- rep(NA, 20000)
z <- rep(NA, 20000)
color <- rep(NA, 20000)
#subspace
for(i in 1:10000){
@mkowoods
mkowoods / qlearning-sketch.py
Created April 22, 2015 19:36
Q Learning Sketches
#-------------------------------------------------------------------------------
# Name: module2
# Purpose:
#
# Author: mwoods
#
# Created: 22/04/2015
# Copyright: (c) mwoods 2015
# Licence: <your licence>
#-------------------------------------------------------------------------------
@mkowoods
mkowoods / Bayesian Network Analysis.py
Created May 16, 2015 00:48
Bayesian Network Analysis
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: mwoods
#
# Created: 15/05/2015
# Copyright: (c) mwoods 2015
# Licence: <your licence>
#-------------------------------------------------------------------------------
@mkowoods
mkowoods / daily_programmer_20150518.py
Created May 18, 2015 19:53
Solution to Daily Programmer 2015-05-18
#https://www.reddit.com/r/dailyprogrammer/comments/36cyxf/20150518_challenge_215_easy_sad_cycles
def next_digit(n, b):
return sum([int(d)**b for d in str(n)])
def recover_cycle(nodes, end):
tmp = []
while nodes:
node = nodes.pop()
@mkowoods
mkowoods / daily_programmer_20150520.py
Last active August 29, 2015 14:21
daily_programmer_20150520
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: mwoods
#
# Created: 20/05/2015
# Copyright: (c) mwoods 2015
# Licence: <your licence>
#-------------------------------------------------------------------------------