Last active
August 29, 2015 14:17
-
-
Save Nirma/05fc430d189705dd6d76 to your computer and use it in GitHub Desktop.
This file contains 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
=begin | |
This fuction is designed to summarize the contents of a dictionary of arrays. | |
As many elements as possible will be grabbed for a specified size N while maintaining maximum diversity. | |
Function will terminate as soon as the desired amount of items are gathered. | |
Example IO: | |
posts = { 'Greg' => ['A','A','A'], | |
'Steve' => [], | |
'Elly' => ['C','C','C'], | |
'George' => ['D'] | |
} | |
samplify(2, posts) #Output: ['A','C'] | |
samplify(5, posts) #Output: ['A','A','C','C','D'] | |
- Nick Maccharoli | |
March, 2015 | |
=end | |
class Hash | |
def purify! | |
self.each{|h| self.delete(h[0]) if h[1].length == 0} | |
end | |
end | |
def samplify(sampleSize, dic) | |
dic.purify! | |
mixedItems = [] | |
grabPerRow = [(sampleSize / dic.keys.length), 1].max | |
grabPerRow += (grabPerRow % 2) | |
setsVisited = 0 | |
dic.keys.each_with_index{ |item, index| | |
break if setsVisited == sampleSize | |
currentRow = [] | |
begin | |
numberToTake = [sampleSize, item.length].min | |
currentRow = dic[item].take numberToTake | |
grabIndex = [(index * grabPerRow),mixedItems.length].min | |
currentRow.each{|element| | |
mixedItems[grabIndex] = element | |
grabIndex += 1 | |
} | |
setsVisited += 1 | |
rescue | |
end | |
} | |
return mixedItems.slice(0,sampleSize) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment