Skip to content

Instantly share code, notes, and snippets.

@kaiguogit
kaiguogit / max.rb
Last active July 20, 2016 18:29 — forked from davidvandusen/max.rb
# Find the maximum
def maximum(arr)
arr.inject(arr[0]) do |sum,a|
a > sum ? a : sum
end
end
# expect it to return 42 below
result = maximum([2, 42, 22, 02])
puts "max of 2, 42, 22, 02 is: #{result}"
def fizzbuzz(startnumber,endnumber)
#check input for Fixnum
if !(startnumber.is_a?(Fixnum) && endnumber.is_a?(Fixnum))
puts "invalid input"
return nil
end
#check start number is smaller than end number
if startnumber > endnumber
puts "Start number #{startnumber} is greater than end number #{endnumber}, reverting the loop"
# must be baller and either furnished or rent cheaper than 2100
def rent?(furnished, rent, baller)
baller && (furnished || rent < 2100)
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.
# Save this file to your computer so you can run it
# via the command line (Terminal) like so:
# $ ruby shakil_the_dog.rb
#
# Your method should wait for user input, which corresponds
# to you saying something to your dog (named Shakil).
# You'll probably want to write other methods, but this
# encapsulates the core dog logic
def shakil_the_dog
@kaiguogit
kaiguogit / sort.rb
Last active July 20, 2016 18:29 — forked from davidvandusen/sort.rb
require 'benchmark'
# Sort the array from lowest to highest
def sort(arr)
#bubble sort
return arr if arr.length <= 1
sorted = true
while sorted
sorted = false
(arr.length-1).times do |i|
@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)}"
@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)
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 / 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)
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']]
}