Skip to content

Instantly share code, notes, and snippets.

@mendable
mendable / combination.rb
Created February 15, 2019 16:12
Enumerate all possibilities
possibilities = %w(wed thur fri)
(1..possibilities.size).reduce([]) do |accumulator, count|
accumulator + possibilities.combination(count).to_a
end
# => [["wed"], ["thur"], ["fri"], ["wed", "thur"], ["wed", "fri"], ["thur", "fri"], ["wed", "thur", "fri"]]
@mendable
mendable / tower_hopper_dp.rb
Created June 19, 2018 21:01
Tower Hoppers Problem (Dynamic Programming)
#!/usr/bin/env ruby
# Dynamic Programming solution to "Tower Hoppers" problem.
# Walk backwards through the towers, filling in the hash with a true/false flag
# if that position is hoppable or not.
def hoppable_dp?(towers)
hash = {} # index in towers, and if it's hoppable or not.
idx = towers.size - 1
@mendable
mendable / fibonacci.rb
Last active June 19, 2018 16:05
Fibonacci
#!/usr/bin/env ruby
# Fibonacci - sum of previous 2 numbers, where 1 & 2 = 1.
# Recursive
def fib_r(n)
return 1 if n == 1 || n == 2
fib_r(n - 1) + fib_r(n - 2)
end
@mendable
mendable / arduino-fan-cfm.ino
Created April 30, 2017 16:17
arduino-fan-cfm.ino
// This was just an fun project from some years ago, where we attempted to
// determine the efficiency (CFM) of a house celling fan by creating a bag
// vacum around the fan, and then timing how long it took for the bag to
// completely deflate.
//
// An arduino system performed the timing with a sensor being depressed
// upon the collapse of the bag to stop the experiment.
//
// Code is C for Ardunio.
@mendable
mendable / fizzbuzz.go
Created September 29, 2015 21:01
FizzBuzz in Go
package main
import "fmt"
// Fizzbuzz:
// Write a program that prints the numbers from 1 to 100.
// But for multiples of three print "Fizz" instead of the number and for the
// multiples of five print "Buzz". For numbers which are multiples of both
// three and five print "FizzBuzz".
func main() {
@mendable
mendable / redis_real_bitcount.rb
Created May 10, 2015 08:36
Real BITCOUNT for Redis (in Ruby)
# Performs a ranged BITCOUNT based on start + finish as BITS
# instead of BYTES like Redis's BITCOUNT does. Pass in ints.
# index starts from zero. start + finish are inclusive.
#
# Works by fetching the byte bit count and fetching the lower and
# upper bytes of the range, then removing any set bits in those bytes
# that are outside of the counting range if set.
#
# usage:
# count = real_bitcount("mykey", 20, 30)
# A shoulda macro for verifying a class has the writer attribute readers,
# writers and accessors. NOTE: I could be silly for wanting this. But it was
# there. So here it is.
#
# (c) Copyright 2009 Adam Keys. MIT license.
module AccessorMacros
def should_have_reader(name)
should "have an attribute reader for #{name.to_s}" do