This file contains 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' | |
# Find the maximum | |
def maximum(arr) | |
# arr.max | |
big = arr[0] | |
arr.each do |x| | |
big = x if big < x | |
end | |
big |
This file contains 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
=begin | |
The normal way: | |
def fizzbuzz(first, last) | |
first.upto(last) do |i| | |
if dividesby?(i,3) && dividesby?(i,5) | |
puts "FizzBuzz" | |
elsif dividesby?(i,3) |
This file contains 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 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
# checks for all the conditions that make shakil momentarily shut up. | |
def said_magic_words?(input) | |
input.include?("treat") || | |
input.include?("shakil") && input.include?("stop") | |
end | |
# fancy prompt a la James | |
def prompt | |
print "> " | |
gets.chomp.downcase |
This file contains 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
# Sort the array from lowest to highest | |
def sort(arr) | |
arr.sort | |
end | |
def swap(arr, x, y) | |
temp = arr[x] | |
arr[x] = arr[y] | |
arr[y] = temp | |
end |
This file contains 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[0] treats 0 as a key, not an integer index. | |
# we can call the first element like so, assuming we want the value. | |
#p list.values[0] | |
# if we want the key, we can do it like so: |
This file contains 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(string) | |
letters = Hash.new {|hash, key| hash[key] = [] } | |
string.chars.each_with_index do |c, i| | |
letters[c] << i | |
end | |
letters | |
end | |
puts count_letters("Hello World") | |
puts count_letters("I'm the goddamn batman.") |
This file contains 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
# @states = { | |
# OR: 'Oregon', | |
# FL: 'Florida', | |
# CA: 'California', | |
# NY: 'New York', | |
# MI: 'Michigan' | |
# } | |
# # Task 1 | |
# @states[:MN] = 'Minnesota' |
This file contains 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
NUMERALS = { | |
M: 1000, | |
CM: 900, | |
D: 500, | |
CD: 400, | |
C: 100, | |
XC: 90, | |
L: 50, | |
LX: 40, | |
X: 10, |
This file contains 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 bottles_purchased | |
money_redeemed = @money - (@money % 2) | |
empties_redeemed = @empties - (@empties % 2) | |
caps_redeemed = @caps - (@caps % 4) | |
@money -= money_redeemed | |
@total_money_redeemed += money_redeemed | |
@empties -= empties_redeemed | |
@total_empties_redeemed += empties_redeemed |
OlderNewer