Skip to content

Instantly share code, notes, and snippets.

View mneedham's full-sized avatar

Mark Needham mneedham

View GitHub Profile
@mneedham
mneedham / gist:1811708
Created February 12, 2012 23:20
hello_world.rb
require 'sinatra/base' # this is 'modular' style
require 'repository'
class HelloWorld < Sinatra::Base
def repository
@repository ||= Repository.new
end
get '/*' do
repository.get('foo')
@mneedham
mneedham / gist:1811716
Created February 12, 2012 23:21
hello_world_spec.rb
require 'rspec'
require 'rack/test'
require 'hello_world'
require 'repository'
describe 'Hello World application' do
include Rack::Test::Methods
def app
HelloWorld
grid :: [[Int]]
grid = [[08,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,08],
[49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00],
[81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,03,49,13,36,65],
[52,70,95,23,04,60,11,42,69,24,68,56,01,32,56,71,37,02,36,91],
[22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80],
[24,47,32,60,99,03,45,02,44,75,33,53,78,36,84,20,35,17,12,50],
[32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70],
[67,26,20,68,02,62,12,20,95,63,94,39,63,08,40,91,66,49,94,21],
[24,55,58,05,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72],
package com.algoclass.dij
import collection.immutable.IndexedSeq
object Main {
def readCityBlock(id: Int) = {
val cityName = readLine()
val neighbours = readLine().toInt
val fromVertex = new Vertex(cityName, id)
@mneedham
mneedham / neo_loading.rb
Created June 10, 2012 12:41
Loading stuff into neo via the batch API
# So the problem is inserting data into neo using the batch API. So we have a bunch of people and we want to put them into the graph and also
# add to to the index so that we can search for them.
# The way the batch API works is that you can refer to previous commands by referencing their index in the list of commands (zero indexed)
# e.g. if I want to reference the person added in the first command I would reference that node as {0}
# You should be able to see how that works in the code below.
neo_people_to_load = Set.new
neo_people_to_load << { :name => "Mark Needham", :id => 1 }
neo_people_to_load << { :name => "Jenn Smith", :id => 2 }
neo_people_to_load << { :name => "Chris Ford", :id => 3 }
@mneedham
mneedham / rover.hs
Created June 15, 2012 22:34 — forked from uday-rayala/rover.hs
Mars rover in haskell
data Position = Position Int Int Direction
deriving (Show, Eq, Read)
data Direction = N | E | W | S
deriving (Show, Eq, Enum, Bounded, Read)
data Command = L | R | M
deriving (Show, Eq, Enum, Bounded, Read)
data DeltaPosition = DeltaPosition Int Int deriving (Show)
data State = State {
@mneedham
mneedham / gist:2978253
Created June 23, 2012 12:59
thoughtworks colleague to colleague algorithm
require 'rubygems'
require 'neography'
start_time = Time.now
puts "Start Time: #{start_time}"
@neo = Neography::Rest.new(:port => 7476)
all_the_people_query = "start n = node(*) where n.type? = 'person' and has(n.name) return n.name, id(n)"
all_the_people = @neo.execute_query(all_the_people_query)["data"]
@mneedham
mneedham / gist:2983831
Created June 24, 2012 16:05
neo4j query to find thoughtworks colleagues of colleagues
START n = node(*)
MATCH n-[r:colleagues*1..2]->c, n-[r2:member_of]->office
WHERE n.type? = 'person' and has(n.name) and (not(has(r2.end_date))) and office.name = 'London - UK South' and (not(has(c.thoughtquitter)))
RETURN n.name, count(distinct(c)) AS connections, office.name
ORDER BY connections DESC
@mneedham
mneedham / gist:3125294
Created July 16, 2012 21:45
people jen worked with
==> +------------------------------------+
==> | office.name | COUNT(r) |
==> +------------------------------------+
==> | "New York - US East" | 5 |
==> | "Chicago - US Central" | 1 |
==> | "Bangalore - India" | 2 |
==> | "Calgary - Canada West" | 3 |
==> | "London - UK South" | 31 |
==> | "ETG UK - ETG" | 1 |
==> | "Brisbane - Australia" | 1 |
@mneedham
mneedham / gist:3684005
Created September 9, 2012 12:05
neo4j: cypher vs core API
cypher:
START researchArea = node:research_areas('research_area_id:1'), month = node:months('month:8'), year = node:years('year:2012')
MATCH researchArea-[:has_child*1..5]->otherResearchArea-[:has_product]->product-[:sold]->sales-[:soldInMonth]->month, sales-[:soldInYear]->year
RETURN DISTINCT(sales.sales), sales.name, product
core API:
GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase("/path/to/db");