Skip to content

Instantly share code, notes, and snippets.

@leighhalliday
leighhalliday / gist:40e438505804bc854b97
Last active August 29, 2015 14:13
A crazy, possibly horrible idea for using partials and presenter classes in Rails
# inside helper class
def render_share_partial
class SharePresenter
attr_accessor :campaign, :message
def campaign_title
self.campaign.name
end
end
@leighhalliday
leighhalliday / config.ru
Created February 17, 2015 16:15
Rack Example
# Use:
# This is in file config.ru
# 1) gem install rack
# 2) rackup
# 3) visit http://localhost:9292 in browser
# 4) Timer info will be in console
# 5) when changing code, will have to restart rackup
# Include rack
require 'rack'
@leighhalliday
leighhalliday / Output
Created April 26, 2015 19:47
Generator
Generator #1 is off.
Generator #2 is on, adding 62 MW, for a total of 62 MW!
Generator #3 is off.
Generator #4 is on, adding 62 MW, for a total of 124 MW!
Generator #5 is off.
Generator #6 is on, adding 124 MW, for a total of 248 MW!
Generator #7 is off.
Generator #8 is on, adding 124 MW, for a total of 372 MW!
Generator #9 is off.
Generator #10 is on, adding 124 MW, for a total of 496 MW!
var userAge = prompt("What's your age, user?");
var ageIsCorrect = false;
while (ageIsCorrect == false) {
ageIsCorrect = confirm("You entered " + userAge + ". Is this correct?");
if (ageIsCorrect) {
alert("Great! Your age is logged as " + userAge + ".");
} else {
userAge = prompt("What's your age, user?");
}
@leighhalliday
leighhalliday / gist:abbb1bec32d02269d4ee
Created May 8, 2015 20:04
# Write a program that outputs all possibilities to put + or - or nothing between the numbers 1, 2, ..., 9 (in this order) # such that the result is always 100. For example: 1 + 2 + 34 – 5 + 67 – 8 + 9 = 100.
['+', '-', ''].repeated_permutation(8).each do |perm|
sumText = ('1'..'8').zip(perm).flatten.join + '9'
sum = eval(sumText)
if sum == 100
puts "#{sumText} = 100"
end
end
@leighhalliday
leighhalliday / output.txt
Created May 9, 2015 18:45
Recursive "Carnac" solution in Ruby
1 + 2 + 3 - 4 + 5 + 6 + 78 + 9
1 + 2 + 34 - 5 + 67 - 8 + 9
1 + 23 - 4 + 5 + 6 + 78 - 9
1 + 23 - 4 + 56 + 7 + 8 + 9
12 + 3 + 4 + 5 - 6 - 7 + 89
12 + 3 - 4 + 5 + 67 + 8 + 9
12 - 3 - 4 + 5 - 6 + 7 + 89
123 + 4 - 5 + 67 - 89
123 - 4 - 5 - 6 - 7 + 8 - 9
123 + 45 - 67 + 8 - 9
@leighhalliday
leighhalliday / solution.rb
Last active August 29, 2015 14:20
Solution to #4 of apparent problems every programmer can do in an interview under an hour (I don't believe that)
# Write a function that given a list of non negative integers, arranges them such that they form the largest possible number.
# For example, given [50, 2, 1, 9], the largest formed number is 95021.
def max_num(arr)
# find the longest number
max_size = arr.map{|n| n.to_s.length}.max
# padded num repeats the last number in string up until the max length of a specific number
# then we append a number wich will help us get the shorter number first when all else equal
# sort by the padded number, reverse it, and then convert it back to integer
@leighhalliday
leighhalliday / gist:da85b206011851fcac55
Created May 30, 2015 21:10
To start console in Sinatra
desc "start console"
task :console do
require 'irb'
require 'irb/completion'
require ::File.expand_path('../config/environment', __FILE__)
ARGV.clear
IRB.start
end
@leighhalliday
leighhalliday / bin_tree.exs
Last active March 7, 2019 16:29
Playing with Binary Trees in Elixir
defmodule MyList do
# Takes 2 lists like [1,2] and [3,4]
# and produces a single list like [1,3,2,4]
def zip_merge([heada | taila], [headb | tailb]) do
[heada] ++ [headb] ++ zip_merge(taila, tailb)
end
def zip_merge([heada | taila], []) do
[heada] ++ zip_merge(taila, [])
end
@leighhalliday
leighhalliday / union_find.rb
Created June 22, 2015 13:05
Quick-Find & Quick-Union with Path Compression
class UnionFind
attr_accessor :nodes, :sizes
def initialize(num)
self.nodes = []
self.sizes = []
num.times do |n|
self.nodes[n] = n