This file contains hidden or 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
# 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}" |
This file contains hidden or 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 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" |
This file contains hidden or 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
# 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. |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
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| |
This file contains hidden or 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 get_quote(dimensions, colour) | |
(dimensions * 15 + (colour > 2 ? colour * 15 : colour * 10))*1.15 | |
end | |
puts "#{get_quote(10, 3)}" |
This file contains hidden or 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) | |
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) |
This file contains hidden or 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 hidden or 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) | |
# count={} | |
# str.strip.each_char do |char| | |
# count[char] = 0 unless count.include?(char) | |
# count[char] += 1 | |
# end | |
# count | |
# end | |
def count_letters(str) |
This file contains hidden or 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
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']] | |
} |
OlderNewer