Skip to content

Instantly share code, notes, and snippets.

@goofiw
goofiw / IRB Play
Created April 28, 2015 18:25
Playing in IRB
2.1.3 :003 > fun say_hi {
2.1.3 :004 > puts "Hi
2.1.3 :005"> }
2.1.3 :006">
2.1.3 :007">
2.1.3 :008">
2.1.3 :009">
2.1.3 :010"> "Bob"
2.1.3 :011">
2.1.3 :012">
@goofiw
goofiw / max
Created April 28, 2015 19:12
Array#max
# Find the maximum
def maximum(arr)
max = arr[0]
arr.each { |x| max = x if x > max }
max
end
@goofiw
goofiw / fizzbuzz
Created April 28, 2015 19:27
Fizzbuzz method choose your start and stop
def fizzbuzz(start, finish)
for x in start .. finish
if x % 3 == 0
puts "Fizz"
elsif x % 5 == 0
puts "Buzz"
else
puts x
end
end
@goofiw
goofiw / yuppie
Created April 28, 2015 19:47
Vancouver Yuppie
# must be baller and either furnished or rent cheaper than 2100
def rent?(furnished, rent, baller)
baller && (furnished || rent < 2100) ? true : false
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.
@goofiw
goofiw / Shakil
Last active August 29, 2015 14:20
Shakil the asshole
def shakil_response(input)
case input
when"woof"
puts "WOOF " * 3
when "shakil stop" || "Shakil STOP!"
puts "..."
when "meow"
@goofiw
goofiw / BubbleSort
Last active August 29, 2015 14:20
Bubble sort
# Sort the array from lowest to highest
def sort(arr)
swaps = 1
while(swaps != 0)
swaps = 0
for index in 0 .. (arr.size - 2)
if arr[index] > arr[index + 1]
swaps++
temp = arr[index]
arr[index] = arr[index + 1]
@goofiw
goofiw / signs
Created April 28, 2015 21:54
Signs atomically method singular
def colour_cost(colours)
num_colours = colours.size
num_colours > 2 ? 15 * num_colours : 10 * num_colours
end
@goofiw
goofiw / Count_Letters
Created April 29, 2015 19:49
count letters and count_with_indexes
def count(string)
let_counts = {}
string.split(//).each { |let| let_counts[let] ? let_counts[let] += 1 : let_counts[let] = 1 }
let_counts
end
puts count("lea asdjflasdfl asidufoaubvoahboaeurfaw")
puts count ("maybe a better count zzzzzzz ooo")
@goofiw
goofiw / romans
Last active August 29, 2015 14:20
Modern and Ancient Roman Numerals
@numbers = {
M: 1000,
D: 500,
C: 100,
L: 50,
X: 10,
V: 5,
I: 1
}
@goofiw
goofiw / state_info
Created April 30, 2015 00:55
State_info - All but combining into one hash
def describe_state(state_code)
first_part = "#{state_code} is for #{@states[state_code.to_sym]}.\
It has #{@cities[state_code.to_sym].length} major cities:"
@cities[state_code.to_sym].each { |city| first_part << " #{city},"}
first_part.chomp(',')
end
def calculate_tax(state_code, amount)
if @taxes.keys.includes?(state_code.to_sym)
'%.2f' % (amount * (1 + @taxes[state_code.to_sym] / 100)).round(2)