Skip to content

Instantly share code, notes, and snippets.

@pengjunp
pengjunp / sort.rb
Last active April 27, 2016 05:51 — forked from davidvandusen/sort.rb
require 'benchmark'
# Sort the array from lowest to highest
def sort(arr)
swap = true
# Merge sort
while swap
swap = false
for i in 0..arr.length - 1
if i <= arr.length - 2 && arr[i] > arr[i + 1]
greater = arr[i]
@pengjunp
pengjunp / shakil.rb
Created April 26, 2016 21:44
Shakil the Dog
# 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
@pengjunp
pengjunp / renter.rb
Created April 26, 2016 19:51
The Yuppie Vancouverite
# must be baller and either furnished or rent cheaper than 2100
# def rent?(furnished, rent, baller)
# if baller && (furnished || rent < 2100)
# return true
# else
# return false
# end
# end
@pengjunp
pengjunp / fizzbuzz-task1.rb
Last active April 26, 2016 19:29
FuzzBuzz Refactor
def fuzzbuzz (start_num, end_num)
start_num.upto(end_num) do |i|
if i % 5 == 0 && i % 3 == 0
puts "FizzBuzz"
elsif i % 5 == 0
puts "Buzz"
elsif i % 3 == 0
puts "Fizz"
else
puts i
@pengjunp
pengjunp / max.rb
Last active April 26, 2016 19:33 — forked from davidvandusen/max.rb
maximum value
# Find the maximum
def maximum(arr)
result = nil
if !arr.is_a? (Array)
result = "not an array"
else
arr.each { |i| result = i if result == nil || i > result}
return result
end
end