Skip to content

Instantly share code, notes, and snippets.

@PRotondo
Last active December 16, 2015 18:19
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 PRotondo/5476745 to your computer and use it in GitHub Desktop.
Save PRotondo/5476745 to your computer and use it in GitHub Desktop.
My gedit external tool for compiling and testing in CodeForces problems (C++). Filenames should be formatted like {contest-number}{problem-code}.cpp , for example 294A.cpp
#!/usr/bin/env ruby
# [Gedit Tool]
# Name=CodeForces
# Shortcut=<Control>F6
# Languages=cpp
# Applicability=all
# Output=output-panel
# Input=nothing
# Save-files=document
# for info about external tools, visit https://live.gnome.org/Gedit/Plugins/ExternalTools
%w(net/http timeout).each(&method(:require))
begin
name = ENV['GEDIT_CURRENT_DOCUMENT_NAME'].match(/(.*?)\.cpp/)[1].strip
path = ENV['GEDIT_CURRENT_DOCUMENT_PATH'].match(/(.*?)\.cpp/)[1].strip
code = name.match(/(\d+)(\D\w*)/)
compile = `g++ #{ENV['GEDIT_CURRENT_DOCUMENT_PATH']} -o #{path} -O2 2>&1`
raise compile if compile.match(/error/)
warn compile if compile.match(/warning/)
res = Timeout::timeout(5) {Net::HTTP.get_response('codeforces.com',"/contest/#{code[1]}/problem/#{code[2]}")}
raise "CodeForces #{res.code} #{res.message}" if res.code != "200"
sample_tests = res.body.scan(/<pre>(.*?)<\/pre>/).each_slice(2).map(&:flatten).map!{|io| io.map!{|a| a.gsub(/<\s*br\s*\/>/,"\n").strip}}
time_limit = res.body.match(/(\d+) seconds?/)[1].to_f
sample_tests.each_with_index do |test,i|
input,output = test
`(echo \"#{input}\") > #{name}.in`
begin
t = Time.now
out = Timeout::timeout(time_limit) {`#{path} < #{name}.in`}
t2 = Time.now
if out.strip != output
puts "WA on test case #{i}:\nExpected\n#{output}\nFound\n#{out}", ''
else
printf "Test case #{i} passed in %.3f seconds\n", (t2-t)
end
rescue Exception => e
puts "#{e} on test case #{i}"
pid = `ps -A`.match(/(\d+).*?#{name}/)
Process.kill('INT', pid[1].to_i) if pid
end
end
`rm #{name}.in #{path}`
rescue Exception => e
warn e
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment