Skip to content

Instantly share code, notes, and snippets.

# 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 / 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
@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
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 / 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)
@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 / reverse_linked_list.rb
Created April 8, 2014 14:37
Reverse a Singley Linked List
# PROBLEM:
# Given a singley linked list, reverse the list.
#
# SOLUTION:
# Nothing to tricky other to keep track of all the pointers when iterating through.
# Basic object to represent a linked list.
class Node
attr_accessor :next
attr_accessor :value
@HoyaBoya
HoyaBoya / x.rb
Last active August 29, 2015 13:58
Print "helloworld" with the following if/else statement.
# PROBLEM
# Find an implementation of "x" that will force the code below to print "helloworld".
# SOLUTION
# This doesn't work with threads forked to return true/false. The trick is to just have x do both.
def x
print "hello"
false
end
##
# This is spree 2-1-stable specific logic to re-apply a promotion code. It will not work for 2-2-stable because promotions are being revamped.
# https://github.com/spree/spree/issues/4398
#
def reapply_coupon_codes
codes = []
# Go through the adjustments to find promotions that are not eligible
adjustments.each do |a|
if a.eligible == false && a.originator.respond_to?(:promotion) && a.originator.promotion.code