Sha256: bd424286ca456f5ab4838887837271727d6058d30d868ede9c0dc707cfb30f18
Contents?: true
Size: 934 Bytes
Versions: 10
Compression:
Stored size: 934 Bytes
Contents
require 'usaidwat/algo' module USaidWat module Application module CountCommand 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 def algorithm(sort_by_count) sort_by_count ? USaidWat::Algorithms::CountAlgorithm : USaidWat::Algorithms::LexicographicalAlgorithm end end end end
Version data entries
10 entries across 10 versions & 1 rubygems