Skip to content

Instantly share code, notes, and snippets.

View conorgriffin's full-sized avatar

Conor Griffin conorgriffin

View GitHub Profile
@conorgriffin
conorgriffin / CJGGridView.h
Last active August 29, 2015 13:55
A board game grid UIView subclass
//
// CJGGridView.h
//
//
// Created by Conor Griffin on 30/12/2013.
// Copyright (c) 2013 Conor Griffin. All rights reserved.
//
#import <UIKit/UIKit.h>
@conorgriffin
conorgriffin / example1a.java
Last active August 29, 2015 13:56
Java Ternary Operator
// Example #1a
public class Person {
private int age;
public Person(int age) {
this.age = age;
}
public void isAdultOrChild() {
System.out.println(this.age >= 18 ? "Adult" : "Child");
# COLORS
LIGHT_GRAY="\[\033[0;37m\]"; BLUE="\[\033[1;36m\]"; RED="\[\033[0;31m\]"; LIGHT_RED="\[\033[1;31m\]";
GREEN="\[\033[0;32m\]"; WHITE="\[\033[1;37m\]"; LIGHT_GRAY="\[\033[0;37m\]"; YELLOW="\[\033[1;33m\]";
# GIT PROMPT (http://gist.github.com/120804)
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \(\1\)/';
}
function parse_git_status {
git status 2> /dev/null | sed -e '/(working directory clean)$/!d' | wc -l;
}
@conorgriffin
conorgriffin / array_process_benchmark.rb
Last active August 29, 2015 14:07
Benchmark ruby methods to process array of strings of words into array of unique individual words
require 'set'
require 'benchmark/ips'
class Array
def process_old()
self.join(" ").split().uniq
end
def process_new_1()
unique = Set.new()
self.each {
@conorgriffin
conorgriffin / SimpleMailer.rb
Last active August 29, 2015 14:08
Scraping a web page with Nokogiri and mailing the results with ActionMailer
require 'action_mailer'
ActionMailer::Base.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => 'sender@gmail.com',
:password => 'senders-gmail-app-password',
:authentication => :plain,
}
# Explicit path from root of document
elements = doc.xpath("/html"\
"/body"\
"/div[@id='m1']"\
"/div[@class='middle']"\
"/div[@class='col1']"\
"/div[@class='box1l']"\
"/table[@class='cat']"\
"/tr[td/a/img[@src='/i/ple1.gif'] and td/a[contains(@href,'Canon-EF-lenses')]]")
@conorgriffin
conorgriffin / testing.rb
Created November 13, 2014 00:08
Performance testing of various methods to reduce an array of strings to an array of unique individual words
require 'set'
# NOTE: This requires the benchmark-ips gem
# In case you don't want to install this gem, here's sample output of this file
# Calculating -------------------------------------
# Old 1: 9 i/100ms
# New 1: 4 i/100ms
# New 2: 8 i/100ms
# New 3: 8 i/100ms
@conorgriffin
conorgriffin / AlienNumbers.py
Last active August 29, 2015 14:09
A Python solution to the 2009 Google Code Jam problem entitled Alien Numbers
import sys
import re
"""
http://code.google.com/codejam/contest/32003/dashboard#s=p0
"""
def process(current_case):
alien_number, source_language, target_language = current_case.split()
source_language = list(source_language)
@conorgriffin
conorgriffin / pid.c
Last active August 29, 2015 14:10
A really simple C program to get its own pid
// Taken from http://duartes.org/gustavo/blog/post/system-calls/
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
pid_t p = getpid();
printf("%d\n", p);
}
#!/usr/bin/ruby
class IPGenerator
public
def initialize(session_count, session_length)
@session_count = session_count
@session_length = session_length
@sessions = {}
end