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
#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] |
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
# 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 |
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
# 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 |
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
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 |
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
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 |
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
# 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 |
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
# 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| |
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
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) } |
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
48245 | |
0/nm | |
0th/pt | |
1/n1 | |
1st/p | |
1th/tc | |
2/nm | |
2nd/p | |
2th/tc | |
3/nm |
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
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 |
OlderNewer