This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] ||= [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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 |