Skip to content

Instantly share code, notes, and snippets.

View SamGerber-zz's full-sized avatar

Sam Gerber SamGerber-zz

View GitHub Profile
@SamGerber-zz
SamGerber-zz / rubocop_pre_commit_hook
Last active September 1, 2017 21:50 — forked from timheilman/rubocop_pre_commit_hook
Ruby style guide git pre-commit hook using Rubocop as the style guide checker. Only runs on staged ruby files that have been added and/or modified. Itself passes rubocop with default settings.
#!/usr/bin/env ruby
# Source: https://gist.github.com/timheilman/1bb8da91be4b79425d67314c9ae23f6b
# credit to https://www.github.com/joneshf
require 'rubocop'
changed_files =
`git diff --name-only --cached -- '*.rb' '*.rake'`
.split("\n").join(' ')
@SamGerber-zz
SamGerber-zz / dynamic_programming_combinations.js
Created April 27, 2016 21:28
Given various ways to score points, represented by an arbitrary array w, count how many ways you can add up to a score s. Raw
function waysToScore(awards, score) {
var ways = new Array(score + 1).fill(0);
// There's one way to score 0 points
ways[0] = 1;
// Loop through the awards
for (var a = 0; a < awards.length; a++){
// Loop through each integer from the award value up to the score
@SamGerber-zz
SamGerber-zz / dynamic_programming_permutations.js
Created April 27, 2016 21:24
Given various ways to score points, represented by an arbitrary array w, count how many ways you can add up to a score s.
function waysToScore(awards, score, sorted = false) {
// Ensure the ways to score points are sorted.
if(!sorted) { awards.sort((a, b)=>( a - b )); }
var ways = new Array(score + 1).fill(0);
// There's one way to score 0 points
ways[0] = 1;
// Loop through each integer from 1 up to the score
[1] pry(main)> jerry = {};
[2] pry(main)> a = [];
[3] pry(main)> jerry[a] = "anti-dentite";
[4] pry(main)> a << "seinfeld";
[5] pry(main)> jerry[[]] = "yada";
[6] pry(main)> jerry
=> {["seinfeld"]=>"anti-dentite", []=>"yada"}
[7] pry(main)> a.pop; a
=> []
[8] pry(main)> jerry.count
@SamGerber-zz
SamGerber-zz / test.rb
Created January 8, 2016 07:27
code embed test
class Array
def merge_sort(&prc)
return self if count < 2
prc ||= proc { |left, right| left <=> right }
left = self[0...(count / 2)]
right = self[(count / 2)..-1]
Array.merge(left, right, &prc)
end
@answer = "42" # => "42"
"Deep Thought said #{@answer} is the answer." # => "Deep Thought said 42 is the answer."
@SamGerber-zz
SamGerber-zz / setter.rb
Created December 9, 2015 07:36
Setter with "value"
def answer=(value)
@answer = value
end
@SamGerber-zz
SamGerber-zz / setter.rb
Created December 9, 2015 07:35
Setter with "value
def answer=(value)
@answer = value
end
@SamGerber-zz
SamGerber-zz / value.rb
Last active December 9, 2015 06:57
Does manipulating a string change its value?
@answer = "42" # => "42"
@answer.object_id # => 70033493776200
@answer.clear # => ""
@answer # => ""
@answer.object_id # => 70033493776200
@SamGerber-zz
SamGerber-zz / universe.rb
Created December 9, 2015 06:54
Getting and Setting Instance Variables Using an Attribute Accessor
class Universe
attr_accessor :answer
def initialize
@answer = "42"
end
end
universe = Universe.new # => #<Universe:0x007f63e303e848 @answer="42">
universe.answer # => "42"