Skip to content

Instantly share code, notes, and snippets.

View pdparker's full-sized avatar

Philip Parker pdparker

  • Australian Catholic University
  • Australia
View GitHub Profile
@pdparker
pdparker / Fraction.py
Last active January 4, 2016 19:09
Class Fraction
def gcd(m,n):
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
class Fraction:
@pdparker
pdparker / quick
Created February 7, 2014 11:56
Quick Find and Union
class Quick:
def __init__(self,x):
self.ids = {i:i for i in x}
self.node = self.ids.keys()
self.connect = self.ids.values()
self.sz = {i:1 for i in range(len(self.ids))}
def root(self, x,compression=True):
@pdparker
pdparker / shellSort
Created February 28, 2014 01:16
shell sort
def shellSort(x):
h = len(x)//2
i = h
while h > 0:
while i < len(x):
for j in range(i,0,-1):
if x[j] < x[j-h]:
x[j],x[j-h] = x[j-h],x[j]
else:
break
@pdparker
pdparker / rpsls
Last active August 29, 2015 13:57
Rock Paper Scissors Lizard Spock
play <- function(){
possibilities <- c("rock", "Spock", "paper", "lizard", "scissors")
cat(possibilities, sep="\n")
cat("\n")
x <- readline("What is your choice? ")
if(x %in% possibilities == FALSE) {cat("FOOL! That is not one of the options")
x<- readline("Try Again: ")
}
y <- sample(possibilities, 1)
z <- which(possibilities==x)-which(possibilities==y)
@pdparker
pdparker / rpsls_py
Last active August 29, 2015 13:57
Rock Paper Scissors Lizard Spock - Written in Python
import difflib
import random
def name_to_number(x):
'''
Coverts player choice to number.
'''
#Uses dict rather than conditionals.
my_dict = {'rock':0, 'Spock':1, 'paper':2, 'lizard':3, 'scissors':4}
#returns the value associated with the string key
return my_dict[x]
@pdparker
pdparker / newton_sqrt
Created April 5, 2014 06:11
Newton square root
def newton_sqrt(x,s=1., kmax=100, tol=1e-8):
"""
Uses the newton method to approximate square roots.
Tolerence set to
"""
for k in range(kmax):
pre_s = s
s = .5 * (s + x/s)
if abs(s - pre_s) < tol:
break
@pdparker
pdparker / matchItHelp
Last active August 29, 2015 13:58
MatchIt helper function
library(ggplot2); library(MatchIt)
meandifftable<- function (x){
post<-data.frame(x$sum.matched[4])
matchID <- as.vector (row.names (post) )
names(post)[1]<-c("m_mean_diff")
post$absolute<- abs(post[1])
total2<-post[order (-post$absolute, na.last=NA) ,]
meandiffover1<- subset(total2[1], total2[1]> .1 | total2[1]< -.1)
meandiffover1
}
@pdparker
pdparker / index.html
Created May 8, 2014 11:50
Bootstrap Starter: HTML
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<!-- The following tag allows for website to be viewed on mobile devices -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
<!-- Bootstrap -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
@pdparker
pdparker / GBMhiggs
Created July 20, 2014 23:51
GBM initial submission For HiggML challenge
#Custom functions at end of file
setwd("/Users/phparker/Dropbox/kaggle/")
set.seed(100)
library(caret);library(glmnet);library(e1071); library(ROCR)
train = read.csv("Higgs/training.csv")
train.y = train[,32:33]
test = read.csv("Higgs/test.csv")
#Will create hold out set when comparing models or now it is fine to use whole set
#inTrain <- createDataPartition(train$Label, p = 1/2, list = FALSE)
#Re-label missing
@pdparker
pdparker / matchItExtra.R
Last active August 29, 2015 14:07
Additional summary function for the Matchit package
require(ggplot2); require(MatchIt)
meandifftable<- function (x){
post<-data.frame(x$sum.matched[4])
matchID <- as.vector (row.names (post) )
names(post)[1]<-c("m_mean_diff")
post$absolute<- abs(post[1])
total2<-post[order (-post$absolute, na.last=NA) ,]
meandiffover1<- subset(total2[1], total2[1]> .1 | total2[1]< -.1)
meandiffover1
}