Skip to content

Instantly share code, notes, and snippets.

@TGOlson
TGOlson / intersection
Created July 17, 2013 03:53
Question 4: Intersection
#Question 4: Intersection
def intersection(range1, range2)
# range1 is range1.min, range2.min - add in 'maxes' if range1 encompasses range2
r1_only = [range1[0], range2[0]]
r1_only << [range2[1], range1[1]] if range1[1] > range2[1]
# both occurs at range1.max, range2.min if overlap - both = range2 if encompass - nil if no overlap
both = [range2[0], range1[1]] if range1[1] < range2[1]
both = range2 if range1[1] > range2[1]
@TGOlson
TGOlson / morse_code.rb
Created July 19, 2013 22:47
Morse Code Generator
# Generates morse code based on input. Splits string and uses a morse code hash to convert letters.
# morse_encode("q").should == "--.-"
# morse_encode("cat").should == "-.-. .- -"
# morse_encode("CAT in hat").should == "-.-. .- - .. -. .... .- -"
def morse_encode(str)
string_split = str.downcase.split(' ')
coded_words = string_split.map {|wrd| morse_encode_word(wrd)}
coded_words.join(' ')
end
@TGOlson
TGOlson / bubble_sort.rb
Created July 19, 2013 22:51
Simple bubble sort program, written in Ruby. Takes an array, and uses the bubble sort method to return a sorted array.
# Takes an array, and uses the bubble sort method to return a sorted array.
# bubble_sort([]) # .should == []
# bubble_sort([1]) # .should == [1]
# bubble_sort([5, 4, 3, 2, 1]) # .should == [1, 2, 3, 4, 5]
def bubble_sort(arr)
sorted = false
while !sorted
out_of_place = 0
@TGOlson
TGOlson / cruddy_crud.rb
Created July 19, 2013 22:54
Simple CRUD program. I've been wanting to learn how to make one of these for a while, so I decided to whip together the most simple version I could - Create, Read, Update and Delete simple text files. Hopefully lots of updates coming.
def create_file(file)
target = File.open(file,'a+')
puts "#{file} created."
end
def read_file(file)
target = File.open(file,'r')
puts "That file is:"
puts target.read
end
@TGOlson
TGOlson / movies.rb
Created July 23, 2013 16:35
Simple CRUD app that stores info in a Hash. From lesson 6 of Code Academy Ruby Track.
movies = {
Iron_Man: 3,
Fast_Five: 4,
Sharknado: 5
}
puts 'Do you want to ADD, UPDATE, DISPLAY, or DELETE?'
choice = gets.chomp.downcase
case choice
@TGOlson
TGOlson / rp_calc1.rb
Last active December 21, 2015 00:09
Reverse Polish Calculator - Version 1 (an example of what not to do)
# version 1 of 2
# uses while loop and modifies array during looping (an example of what not to do)
# see version 2 here - https://gist.github.com/TGOlson/6218116
class RPNCalculator
def evaluate(string)
array = string.split
i = 0
while i < array.length
@TGOlson
TGOlson / rp_calc2.rb
Created August 13, 2013 05:20
Reverse Polish Calculator - Version 1 (much better!)
# version 2 of 2
# updated to handle negative numbers, removed while loop, and uses send method
class RPNCalculator
def evaluate(string)
array = string.split
output = []
array.each do |x|
@TGOlson
TGOlson / dictionary_scan.rb
Created October 10, 2013 04:31
Scan Dictionary for String Matches
def get_input
puts "Type a word to find matches."
print '> '
base_word = gets.chomp
scan_dictionary(base_word)
end
def scan_dictionary(word)
matches = read_dictionary.select { |line| line.include?(word.downcase) }
@TGOlson
TGOlson / en_US.txt
Created October 10, 2013 04:36
en_US.txt (for match program)
48245
0/nm
0th/pt
1/n1
1st/p
1th/tc
2/nm
2nd/p
2th/tc
3/nm
@TGOlson
TGOlson / rake_tasks.rb
Last active December 26, 2015 12:09
Speed up the process
desc "load all files in app/models to irb"
task :console do
file_list = Dir[File.dirname(__FILE__) + "/app/models/*.rb"].map { |f| "-r #{f}" }
exec "irb #{file_list.join(' ')}"
end
desc "generate new migration file"
task :generate do