Skip to content

Instantly share code, notes, and snippets.

View laurenachoo's full-sized avatar

Lauren laurenachoo

  • Vancouver, BC Canada
View GitHub Profile
def char_count(list)
letters = Hash.new(0)
list.each do |word|
word.split('').each { |letter| letters[letter] += 1 }
end
letters
end
# Why the long face(error)?
# 1. This should return count of each letter in the list
def sum(list)
total = 0
list.each do |ele| total += ele
end
total
end
list1 = [16,21,31,42,55]
# 1. The following should return 165 instead of an error
def average(numbers)
return nil if numbers.empty?
sum = 0
numbers.each do |num|
sum += num.to_f
end
sum / numbers.size
end
## TEST HELPER METHOD
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 [:yvr]
def range(a, b)
a.upto(b) { |x| puts fizzbuzz(x)}
end
def fizzbuzz(num)
if divby3(num) && divby5(num)
"FizzBuzz"
elsif divby5(num)
"Buzz"
elsif divby3(num)
def size_finder
puts 'How long is your sign? '
length = gets.chomp.to_f
puts 'How wide is your sign? '
width = gets.chomp.to_f
@total_size = length*width
end
# Sort the array from lowest to highest
def sort(arr)
#is storing all the parts of the array
n = arr.length
arr_copy = arr.clone
#this will keep looping this code until it cannot swap anymore
loop do
swapped = false
(n-1).times do |i|
if arr_copy[i] > arr_copy[i + 1]
# Save this file to your computer so you can run it
# via the command line (Terminal) like so:
# $ ruby shakil_the_dog.rb
#
# Your method should wait for user input, which corresponds
# to you saying something to your dog (named Shakil).
# You'll probably want to write other methods, but this
# encapsulates the core dog logic
def shakil_the_dog
@laurenachoo
laurenachoo / README.md
Last active August 29, 2015 14:20 — forked from kvirani/README.md

The Yuppie Vancouverite

The Yuppie Vancouverite needs to rent an apartment downtown!

Here we have a rent? method that helps them decide if they are going to an apartment based on some information about it. It's passed 3 key pieces of information that are used to determine this:

  1. Is the apartment furnished? Since this is a yes/no thing, it's a boolean (true/false)
  2. Is the apartment baller? Since this is a yes/no thing, it's a boolean (true/false)
  3. The monthly rent for the apartment. This is expected to be an integer (Fixnum)
@laurenachoo
laurenachoo / README.md
Last active August 29, 2015 14:20 — forked from kvirani/README.md

Create a directory in your /vagrant folder (while ssh'd into your Vagrant VM). Within that directory, clone your fork of this repo, which contains one ruby file max.rb.

Spend the time necessary to read and fully understand what the code in max.rb is doing. Google or discuss as necessary. Have an expectation of what will be output when you first run the code, then use the ruby command to run the max.rb file from the terminal.

Task: Currently, the built-in Array#max method is being used (line 3) to implement the logic for the maximum method. As an exercise, instead of leveraging this built-in method, implement your own logic such that the maximum method continues to work the same way that it was, and the resulting output from this ruby script stays the same. Note: you also cannot use Ruby's built-in sort method.