Skip to content

Instantly share code, notes, and snippets.

@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|
# 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
# 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.
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"
@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}"