pythonchallenge.com 4 in ruby solution
Is there a 'better/alternative/more readable' solution to http://www.pythonchallenge.com/pc/def/开发者_如何学Pythonlinkedlist.php than the ruby code below? I can understand it but it just doesn't seem very clear to me...
#!/usr/bin/env ruby -w
# encoding: utf-8
require 'net/http'
Net::HTTP.start 'www.pythonchallenge.com' do |http|
nothing = 12345
nothing = case http.get("/pc/def/linkedlist.php?nothing=#{nothing}").body
when /(\d+)$/ then $1.to_i
when /by two/ then nothing / 2
when /\.html/ then puts $`
end while nothing
end
It was ok but let's try to make it a little more readable:
#!/usr/bin/env ruby -w
# encoding: utf-8
require 'net/http'
Net::HTTP.start 'www.pythonchallenge.com' do |http|
next_one = 12345
while next_one
response = http.get("/pc/def/linkedlist.php?nothing=#{next_one}").body
case response
when /Divide by two and keep going./ then
next_one = next_one / 2
when /and the next nothing is (\d+)/ then
next_one = $1.to_i
else
puts "Solution url: www.pythonchallenge.com/pc/def/linkedlist.php?nothing=#{next_one}"
next_one = false
end
end
end
精彩评论