Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View thoolihan's full-sized avatar

Tim Hoolihan thoolihan

View GitHub Profile
@thoolihan
thoolihan / primes.py
Created February 9, 2014 21:42
Python Primes
#!/usr/bin/env python
import sys
import locale
locale.setlocale(locale.LC_ALL, 'en_US')
def is_prime_brute(n):
for x in range(2, n / 2 + 1):
if n % x == 0:
return False
@thoolihan
thoolihan / facebook_gender.R
Last active August 29, 2015 13:56
Facebook Friend Gender Analysis
library(rjson)
graph <- fromJSON(paste(readLines("friends.json"), collapse=""))
gender <- as.factor(
sapply(graph$data, function(f) {
ifelse(is.null(f$gender), "na", f$gender)
})
)
@thoolihan
thoolihan / bmi.py
Created March 4, 2014 19:08
Python BMI Utility
#!/usr/bin/env python
def invalid(msg):
print msg
exit()
def getFloat(prompt):
print prompt + ":",
input = raw_input()
if len(input) > 0:
h1 = {a: 1, b: 2}
h2 = h1.keep_if{|k,v| k == :a}
p h1.inspect # => {a: 1}
p h2.inspect # => {a: 1}
@thoolihan
thoolihan / mower_example.rb
Created April 23, 2014 21:26
StateMachine Example
require 'rubygems'
require 'statemachine'
class MowerContext
def cut_mode
puts "blade start spinning"
end
def idle_mode
puts "engine idling"
@thoolihan
thoolihan / packages.R
Created May 11, 2014 23:28
Using CRAN Packages in R
chooseCRANmirror()
install.packages("Rcmdr", dependencies = TRUE)
library("Rcmdr")
@thoolihan
thoolihan / Basic.R
Created May 11, 2014 23:34
Basic Example
help(topic) # help on a topic
help.start() # browser-based help tool
source("someRFile.R") # load a file
a = 1 + 3
rm(a) # remove the variable a
odd_digits <- c(1,3,5,7,9)
total = sum(odd_digits)
@thoolihan
thoolihan / es_cluster
Last active August 29, 2015 14:08
Manage an ElasticSearch Cluster on OS X With LaunchAgents
#!/usr/bin/env bash
apply () {
for node in '' 2 3
do
launchctl $1 ~/Library/LaunchAgents/homebrew.mxcl.elasticsearch${node}.plist
done
}
status () {
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>homebrew.mxcl.elasticsearch2</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/elasticsearch</string>
<string>--config=/usr/local/opt/elasticsearch/config/elasticsearch.yml</string>
@thoolihan
thoolihan / word_count.R
Last active August 29, 2015 14:09
Word Count in R
elements <- c('the','story','was','the','point','of','the',
'story', 'a', 'quick','brown','fox','the','a',
'jumped','fence','over')
words <- sample(elements, 20000, replace = TRUE)
count <- function(word) {
sum(words == word)
}
# unique