Skip to content

Instantly share code, notes, and snippets.

@ruxandrafed
Forked from rafd/debug01.rb
Last active September 7, 2015 19:01
Show Gist options
  • Save ruxandrafed/f8981bba1a13ce65b3dd to your computer and use it in GitHub Desktop.
Save ruxandrafed/f8981bba1a13ce65b3dd to your computer and use it in GitHub Desktop.
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)
return nil if numbers.empty? # returns nil if array is empty
numbers = numbers.reject {|no| no == nil} # removing nil elements from the array
sum = 0
numbers.each do |num|
sum += num.to_f # fixes TypeError when running test with array containing string elements
end
sum % numbers.size == 0 ? (sum / numbers.size).to_i : sum / numbers.size # only show decimals if needed
end
## TEST HELPER METHOD
def test_average(array = []) # defaults to an empty array if no argument is sent
print "avg of #{array.inspect}:"
result = average(array)
p result
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)
ele_sum = 0
list.each do |ele|
ele_sum += ele
end
ele_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 sum_refactored(list)
list.reduce (:+)
end
puts sum_refactored(list1)
def char_count(list)
letters = Hash.new(0) # set default value 0 (instead of nil) so as to be able to use + method at line 4 and fix error
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
list = ['apples', 'oranges', 'hipsters', 'are', 'same']
puts char_count(list)
# 2. What are the improvements you can do to above code?
def char_count_improved(list)
letters = Hash.new(0)
# joined all elements of the array into a string and iterated through each character
list.join.each_char { |letter| letters[letter] += 1 }
letters
end
puts char_count_improved(list)
# 3. Benchmarking the two methods
require 'benchmark'
char_count_benchmark = Benchmark.measure {1000.times {char_count(list)}}
char_count_improved_benchmark = Benchmark.measure {1000.times {char_count_improved(list)}}
puts "Result of benchmarking original method:\t#{char_count_benchmark.total.round(3)}"
puts "Result of benchmarking improved method:\t#{char_count_improved_benchmark.total.round(3)}"
def method1(a)
method2(a)
end
def method2(a)
method3(a)
end
def method3(a)
puts a
end
method1(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment