Skip to content

Commit

Permalink
Slightly faster
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Light committed Feb 5, 2011
1 parent 549cbc3 commit 91a6f37
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions wide_finder.rb
@@ -1,21 +1,18 @@
class WideFinder
def initialize(filename)
url_histogram = nil
url_histogram = Hash.new(0)
File.open(filename, "r") do |handle|
while line = handle.gets
line =~ /"([A-Z]+)\s([^\s\?]+)/
url = $2
hash[url] += 1
hash
url_histogram[$2] += 1
end
end
unformatted_results = Array(url_histogram).sort! { |a, b| a[1] <=> b[1] }.reverse
@results = unformatted_results.inject([]) do |results, result|
results << "#{result[1]}: #{result[0]}"
end
@results = url_histogram.sort { |a, b| a[1] <=> b[1] }.reverse
end

def results(how_many)
@results[0, how_many]
@results[0,how_many].inject([]) do |results, result|
results << "#{result[1]}: #{result[0]}"
end
end
end

0 comments on commit 91a6f37

Please sign in to comment.