6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/usaidwat/count.rb', line 6
def partition(entries, sort_by_count)
longest_subreddit = 0
buckets = Hash.new { |hash, key| hash[key] = 0 }
entries.each do |e|
subreddit = e.subreddit
longest_subreddit = subreddit.length if subreddit.length > longest_subreddit
buckets[subreddit] += 1
end
algo = algorithm(sort_by_count).new(buckets)
subreddits = buckets.keys.sort { |a,b| algo.sort(a, b) }
counts = subreddits.map { |s| buckets[s] }
subreddit_counts = subreddits.zip(counts)
partition_data = Struct.new(:longest, :counts)
partition_data.new(longest_subreddit, subreddit_counts)
end
|