Skip to content

Instantly share code, notes, and snippets.

@mirakui
Created December 1, 2011 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirakui/1416775 to your computer and use it in GitHub Desktop.
Save mirakui/1416775 to your computer and use it in GitHub Desktop.
うわっ…私の二分探索ショボすぎ…?
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