Skip to content

Instantly share code, notes, and snippets.

View heavymetta's full-sized avatar

Kevin J Leung heavymetta

View GitHub Profile
@heavymetta
heavymetta / RegularExpressions.rb
Created September 4, 2015 03:34
Regular Expressions
#require "pry"
# 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(\d{3}-){2}\d{3}\b/ =~ string ? true : false
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
@heavymetta
heavymetta / statecity.rb
Created September 3, 2015 15:22
State & City
states = {
OR: 'Oregon',
FL: 'Florida',
TX: 'Texas',
NY: 'New York'
}
# task1 /////
states[:OG] = 'Oregon'
states[:COLO] = 'Colorado'
@heavymetta
heavymetta / Charactercounting.rb
Created September 3, 2015 14:50
Character.Counting
def count_letters(list)
variable = Hash.new
list.each do |word|
word.split('').each do |letter|
variable[letter] = variable[letter].nil? ? 1 : variable[letter] + 1
end
end
variable
end
@heavymetta
heavymetta / debug.rb
Last active September 3, 2015 14:48
Debugging 01.02.03.04
///// DEBUG 01 ///// --------------------
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']
///// DEBUG 02 ///// -------------------
def average(numbers)
@heavymetta
heavymetta / sort.rb
Created September 2, 2015 03:01
Sorting Algorithm
# Sort the array from lowest to highest
def bubblesort(arr)
n = arr.length
loop do
swapped = false
(n-1).times do |i|
if arr[i-1] > arr[i]
then arr[i-1], arr[i] = arr[i], arr[i-1]
swapped = true
end
@heavymetta
heavymetta / shakil.rb
Last active September 2, 2015 02:59
Shakil The Dog
def shakil_the_dog
puts "Your with shakil"
puts "Say something"
carryon = true
while carryon
userInput = gets.chomp.downcase
case userInput
when "woof"
puts "WOOF"*3
@heavymetta
heavymetta / renter.rb
Created September 1, 2015 21:52
The Yuppie Vancouverite
# must be baller and either furnished or rent cheaper than 2100
def rent?(furnished, rent, baller)
(baller && rent < 2100) || (baller && furnished)
end
###
# Add your "test" ("driver") code below in order to "test drive" (run) your method above...
# The test code will call the method with different permutations of options and output the result each time.
# This way, you will be able to run the renter.rb file from the CLI and look at the output of your "tests" to validate if the method works.
# Without the test code, it will be hard for you to know if this method is working as it should or not.
@heavymetta
heavymetta / fizzbuzz.rb
Created September 1, 2015 21:17
FizzBuzz Refactor
def fizzbuzz_value(i)
if i % 5 == 0 && i % 3 == 0
"FizzBuzz"
elsif i % 5 == 0
"Buzz"
elsif i % 3 == 0
"Fizz"
else
@heavymetta
heavymetta / max.rb
Created September 1, 2015 20:23
Maximum Value
# Find the maximum
def maximum(arr)
maxNumber = arr[0]
arr.each do |item|
if item > maxNumber
end
end
return maxNumber
end
# expect it to return 42 below