Skip to content

Instantly share code, notes, and snippets.

@McTano
McTano / max.rb
Last active May 24, 2016 20:22 — forked from davidvandusen/max.rb
require 'pry'
# Find the maximum
def maximum(arr)
# arr.max
big = arr[0]
arr.each do |x|
big = x if big < x
end
big
=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)
# 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.
# 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
@McTano
McTano / sort.rb
Last active May 25, 2016 03:26 — forked from davidvandusen/sort.rb
# 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
@McTano
McTano / debug01.rb
Last active May 25, 2016 19:37 — forked from monicao/debug01.rb
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:
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.")
# @states = {
# OR: 'Oregon',
# FL: 'Florida',
# CA: 'California',
# NY: 'New York',
# MI: 'Michigan'
# }
# # Task 1
# @states[:MN] = 'Minnesota'
NUMERALS = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
LX: 40,
X: 10,
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