Skip to content

Instantly share code, notes, and snippets.

@Leejojo
Created June 23, 2016 12:53
Show Gist options
  • Save Leejojo/35ae7df4f7122b0dc45a278a772839c1 to your computer and use it in GitHub Desktop.
Save Leejojo/35ae7df4f7122b0dc45a278a772839c1 to your computer and use it in GitHub Desktop.
Debug
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 average(numbers)
sum = 0
numbers.each do |num|
sum += num.to_i.to_f
end
sum.to_i / numbers.size
end
## TEST HELPER METHOD
def test_average(*array)
array.flatten!
print "avg of #{array.inspect}:"
result = average(array)
p result.round(1)
end
## TEST CODE
test_average([4,5,6]) # => 5
test_average([15,5,10]) # => 10
# Should treat string like number
test_average([15,'5',10]) # => 10
# Should show decimal value
test_average([10, 5]) # => 7.5 instead of just 7
# Watch out! Even tests can have bugs!
test_average(9, 5, 7)
# Empty set should return nil, not throw an error
test_average([]) # => nil
# Non-existent set should return nil
test_average() # => nil
# BONUS: Should ignore nils in the set
test_average([9,6,nil,3]) # => 6
def sum(list)
# list.each do |ele|
# sum = 0
# sum += ele
# end
list.inject(:+)
#sum
end
list1 = [16,21,31,42,55]
# 1. The following should return 165 instead of an error
puts sum(list1)
# 2. How would you refactor it using an enumerable method other than each? Examples of enumerables: map, select, inject, reject, detect.
def char_count (list)
new_list = list.join(",")
result = new_list.split('')
hash = Hash.new(0)
result.each.inject(hash) {|key, value|
hash[value] += 1
}
p hash
end
# Why the long face(error)?
# 1. This should return count of each letter in the list
# ex: { "a" => 4, "p" => 3, "l" => 1 ...}
puts char_count(['apples', 'oranges', 'hipsters', 'are', 'same'])
# 2. What are the improvements you can do to above code?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment