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
# 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) | |
my_match = /\b\d\d\d\-\d\d\d\-\d\d\d\b/.match(string) | |
my_match == nil ? false : true | |
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 |
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
@states = Hash.new { |h, k| h[k][1] = []} | |
@states = { | |
OR: ['Oregon', ['Portland', 'Jacksonville']], | |
FL: ['Florida', ['Miami']], | |
CA: ['California', ['San Jose', 'San Francisco', 'Monterrey']], | |
NY: ['New York', ['New York City', 'Long Island']], | |
MI: ['Michigan', ['Detroit']] | |
} |
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
states = { | |
OR: 'Oregon', | |
FL: 'Florida', | |
CA: 'California', | |
NY: 'New York', | |
MI: 'Michigan' | |
} |
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 count_letters(str) | |
letters = Hash.new{0} | |
str = str.gsub(/\s+/, "") #removes spaces | |
str.each_char { |c| letters[c] += 1 } | |
puts letters.inspect | |
end | |
puts "What's your string?" | |
user_input = gets.chomp |
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
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'] |
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 fizz_buzz_puts(start, finish) | |
# loops through each number in the range | |
start.upto(finish) do |number| | |
puts fizz_buzz(number) | |
end | |
end | |
def fizz_buzz(number) | |
# checks if number is divisible by both 3 and 5 | |
if div_3?(number) && div_5?(number) |
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 fb(s, f) | |
s.upto(f) { |x| | |
puts e(x) | |
} | |
end | |
def e(y) | |
if div_3?(y) && div_5?(y) | |
"FizzBuzz" | |
elsif div_5?(y) |
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 merge_sort(arr) | |
# if array is empty or has just one element, no need to sort | |
return arr if arr.size <= 1 | |
# split array in 2 halves (divide) | |
mid = arr.size/2 | |
left = arr[0, mid] # arr[start,length] | |
right = arr[mid, arr.size-mid] | |
return merge(merge_sort(left),merge_sort(right)) |
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 area_price(length,width) | |
length * width * 15 | |
end | |
def colour_price(colour_count) | |
colour_count <= 2 ? 10 * colour_count : 15 * colour_count | |
end | |
def add_tax(price_without_tax) | |
(price_without_tax * 1.15).round(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
# Implemented bubble sort & radix sort | |
# Included benchmarking module & benchmarked both methods | |
# Included manual benchmarking for radix_sort | |
# Sort the array from lowest to highest | |
def bubble_sort(arr) | |
return arr if arr.size <= 1 # already sorted | |
swapped = true | |
while swapped do | |
swapped = false |