This file contains hidden or 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 ORM | |
def save | |
table_name = self.class | |
instance_variable_names = self.instance_variables.map do |i| | |
i.slice(1, i.length) | |
end | |
instance_variable_values = self.instance_variables.map do |i| | |
self.instance_variable_get("#{i}") |
This file contains hidden or 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
require 'active_record' | |
require 'pg' | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Base.establish_connection( | |
:adapter => "postgresql", | |
:host => 'ec2-54-204-41-178.compute-1.amazonaws.com', | |
:username => 'bmdjwluxchptuq', | |
:password => 'aEH-cKdr2zoXYUAjI8Xjma5eXK', |
This file contains hidden or 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
require 'sinatra' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'sinatra/json' | |
require 'json' | |
# sets the view directory correctly (to make it work with gists) | |
set :views, Proc.new { File.dirname(__FILE__) } | |
This file contains hidden or 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 ORM | |
def save | |
if defined? @id | |
sql_update | |
else | |
sql_insert | |
end | |
end |
This file contains hidden or 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
# An edit distance (e.d.) is defined as how many times you need to replace, add, or remove characters to match two strings. | |
# E.G. "train" and "brain" have e.d.=1 | |
# "train" and "rain" have e.d.=1 | |
# "train" and "trains" have e.d.=1 | |
# Write a method that returns TRUE if edit distance is exactly 1, FALSE if not. | |
def edit_distance_is_one(word1, word2) | |
i1 = 0 | |
i2 = 0 |
This file contains hidden or 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
# pat, tap, apt | |
def find_longest_anagram() | |
words = File.open('/usr/share/dict/words').read | |
anagrams = Hash.new { [] } | |
most_so_far = 0 | |
most_signature = nil |
This file contains hidden or 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
# runtime O(N^2) | |
# memory O(1) | |
def find_pair(numbers, sum) | |
numbers.each_with_index do |n, i| | |
numbers.each_with_index do |m, j| | |
if n + m == sum | |
return [n, m] | |
end | |
end |
This file contains hidden or 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
# N is number of elements | |
# N | |
# O(N) | |
def find_index(number, numbers) | |
numbers.each_with_index do |n, i| | |
if n == number | |
return i |
This file contains hidden or 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
require 'sinatra' | |
require 'open-uri' | |
require 'sinatra/json' | |
require 'json' | |
# sets the view directory correctly (to make it work with gists) | |
set :views, Proc.new { File.dirname(__FILE__) } | |
This file contains hidden or 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 example demonstrates how to generate SQL statements | |
# to fetch and save objects to a database. | |
# Warning: It does not do the actual database calls. just returns the statements. | |
class Book | |
attr_accessor :id, :title, :author_id, :subject_id | |
def initialize(id, title, author_id, subject_id) | |
@id = id |