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
# 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
=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)
@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'
# Determine whether a string contains a SIN (Social Insurance Number).
# A SIN is 9 digits and we are assuming that they must have dashes in them
def has_sin?(string)
!(string !~ /(?<=\D|^)(\d{3}-\d{3}-\d{3})(?=\D|$)/)
end
puts "has_sin? returns true if it has what looks like a SIN"
puts has_sin?("please don't share this: 234-604-142") == true
puts "has_sin? returns false if it doesn't have a SIN"
def add_tax(cost, tax_rate)
price + (price * tax_rate)
end
def sign_cost(height, width, color_count)
subtotal = height * width * 15
color_modifier = (color_count <= 2) ? 10 : 15
subtotal += subtotal * color_modifier
total = add_tax(subtotal, 0.15)