Skip to content

Instantly share code, notes, and snippets.

@dustMason
dustMason / 1 spec_helper.rb
Created September 16, 2012 22:17
Poltergeist Issue #155
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# require 'rspec/autorun'
require 'capybara/rails'
require 'capybara/poltergeist'
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {
# phantomjs_options: ['--cookies-file=/Users/turtle/Desktop/cookies.txt']
})
@dustMason
dustMason / game.coffee
Last active March 2, 2017 19:18
Simple console-based Letters and Numbers game – only the "Numbers" version. Requires node.js, coffeescript module and prompt (`npm install -g prompt`) Run with `coffee game.coffee`
prompt = require('prompt')
class Player
constructor: (@name,@deck)->
@cards = []
@ops = ["+","-","*","/"]
@chooseCards()
chooseCards: (slots=6)->
for slot in [0..slots-1]
i = Math.floor(Math.random()*@deck.length)
@dustMason
dustMason / about.md
Last active December 13, 2015 20:58
Letters and Numbers "Numbers Round" solver in Scala

A Scala object capable of coming up with the best answer during "Numbers Rounds" of the famous TV game-show Letters and Numbers. Uses Streams and lazy evaluation for a nicely performant solution.

see http://en.wikipedia.org/wiki/Letters_and_Numbers for more

One contestant chooses how many "small" and "large" numbers they would like to make up six randomly chosen numbers. Small numbers are between 1 and 10 inclusive, and large numbers are 25, 50, 75, or 100. All large numbers will be different, so at most four large numbers may be chosen. The contestants have to use arithmetic on some or all of those numbers to get as close as possible to a randomly generated three-digit target number within the thirty second time limit. Fractions are not allowed—only integers may be used at any stage of the calculation.

@dustMason
dustMason / lan.rb
Last active December 13, 2015 22:39
A ruby version of the Letters and Numbers "Numbers Round" solver. Check my previous gist for a Scala version with more info. REQUIRES RUBY 2.0! due to use of lazy enumerators.
class Equation
attr_reader :ops
def initialize(ops)
@ops = ops
end
def calc
calc_rec(@ops)
@dustMason
dustMason / threads.rb
Created May 31, 2013 03:38
Queue with finite number of threads pattern
@threads = []
@num_of_threads = 8
@queue = Queue.new
# fill queue with items ...
@num_of_threads.times do
@threads << Thread.new {
loop do
break if @queue.length == 0
do_something @queue.deq
@dustMason
dustMason / application.rb
Last active December 18, 2015 17:19 — forked from t2/application.rb
Rails 3 ActionView override for Foundation 4 field error styles.
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
html = %(<div class="error">#{html_tag})
html += %(<small>#{instance.error_message.to_a.to_sentence}</small>).html_safe unless html_tag =~ /^<label/
html += %(</div>)
html.html_safe
end
require 'net/http'
require 'json'
require 'uri'
uri = URI.parse "http://www.letsrevolutionizetesting.com/challenge.json"
loop do
response = JSON.parse(Net::HTTP.get_response(uri).body)
puts response
break unless response["follow"]
require 'open-uri'
require 'nokogiri'
base_uri = "http://www.thefremontproject.com"
path = "/rabbithole"
loop do
html = open(base_uri + path).read
link = Nokogiri::HTML(html).at_css("#next-link")
open("rabbithole.html", "wb") { |file| file.write(html) }
# Here is a sketch of some ActiveRecord models which could be used to support the features
# requested. I didn't code the implementation of the state machine as that isn't crucial
# to the question, but I think it would suit the system well and help in designing further
# features.
#
# The instance methods on the Seller model allow for flexibility of the rolling settlement
# window dates.
#
# As for indexing the tables, the most important one for the models below is:
# [:created_at, :seller_id] on the Transactions table. This makes the rolling settlement window
# I first implemented a solution in a naive style to feel out an algorithm.
# While sketching the first solution I realized that a version with much less
# complexity is easily written. The second version avoids exponential run-times
# by doing simple arithmetic on each iteration to avoid the call to
# Enumerable#reduce. I left both versions here so that you might get a feel for
# my thought process.
def pivot_index_v1 integers
pivot = -1
return pivot if integers.empty?