Skip to content

Instantly share code, notes, and snippets.

@HoyaBoya
HoyaBoya / array_flattener.rb
Created June 23, 2013 01:26
This is the answer to an old interview question I used to ask.
class ArrayFlattener
# simple recursive flatten
def flatten(a)
new_a = []
a.each do |i|
if i.instance_of?(Array)
new_a.concat(flatten(i))
else
new_a << i
end
@HoyaBoya
HoyaBoya / fib.rb
Created June 23, 2013 12:52
Different ways to calculate the Fibonacci sequence to illustrate good recursion and bad recursion.
class Fibonacci
# really bad recursive fibonacci
def fib_at(n)
if n == 0
return 0
elsif n == 1
return 1
else
# we are calculating the same values over and over again!
return fib_at(n - 1) + fib_at(n - 2)
class StringSearch
# naive string search implementation
def search(pattern, text)
pattern_array = pattern.split(//)
text_array = text.split(//)
for i in 0 .. text_array.size - 1
match_count = 0
for j in 0 .. pattern_array.size - 1
# keep incrementing match count for every matching character
if text_array[i + j] == pattern_array[j]
@HoyaBoya
HoyaBoya / poor_kmp_search.rb
Created June 24, 2013 11:48
Very poor implementation of KMP string search in my attempt to understand the mechanics.
class StringSearch
def search(pattern, text)
pattern_array = pattern.split(//)
text_array = text.split(//)
for i in 0 .. text_array.size - 1
matched_so_far = ""
for j in 0 .. pattern_array.size - 1
# keep adding to the matched_so_far word for every matching character
if text_array[i + j] == pattern_array[j]
matched_so_far += pattern_array[j].to_s
@HoyaBoya
HoyaBoya / kmp_failure_function.rb
Created June 24, 2013 14:59
An attempt to understand the KMP Failure Function.
class FailureFunction
# build a table containing all of the maximum overlaps for a string
def build_table(s)
s_array = s.split(//)
table = {}
for i in 0 .. s_array.size - 1
word = s_array[0 .. i].join('')
table[word] = max_overlap(word)
end
# attempt to perform hash based string search
# http://en.wikipedia.org/wiki/Rabin–Karp_algorithm
class HashStringSearch
def search(pattern, target)
table = {}
# loop through the target string, building position matches for the candidate
for i in 0 .. target.size
candidate = target[i .. i + pattern.size - 1]
table[candidate] ||= []
class Node
attr_accessor :next
attr_accessor :value
end
def traverse_linked_list(start_node)
current_node = start_node
while current_node != nil do
puts "VALUE #{current_node.value}"
current_node = current_node.next
@HoyaBoya
HoyaBoya / index.html.erb
Created June 26, 2013 15:14
Remembering functional testing in Rails....
<h1>Welcome#index</h1>
<p>Find me in app/views/welcome/index.html.erb</p>
<p>Hello <%= @first_name %> <%= @last_name %></p>
@HoyaBoya
HoyaBoya / migration_create_user.rb
Last active December 19, 2015 00:29
I'm trying to understand Active Record again....
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :first_name
t.string :last_name
t.timestamps
end
add_index :users, :email, unique: true
Spree::Admin::ReportsController.class_eval do
add_available_report! :some_report
def some_report
# do something
end
end