Skip to content

Instantly share code, notes, and snippets.

require 'active_support/all'
@candidates = [
{
id: 5,
years_of_experience: 4,
github_points: 293,
languages: ['C', 'Ruby', 'Python', 'Clojure'],
date_applied: 5.days.ago.to_date,
age: 26
# Determine whether a string contains a SIN (Social Insurance Number).
# A SIN is 9 digits and we are assuming that they must have dashes in them
def has_sin?(string)
/\b[0-9]{3}-[0-9]{3}-[0-9]{3}\b/.match(string) != nil
end
puts "has_sin? returns true if it has what looks like a SIN"
puts has_sin?("please don't share this: 234-604-142") == true
puts "has_sin? returns false if it doesn't have a SIN"
def benchmark
# Your benchmarking code goes here.
start_time = Time.now
yield
end_time = Time.now
end_time - start_time
end
# Be careful, pasting this into IRB will take a long time to print.
@kaiguogit
kaiguogit / bottlerecycling.rb
Last active July 21, 2016 00:47
bottle recycling
def recycle_bottle (full,empty,caps)
b = ((full + empty) / 2).floor
c = ((full + caps) / 4).floor
@fromB += b
@fromC += c
empty = (full + empty) % 2
@kaiguogit
kaiguogit / toroman.rb
Last active July 20, 2016 23:07
toroman
require "pry-byebug"
def to_roman(num)
roman = ['','','M','D','C','L','X','V','I']
array = num.to_s.each_char.map(&:to_i)
result = ""
array.each_with_index do |item,index|
result += one_digit(item,roman.slice(index*2,3))
end
result
end
require "pry-byebug"
@states = {
OR: ['Oregon',['Salem', 'Portland']],
FL: ['Florida',['Tallahassee','Jacksonville']],
CA: ['California',['Sacramento','Los Angeles']],
NY: ['New York',['Albany','New York']],
MI: ['Michigan',['Lansing', 'Detroit']],
WA: ['Wahsington',['Olympia','Seattle']],
IN: ['Indiana',['Boise']]
}
@kaiguogit
kaiguogit / charcounting.rb
Last active July 20, 2016 19:04
character counting
# def count_letters(str)
# count={}
# str.strip.each_char do |char|
# count[char] = 0 unless count.include?(char)
# count[char] += 1
# end
# count
# end
def count_letters(str)
list = {'yvr' => 'Vancouver', 'yba' => 'Banff', 'yyz' => 'Toronto', 'yxx' => 'Abbotsford', 'ybw' => 'Calgary'}
# Why is it returning nil instead of first element of the list above
p list["yvr"]
@kaiguogit
kaiguogit / merge_sort.rb
Last active July 20, 2016 18:28
Merge_sort
def merge_sort(arr)
return arr if arr.length <=1
left = []
right = []
arr.each_index do |index|
index.odd? ? left.push(arr[index]) : right.push(arr[index])
end
left = merge_sort(left)
right = merge_sort(right)
@kaiguogit
kaiguogit / signquote.rb
Last active July 19, 2016 21:44
Sign Quote
def get_quote(dimensions, colour)
(dimensions * 15 + (colour > 2 ? colour * 15 : colour * 10))*1.15
end
puts "#{get_quote(10, 3)}"