Forked from dbc-challenges/phase0_template_gist.rb
Last active
January 3, 2016 02:19
-
-
Save carolineartz/d4507d601d3e2bd9fb17 to your computer and use it in GitHub Desktop.
Write a method mode which takes an Array of numbers as its input and returns an Array of the most frequent values. If there's only one most-frequent value, it returns a single-element Array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mode(array) | |
array_of_modes = [] | |
#NOTE: | |
#Hash.new(0) differs from defining an empty hash by {} in that: | |
# 1. Hash.new(0) defines an empty hash with a default value of 0 for new keys with undefined values. | |
# 2. {} defines an empty hash that will default to nil for new keys with undefined values. | |
#Here, executing the block on nil will result in NoMethodError as + is undefined for nil, so must Hash.new(0) | |
##############CREATE OUR FREQUENCIES HASH######### | |
frequencies = array.each_with_object(Hash.new(0)){|key, hash| hash[key] += 1} | |
# NOTE: | |
# Using #each_with_object(Hash.new(0)) iterates the specified block for each array element with the object (i.e., the empty hash) | |
# #each_with_object(obejct) always returns the given object. returns the our new hash "frequences", populated with the array element, frequency pairs. | |
##############DETERMINE THE COUNT OF THE HIGHEST FREQUENCY################# | |
highest_frequency = frequencies.invert.sort.last.first | |
# breakdown when running on array: [1, 4, 3, 5, 6, 7, 8, 2, 1, 5, 6, 5, 3, 3, 3, 5] | |
#frequencies #{1=>2, 4=>1, 3=>4, 5=>4, 6=>2, 7=>1, 8=>1, 2=>1} | |
#frequencies.invert #{2=>6, 1=>2, 4=>5} --> swap values as keys; 4=>3 gets overridden by redefining 4=>5 | |
#frequencies.invert.sort #[[1, 2], [2, 6], [4, 5]] --> can't sort a by pair, but can sort based on hash key to a 2D array of the key/value pairs | |
#print frequencies.invert.sort.last --> snag the last element array that holds the elements telling us [highest frequency, a mode value], but there may be more than one mode | |
#print frequencies.invert.sort.last.first --> snag the first element we know tells us frequency of mode | |
#######USE FREQUENCY HASH TO COMPARE FREQs OF THE ARRAY ELS TO HIGHEST FREQUENCY###### | |
frequencies.each_pair do |el, count| | |
array_of_modes << el if count == highest_frequency #push any keys with values matching the highest frequency to the array of modes | |
end | |
return array_of_modes | |
end | |
# I didn't complete this challenge to the extent of the required challenges to include refactoring the code, etc. | |
# I'm running out of time to submit that this evening but I wanted to post what I learned from the work that I did. | |
# I spent a good amount of time deciphering the above code that I was having a tough time working through. Through the | |
# process I learned some new methods and gained a better understanding of ones we have been using often. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment