gettc 1.5 → 1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/bin/gettc +45 -29
  3. data/core/lib/gettc.rb +7 -0
  4. data/core/lib/{topcoder → gettc}/download.rb +53 -45
  5. data/core/lib/gettc/generate.rb +145 -0
  6. data/core/lib/{topcoder → gettc}/parse.rb +104 -102
  7. data/core/lib/{topcoder → gettc}/print.rb +11 -11
  8. data/core/lib/{topcoder → gettc}/problem.rb +11 -11
  9. data/core/lib/{topcoder → gettc}/signature.rb +8 -8
  10. data/core/lib/{topcoder → gettc}/types.rb +34 -17
  11. data/core/test/{topcoder → gettc}/download_test.rb +6 -6
  12. data/core/test/gettc/generate_test.rb +31 -0
  13. data/core/test/{topcoder → gettc}/parse_test.rb +26 -26
  14. data/core/test/gettc/signature_test.rb +54 -0
  15. data/core/test/gettc/types_test.rb +24 -0
  16. data/dist/config.yml +1 -0
  17. data/dist/include/cpp/engine.rb +32 -32
  18. data/dist/include/cpp/topcoder +89 -25
  19. data/dist/include/haskell/TopCoder.hs +72 -53
  20. data/dist/include/haskell/engine.rb +49 -47
  21. data/dist/include/java/TopCoder.jar +0 -0
  22. data/dist/include/java/engine.rb +71 -71
  23. data/dist/include/python/engine.rb +46 -0
  24. data/dist/include/python/topcoder/__init__.py +3 -0
  25. data/dist/include/python/topcoder/errors.py +4 -0
  26. data/dist/include/python/topcoder/reader.py +160 -0
  27. data/dist/include/python/topcoder/writer.py +13 -0
  28. data/dist/template/bin/runner.sh +16 -6
  29. data/dist/template/solve/cpp/Makefile +9 -5
  30. data/dist/template/solve/cpp/{name}.cpp +8 -8
  31. data/dist/template/solve/cpp/{name}Runner.cpp +8 -8
  32. data/dist/template/solve/haskell/Makefile +9 -5
  33. data/dist/template/solve/haskell/{name}.hs +1 -1
  34. data/dist/template/solve/haskell/{name}Runner.hs +5 -5
  35. data/dist/template/solve/java/Makefile +18 -0
  36. data/dist/template/solve/java/build.xml +7 -3
  37. data/dist/template/solve/java/{name}.java +1 -1
  38. data/dist/template/solve/java/{name}Runner.java +1 -1
  39. data/dist/template/solve/python/Makefile +27 -0
  40. data/dist/template/solve/python/{name}.py +4 -0
  41. data/dist/template/solve/python/{name}Runner.py +27 -0
  42. data/dist/template/util/check/Makefile +3 -1
  43. data/dist/usage +5 -0
  44. metadata +30 -24
  45. data/Rakefile +0 -41
  46. data/core/lib/topcoder.rb +0 -3
  47. data/core/lib/topcoder/generate.rb +0 -131
  48. data/core/test/topcoder/generate_test.rb +0 -31
  49. data/core/test/topcoder/signature_test.rb +0 -52
  50. data/core/test/topcoder/types_test.rb +0 -24
  51. data/dist/template/solve/cpp/{name}Test.cpp +0 -8
  52. data/dist/template/solve/haskell/{name}Test.hs +0 -10
  53. data/dist/template/solve/java/{name}Test.java +0 -8
@@ -0,0 +1,27 @@
1
+ #! python
2
+
3
+ import os, sys
4
+ import <%= prob.name %>
5
+
6
+ def init():
7
+ default_path = os.path.join(os.getenv("HOME"), ".gettc")
8
+ gettc_home = os.path.abspath(os.getenv("GETTC_HOME", default_path))
9
+ include_dir = os.path.join(gettc_home, "include/python")
10
+ sys.path.append(include_dir)
11
+
12
+ def main():
13
+ import topcoder as tc
14
+ with open(sys.argv[1], "r") as fi:
15
+ input = fi.read()
16
+ reader = tc.Reader(input)
17
+
18
+ <%= engine = PythonEngine.new func, vars
19
+ engine.input.gsub(/^/, ' ' * 8) %>
20
+
21
+ result = <%= prob.name %>.<%= func.name %>(<%= engine.vars_list %>)
22
+ with open(sys.argv[2], "w") as fo:
23
+ fo.write(tc.write(result, "<%= func.type %>"))
24
+
25
+ if __name__ == "__main__":
26
+ init()
27
+ main()
@@ -1,5 +1,7 @@
1
+ GETTC_HOME ?= $(HOME)/.gettc
2
+
1
3
  default : check
2
4
 
3
5
  check : check.cpp
4
6
  if [ ! -d ../../build ]; then mkdir ../../build; fi
5
- g++ -I$$HOME/.gettc/include/cpp -o ../../build/$@ $<
7
+ g++ -I$(GETTC_HOME)/include/cpp -o ../../build/$@ $<
@@ -0,0 +1,5 @@
1
+ Hello from gettc! The following commands are available:
2
+ make demo: Run the solution against the examples
3
+ make sys: Run the solution against the system tests
4
+ make sysv: Like make sys but in verbose mode to debug
5
+ make clean: Delete all temporary files
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gettc
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.5'
4
+ version: '1.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-20 00:00:00.000000000 Z
11
+ date: 2014-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hpricot
@@ -49,14 +49,6 @@ extensions: []
49
49
  extra_rdoc_files: []
50
50
  files:
51
51
  - bin/gettc
52
- - core/lib/topcoder.rb
53
- - core/lib/topcoder/download.rb
54
- - core/lib/topcoder/generate.rb
55
- - core/lib/topcoder/parse.rb
56
- - core/lib/topcoder/print.rb
57
- - core/lib/topcoder/problem.rb
58
- - core/lib/topcoder/signature.rb
59
- - core/lib/topcoder/types.rb
60
52
  - dist/config.yml
61
53
  - dist/include/cpp/engine.rb
62
54
  - dist/include/cpp/topcoder
@@ -64,6 +56,11 @@ files:
64
56
  - dist/include/haskell/engine.rb
65
57
  - dist/include/java/TopCoder.jar
66
58
  - dist/include/java/engine.rb
59
+ - dist/include/python/engine.rb
60
+ - dist/include/python/topcoder/__init__.py
61
+ - dist/include/python/topcoder/errors.py
62
+ - dist/include/python/topcoder/reader.py
63
+ - dist/include/python/topcoder/writer.py
67
64
  - dist/template/bin/runner.sh
68
65
  - dist/template/data/demo/{examples_d}
69
66
  - dist/template/data/sys/{systests_d}
@@ -73,23 +70,32 @@ files:
73
70
  - dist/template/solve/cpp/Makefile
74
71
  - dist/template/solve/cpp/{name}.cpp
75
72
  - dist/template/solve/cpp/{name}Runner.cpp
76
- - dist/template/solve/cpp/{name}Test.cpp
77
73
  - dist/template/solve/haskell/Makefile
78
74
  - dist/template/solve/haskell/{name}.hs
79
75
  - dist/template/solve/haskell/{name}Runner.hs
80
- - dist/template/solve/haskell/{name}Test.hs
76
+ - dist/template/solve/java/Makefile
81
77
  - dist/template/solve/java/build.xml
82
78
  - dist/template/solve/java/{name}.java
83
79
  - dist/template/solve/java/{name}Runner.java
84
- - dist/template/solve/java/{name}Test.java
80
+ - dist/template/solve/python/Makefile
81
+ - dist/template/solve/python/{name}.py
82
+ - dist/template/solve/python/{name}Runner.py
85
83
  - dist/template/util/check/Makefile
86
84
  - dist/template/util/check/check.cpp
87
- - Rakefile
88
- - core/test/topcoder/download_test.rb
89
- - core/test/topcoder/generate_test.rb
90
- - core/test/topcoder/parse_test.rb
91
- - core/test/topcoder/signature_test.rb
92
- - core/test/topcoder/types_test.rb
85
+ - dist/usage
86
+ - core/lib/gettc.rb
87
+ - core/lib/gettc/download.rb
88
+ - core/lib/gettc/generate.rb
89
+ - core/lib/gettc/parse.rb
90
+ - core/lib/gettc/print.rb
91
+ - core/lib/gettc/problem.rb
92
+ - core/lib/gettc/signature.rb
93
+ - core/lib/gettc/types.rb
94
+ - core/test/gettc/download_test.rb
95
+ - core/test/gettc/generate_test.rb
96
+ - core/test/gettc/parse_test.rb
97
+ - core/test/gettc/signature_test.rb
98
+ - core/test/gettc/types_test.rb
93
99
  homepage: http://seri.github.io/gettc
94
100
  licenses: []
95
101
  metadata: {}
@@ -114,8 +120,8 @@ signing_key:
114
120
  specification_version: 4
115
121
  summary: TopCoder offline arena supporting multiple languages
116
122
  test_files:
117
- - core/test/topcoder/download_test.rb
118
- - core/test/topcoder/generate_test.rb
119
- - core/test/topcoder/parse_test.rb
120
- - core/test/topcoder/signature_test.rb
121
- - core/test/topcoder/types_test.rb
123
+ - core/test/gettc/download_test.rb
124
+ - core/test/gettc/generate_test.rb
125
+ - core/test/gettc/parse_test.rb
126
+ - core/test/gettc/signature_test.rb
127
+ - core/test/gettc/types_test.rb
data/Rakefile DELETED
@@ -1,41 +0,0 @@
1
- require 'rake/testtask'
2
- require 'rubygems/package_task'
3
- require 'rubygems/installer'
4
- require 'fileutils'
5
-
6
- Rake::TestTask.new do |t|
7
- t.libs << 'core/lib'
8
- t.pattern = 'core/test/**/*_test.rb'
9
- end
10
-
11
- spec = Gem::Specification.new do |s|
12
- s.platform = Gem::Platform::RUBY
13
- s.name = 'gettc'
14
- s.summary = 'TopCoder offline arena supporting multiple languages'
15
- s.description = 'Given a TopCoder problem ID, gettc downloads the problem specification, parses the whole thing into a Markdown file, generates inputs/outputs based on the Examples and System Tests given, and finally generates basic solution files for you to get started.'
16
- s.version = '1.5'
17
-
18
- s.author = 'Seri'
19
- s.email = 'seritrinh@gmail.com'
20
- s.homepage = 'http://seri.github.io/gettc'
21
-
22
- s.files = FileList["{bin,dist,core/lib}/**/*"].to_a + ['Rakefile']
23
- s.test_files = FileList["core/test/**/*_test.rb"].to_a
24
- s.require_path = 'core/lib'
25
- s.has_rdoc = false
26
-
27
- s.bindir = 'bin'
28
- s.executables = ['gettc']
29
-
30
- s.add_dependency 'hpricot'
31
- s.add_dependency 'rdiscount'
32
- end
33
- Gem::PackageTask.new spec do |pkg|
34
- pkg.need_tar = true
35
- end
36
-
37
- task :clean do
38
- if File.exists? 'pkg' then
39
- FileUtils.rm_rf 'pkg'
40
- end
41
- end
@@ -1,3 +0,0 @@
1
- require 'topcoder/download'
2
- require 'topcoder/parse'
3
- require 'topcoder/generate'
@@ -1,131 +0,0 @@
1
- require 'fileutils'
2
- require 'erb'
3
-
4
- require 'topcoder/problem'
5
- require 'topcoder/signature'
6
- require 'topcoder/print'
7
-
8
- module TopCoder
9
- class GenerateError < StandardError
10
- end
11
- class ProblemDirExists < GenerateError
12
- attr_accessor :dir
13
- def initialize dir, msg = nil
14
- if msg.nil? then
15
- msg = 'Cannot create problem directory because it already exists'
16
- end
17
- @dir = dir
18
- super "#{msg} (#{dir})"
19
- end
20
- end
21
- class SourceDirNotExist < GenerateError
22
- attr_accessor :dir
23
- def initialize dir, msg = 'Source directory does not exist'
24
- @dir = dir
25
- super "#{msg} (#{dir})"
26
- end
27
- end
28
- class TargetDirNotExist < GenerateError
29
- attr_accessor :dir
30
- def initialize dir, msg = 'Target directory does not exist'
31
- @dir = dir
32
- super "#{msg} (#{dir})"
33
- end
34
- end
35
- class Generator
36
- def initialize source_d, target_d
37
- raise SourceDirNotExist.new source_d if not File.directory? source_d
38
- raise TargetDirNotExist.new target_d if not File.directory? target_d
39
- @source_d = source_d
40
- @target_d = target_d
41
- end
42
- def gen_images images, images_d
43
- images.each do |image|
44
- filename = File.join images_d, image.name
45
- File.open filename, 'wb' do |f| f.write image.content end
46
- end
47
- end
48
- def gen_cases cases, data_d
49
- cases.each_index do |i|
50
- c = cases[i]
51
- File.open File.join(data_d, "#{i.to_s}.in"), 'w' do |f|
52
- f.write c.input
53
- end
54
- File.open File.join(data_d, "#{i.to_s}.out"), 'w' do |f|
55
- f.write c.output
56
- end
57
- end
58
- end
59
- def gen_template source, target
60
- before = File.open source, 'r' do |f| f.read end
61
- begin
62
- after = ERB.new(before).result @context
63
- rescue StandardError => err
64
- puts "Template error (#{File.expand_path source}): "
65
- puts err.backtrace.join "\n"
66
- end
67
- File.open target, 'w' do |f| f.write after end
68
- end
69
- def filter target_d, name
70
- if name == '{images_d}' then
71
- gen_images @prob.images, target_d
72
- elsif name == '{examples_d}' then
73
- gen_cases @prob.examples, target_d
74
- elsif name == '{systests_d}' then
75
- gen_cases @prob.systests, target_d
76
- else
77
- target_n = name.gsub /\{(\w*)\}/ do |match|
78
- @prob.name if $1 == 'name'
79
- end
80
- return target_n
81
- end
82
- return nil
83
- end
84
- def load_engines
85
- include_d = File.join File.expand_path('~'), '.gettc/include'
86
- return if not File.exists? include_d
87
- Dir.foreach include_d do |name|
88
- child = File.join include_d, name
89
- if File.directory? child then
90
- engine = File.join child, 'engine.rb'
91
- require engine if File.exists? engine
92
- end
93
- end
94
- end
95
- def walk source_d, target_d
96
- Dir.foreach source_d do |name|
97
- if name != '.' and name != '..' then
98
- source_p = File.join source_d, name
99
- target_n = filter target_d, name
100
- if not target_n.nil? then
101
- target_p = File.join target_d, target_n
102
- if File.directory? source_p then
103
- FileUtils.mkdir target_p if not File.exists? target_p
104
- walk source_p, target_p
105
- elsif File.file? source_p then
106
- gen_template source_p, target_p
107
- end
108
- end
109
- end
110
- end
111
- end
112
- def generate prob
113
- @prob = prob
114
- @prob_d = File.join @target_d, prob.name
115
- raise ProblemDirExists.new @prob_d if File.exists? @prob_d
116
- FileUtils.mkdir @prob_d
117
-
118
- method_sig = @prob.definitions['Method signature']
119
- if method_sig.nil? then
120
- $stderr.puts '[Warning] No definition for method signature found'
121
- else
122
- vars = parse_method_signature method_sig
123
- func = vars.shift
124
- end
125
- @context = binding
126
-
127
- load_engines
128
- walk @source_d, @prob_d
129
- end
130
- end
131
- end
@@ -1,31 +0,0 @@
1
- require 'test/unit'
2
- require 'fileutils'
3
- require 'tmpdir'
4
-
5
- require 'topcoder/generate'
6
- include TopCoder
7
-
8
- class GenerateTest < Test::Unit::TestCase
9
- def setup
10
- @source_d = File.join File.dirname(__FILE__), '../../../dist/template'
11
- @target_d = File.join Dir.tmpdir, 'gettc'
12
- FileUtils.mkdir @target_d if not File.directory? @target_d
13
- @generator = Generator.new @source_d, @target_d
14
- end
15
- def test_initialize
16
- assert_raise SourceDirNotExist do
17
- Generator.new 'this_directory_must_not_exist', @target_d
18
- end
19
- assert_raise TargetDirNotExist do
20
- Generator.new @source_d, 'this_directory_must_not_exist'
21
- end
22
- end
23
- def test_prob_dir_exists
24
- prob = Problem.new
25
- prob.name = 'JustATest'
26
- prob_d = File.join @target_d, prob.name
27
- FileUtils.mkdir prob_d if not File.exists? prob_d
28
- assert_raise ProblemDirExists do @generator.generate prob end
29
- FileUtils.rmdir prob_d
30
- end
31
- end
@@ -1,52 +0,0 @@
1
- require 'test/unit'
2
- require 'topcoder/signature'
3
- include TopCoder
4
-
5
- class SignatureTest < Test::Unit::TestCase
6
- def test_parse_signature
7
- assert_raise CannotParseSignature do
8
- parse_signature 'invalid_signature'
9
- end
10
- assert_raise UnrecognizedType do
11
- parse_signature 'strange_type name'
12
- end
13
- assert_raise InvalidVariableName do
14
- parse_signature 'int not&ok&name'
15
- end
16
- assert_raise InvalidVariableName do parse_signature 'int 0zero' end
17
- sig = parse_signature 'String[] a_valid_nam3'
18
- assert_equal TArray.new(TString), sig.type
19
- assert_equal 'a_valid_nam3', sig.name
20
- end
21
- def test_parse_method_signature
22
- assert_raise CannotParseSignature do
23
- parse_method_signature 'there are no brackets'
24
- end
25
- assert_raise CannotParseSignature do
26
- parse_method_signature 'int main(int main())'
27
- end
28
- assert_raise CannotParseSignature do
29
- parse_method_signature 'int main(oops forget to close bracket'
30
- end
31
- method = ' int leastBorders(String[] X , int[] Y, double[] R,'
32
- method += ' char my_x1, long y1 , float x2, int[][] y2) '
33
- sigs = parse_method_signature method
34
- assert_equal 8, sigs.size
35
- assert_equal TInt, sigs[0].type
36
- assert_equal 'leastBorders', sigs[0].name
37
- assert_equal TArray.new(TString), sigs[1].type
38
- assert_equal 'X', sigs[1].name
39
- assert_equal TArray.new(TInt), sigs[2].type
40
- assert_equal 'Y', sigs[2].name
41
- assert_equal TArray.new(TDouble), sigs[3].type
42
- assert_equal 'R', sigs[3].name
43
- assert_equal TChar, sigs[4].type
44
- assert_equal 'my_x1', sigs[4].name
45
- assert_equal TLong, sigs[5].type
46
- assert_equal 'y1', sigs[5].name
47
- assert_equal TFloat, sigs[6].type
48
- assert_equal 'x2', sigs[6].name
49
- assert_equal TArray.new(TArray.new(TInt)), sigs[7].type
50
- assert_equal 'y2', sigs[7].name
51
- end
52
- end
@@ -1,24 +0,0 @@
1
- require 'test/unit'
2
- require 'topcoder/types'
3
- include TopCoder
4
-
5
- class TypesTest < Test::Unit::TestCase
6
- def test_parse_type
7
- assert_raise UnrecognizedType do array = TArray.new 123 end
8
- assert_raise UnrecognizedType do type = parse_type '' end
9
- assert_raise UnrecognizedType do type = parse_type ' ' end
10
- assert_raise UnrecognizedType do type = parse_type '[]' end
11
- assert_raise UnrecognizedType do type = parse_type 'vector' end
12
- assert_raise UnrecognizedType do type = parse_type 'vector[]' end
13
- assert_equal TBoolean, parse_type('boolean')
14
- assert_equal TInt, parse_type('int')
15
- assert_equal TLong, parse_type('long')
16
- assert_equal TFloat, parse_type('float')
17
- assert_equal TDouble, parse_type('double')
18
- assert_equal TChar, parse_type('char')
19
- assert_equal TString, parse_type('String')
20
- assert_equal TArray.new(TBoolean), parse_type('boolean[]')
21
- assert_equal TArray.new(TInt), parse_type('int[]')
22
- assert_equal TArray.new(TArray.new(TString)), parse_type('String[][]')
23
- end
24
- end
@@ -1,8 +0,0 @@
1
- #define BOOST_TEST_DYN_LINK
2
- #define BOOST_TEST_MODULE MyTest
3
- #include <boost/test/unit_test.hpp>
4
- #include "<%= prob.name %>.cpp"
5
-
6
- BOOST_AUTO_TEST_CASE (test_<%= func.name %>) {
7
- BOOST_CHECK_EQUAL(0, 0);
8
- }
@@ -1,10 +0,0 @@
1
- import Test.HUnit
2
- import <%= prob.name %>
3
- <% test_name = func.name[0].upcase + func.name[1..-1] %>
4
- test<%= test_name %> :: Test
5
- test<%= test_name %> = TestCase
6
- (assertEqual "" 0 0)
7
-
8
- tests = TestList [ TestLabel "test<%= test_name %>" test<%= test_name %> ]
9
-
10
- main = runTestTT tests