Skip to content

Instantly share code, notes, and snippets.

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.")
@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:
@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
# 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
# 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.
=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 / 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