Created
May 22, 2013 21:20
-
-
Save seki/5631022 to your computer and use it in GitHub Desktop.
merge sort for ikezawa-san 2
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
module Enumerable | |
def merge(*other) | |
return to_enum(__method__, *other) unless block_given? | |
inputs = [self, *other] | |
while ! inputs.keep_if {|it| it.peek rescue nil}.empty? | |
yield(inputs.min_by {|it| it.peek}.next) | |
end | |
end | |
def split_for_shuffle(n = 1000000) | |
return to_enum(__method__, n) unless block_given? | |
each_slice(n) do |ary| | |
yield(ary.sort) | |
end | |
end | |
def uniq_0 | |
return to_enum(__method__) unless block_given? | |
last = nil | |
each do |curr| | |
next if last == curr | |
yield(curr) | |
last = curr | |
end | |
end | |
end | |
if __FILE__ == $0 | |
fname = 'kagero_0_000' | |
fnames = ARGF.each_line.split_for_shuffle(1000).map {|n| | |
fname = fname.succ | |
File.open(fname, 'w') {|fp| n.each {|line| fp.puts(line)}} | |
fname | |
} | |
splited = fnames.map {|fn| | |
File.open(fn).each_line | |
} | |
[].merge(*splited).uniq_0 do |it| | |
puts it | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment