Skip to content

Instantly share code, notes, and snippets.

@Nirma
Last active Aug 29, 2015
Embed
What would you like to do?
=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