Created
December 1, 2011 13:36
-
-
Save mirakui/1416775 to your computer and use it in GitHub Desktop.
うわっ…私の二分探索ショボすぎ…?
This file contains hidden or 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
require "rubygems" | |
require "rest-client" | |
OFFSET = 700 | |
STEP = 100 | |
def exist?(url) | |
res = RestClient.head url | |
res.code == 200 | |
rescue | |
false | |
end | |
def p_exist?(url) | |
exist?(url).tap{|e|puts "#{url} => #{e ? 'exist' : 'not exist'}"} | |
end | |
def url(i) | |
"http://nakamurahiroki.com/neet/img/%07d.jpg" % i | |
end | |
max = OFFSET | |
max += STEP while p_exist?(url(max)) | |
min = [0, max - STEP].max | |
puts "min = #{min}, max = #{max}" | |
while max-min>1 | |
i = ((min + max) / 2.0).ceil | |
if p_exist?(url(i)) | |
min = i | |
else | |
max = i | |
end | |
puts "min = #{min}, max = #{max}" | |
end | |
puts "max url: #{url(max)}" | |
open("nenshu.html", "w+") do |f| | |
f.puts <<-END | |
<html> | |
<head></head> | |
<body> | |
END | |
(max..1).each do |i| | |
f.puts %Q!<img src="#{url(i)}" width="425" height="425" />! | |
end | |
f.puts <<-END | |
</body> | |
</html> | |
END | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment