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
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 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 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 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
# 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
<h1>Welcome#index</h1> | |
<p>Find me in app/views/welcome/index.html.erb</p> | |
<p>Hello <%= @first_name %> <%= @last_name %></p> |
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 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 |
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
Spree::Admin::ReportsController.class_eval do | |
add_available_report! :some_report | |
def some_report | |
# do something | |
end | |
end |
OlderNewer