gettc 1.10 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/bin/gettc +60 -69
  3. data/dist/config.yml +1 -1
  4. data/dist/include/cpp/engine.rb +78 -86
  5. data/dist/include/cpp/topcoder +236 -236
  6. data/dist/include/go/engine.rb +53 -61
  7. data/dist/include/haskell/engine.rb +112 -122
  8. data/dist/include/java/engine.rb +187 -184
  9. data/dist/include/javascript/engine.rb +26 -30
  10. data/dist/include/javascript/topcoder.js +3 -3
  11. data/dist/include/javascript/topcoder/errors.js +5 -5
  12. data/dist/include/javascript/topcoder/reader.js +188 -165
  13. data/dist/include/javascript/topcoder/writer.js +37 -33
  14. data/dist/include/python/engine.rb +43 -52
  15. data/dist/include/python/topcoder/__init__.pyc +0 -0
  16. data/dist/include/python/topcoder/__pycache__/__init__.cpython-34.pyc +0 -0
  17. data/dist/include/python/topcoder/__pycache__/errors.cpython-34.pyc +0 -0
  18. data/dist/include/python/topcoder/__pycache__/reader.cpython-34.pyc +0 -0
  19. data/dist/include/python/topcoder/__pycache__/writer.cpython-34.pyc +0 -0
  20. data/dist/include/python/topcoder/errors.pyc +0 -0
  21. data/dist/include/python/topcoder/reader.pyc +0 -0
  22. data/dist/include/python/topcoder/writer.pyc +0 -0
  23. data/dist/include/ruby/engine.rb +47 -52
  24. data/dist/include/ruby/topcoder/reader.rb +205 -193
  25. data/dist/include/ruby/topcoder/writer.rb +39 -37
  26. data/dist/template/bin/runner.rb +146 -151
  27. data/dist/template/bin/runner.sh +96 -96
  28. data/dist/template/prob/{name}.html +1 -1
  29. data/dist/template/solve/cpp/{name}.cpp +3 -4
  30. data/dist/template/solve/cpp/{name}Solver.cpp +14 -14
  31. data/dist/template/solve/go/{name}/{name}.go +3 -3
  32. data/dist/template/solve/go/{name}Solver.go +2 -2
  33. data/dist/template/solve/haskell/{name}.hs +6 -6
  34. data/dist/template/solve/haskell/{name}Solver.hs +10 -10
  35. data/dist/template/solve/java/{name}.java +4 -4
  36. data/dist/template/solve/java/{name}Solver.java +4 -4
  37. data/dist/template/solve/javascript/{name}.js +4 -6
  38. data/dist/template/solve/javascript/{name}Solver.js +11 -9
  39. data/dist/template/solve/python/{name}.py +1 -1
  40. data/dist/template/solve/python/{name}Solver.py +2 -2
  41. data/dist/template/solve/ruby/{name}.rb +4 -4
  42. data/dist/template/solve/ruby/{name}Solver.rb +14 -17
  43. data/dist/template/util/check/check.cpp +19 -19
  44. data/{core/lib → lib}/gettc.rb +0 -0
  45. data/lib/gettc/account.rb +14 -0
  46. data/lib/gettc/download.rb +211 -0
  47. data/lib/gettc/generate.rb +156 -0
  48. data/lib/gettc/parse.rb +237 -0
  49. data/lib/gettc/print.rb +54 -0
  50. data/lib/gettc/problem.rb +39 -0
  51. data/lib/gettc/signature.rb +63 -0
  52. data/lib/gettc/types.rb +93 -0
  53. data/lib/version.rb +3 -0
  54. data/test/gettc/download_test.rb +61 -0
  55. data/test/gettc/generate_test.rb +70 -0
  56. data/test/gettc/parse_test.rb +78 -0
  57. data/test/gettc/signature_test.rb +71 -0
  58. data/test/gettc/types_test.rb +31 -0
  59. metadata +28 -23
  60. data/core/lib/gettc/download.rb +0 -130
  61. data/core/lib/gettc/generate.rb +0 -145
  62. data/core/lib/gettc/parse.rb +0 -233
  63. data/core/lib/gettc/print.rb +0 -56
  64. data/core/lib/gettc/problem.rb +0 -33
  65. data/core/lib/gettc/signature.rb +0 -55
  66. data/core/lib/gettc/types.rb +0 -83
  67. data/core/lib/version.rb +0 -3
  68. data/core/test/gettc/download_test.rb +0 -29
  69. data/core/test/gettc/generate_test.rb +0 -31
  70. data/core/test/gettc/parse_test.rb +0 -104
  71. data/core/test/gettc/signature_test.rb +0 -54
  72. data/core/test/gettc/types_test.rb +0 -28
@@ -1,56 +0,0 @@
1
- require "gettc/problem"
2
- require "rdiscount"
3
-
4
- module Gettc
5
- class Problem
6
- def to_md
7
- out = "# [#{@name}](#{@url})\n"
8
- out << "*#{@source}*\n\n"
9
-
10
- out << "## Statement\n"
11
- out << @statement << "\n\n"
12
-
13
- unless @definitions.empty? then
14
- out << "## Definitions\n"
15
- @definitions.each do |k, v| out << "- *#{k}*: `#{v}`\n" end
16
- out << "\n"
17
- end
18
-
19
- unless @notes.empty? then
20
- out << "## Notes\n"
21
- @notes.each do |note| out << "- #{note}\n" end
22
- out << "\n"
23
- end
24
-
25
- unless @constraints.empty? then
26
- out << "## Constraints\n"
27
- @constraints.each do |constraint|
28
- out << "- #{constraint}\n"
29
- end
30
- out << "\n"
31
- end
32
-
33
- unless @examples.empty? then
34
- out << "## Examples\n"
35
- @examples.each_index do |i|
36
- example = @examples[i]
37
- out << "### Example #{i + 1}\n"
38
- out << "#### Input\n<c>"
39
- out << example.input.gsub("\n", "<br />")
40
- out << "</c>\n"
41
- out << "#### Output\n<c>"
42
- out << example.output.gsub("\n", "<br />")
43
- out << "</c>\n"
44
- unless example.reason.empty? then
45
- out << "#### Reason\n#{example.reason}\n\n"
46
- end
47
- end
48
- end
49
- return out
50
- end
51
- def to_html
52
- markdown = RDiscount.new to_md
53
- return markdown.to_html
54
- end
55
- end
56
- end
@@ -1,33 +0,0 @@
1
- module Gettc
2
- class Case
3
- attr_accessor :input, :output, :reason
4
- def initialize
5
- @input = ""
6
- @output = ""
7
- @reason = ""
8
- end
9
- end
10
- class Image
11
- attr_accessor :name, :content
12
- def initialize
13
- @name = ""
14
- @content = ""
15
- end
16
- end
17
- class Problem
18
- attr_accessor :name, :url, :source, :statement, :definitions, :notes,
19
- :constraints, :examples, :systests, :images
20
- def initialize
21
- @name = ""
22
- @url = ""
23
- @source = ""
24
- @statement = ""
25
- @definitions = { }
26
- @notes = []
27
- @constraints = []
28
- @examples = []
29
- @systests = []
30
- @images = []
31
- end
32
- end
33
- end
@@ -1,55 +0,0 @@
1
- require "gettc/types"
2
-
3
- module Gettc
4
- class SignatureError < StandardError
5
- end
6
- class CannotParseSignature < SignatureError
7
- attr_accessor :source
8
- def initialize source, msg = "Cannot parse signature"
9
- @source = source
10
- super "#{msg} (#{source}"
11
- end
12
- end
13
- class InvalidVariableName < SignatureError
14
- attr_accessor :name
15
- def initialize name, msg = "Invalid variable name"
16
- @name = name
17
- super "#{msg} (#{name})"
18
- end
19
- end
20
- class Signature
21
- attr_accessor :type, :name
22
- def initialize type, name
23
- @type = type
24
- @name = name
25
- end
26
- end
27
- def parse_signature str
28
- str.strip!
29
- parts = str.split
30
- raise CannotParseSignature.new str if parts.size != 2
31
- type = parse_type parts[0]
32
- name = parts[1]
33
- if name =~ /^[a-zA-Z_]\w*$/
34
- return Signature.new type, name
35
- else
36
- raise InvalidVariableName.new name
37
- end
38
- end
39
- def parse_method_signature str
40
- str.strip!
41
- sigs = []
42
- parts = str.split "("
43
- raise CannotParseSignature.new str if parts.size != 2
44
- sigs << parse_signature(parts[0])
45
-
46
- str = parts[1]
47
- raise CannotParseSignature.new str if str[-1] != ")"
48
- str.chop!
49
-
50
- parts = str.split ","
51
- parts.each do |sig| sigs << parse_signature(sig) end
52
-
53
- return sigs
54
- end
55
- end
@@ -1,83 +0,0 @@
1
- module Gettc
2
- TypeError = Class.new StandardError
3
- class UnsupportedType < TypeError
4
- attr_accessor :type
5
- def initialize type = nil, msg = "Not a valid TopCoder type"
6
- @type = type
7
- super "#{msg} (#{type})"
8
- end
9
- end
10
- class Type
11
- def initialize is_object
12
- @is_object = is_object
13
- end
14
- def obj?
15
- return @is_object
16
- end
17
- def to_s
18
- if is_a? TArray then
19
- return subtype.to_s + "[]"
20
- end
21
-
22
- case self
23
- when TInt
24
- return "int"
25
- when TLong
26
- return "long"
27
- when TFloat
28
- return "float"
29
- when TDouble
30
- return "double"
31
- when TChar
32
- return "char"
33
- when TString
34
- return "String"
35
- when TBoolean
36
- return "boolean"
37
- end
38
-
39
- return "unknown"
40
- end
41
- end
42
- TBoolean = Type.new false
43
- TInt = Type.new false
44
- TLong = Type.new false
45
- TFloat = Type.new false
46
- TDouble = Type.new false
47
- TChar = Type.new false
48
- TString = Type.new true
49
- class TArray < Type
50
- attr_accessor :subtype
51
- def initialize subtype
52
- raise UnsupportedType.new subtype unless subtype.is_a? Type
53
- @subtype = subtype
54
- end
55
- def == ary
56
- return false unless ary.is_a? TArray
57
- return @subtype == ary.subtype
58
- end
59
- def obj?
60
- return true
61
- end
62
- end
63
- def parse_type str
64
- return TArray.new parse_type str[0 .. -3] if str[-2 .. -1] == "[]"
65
- case str
66
- when "boolean"
67
- return TBoolean
68
- when "int"
69
- return TInt
70
- when "long"
71
- return TLong
72
- when "float"
73
- return TFloat
74
- when "double"
75
- return TDouble
76
- when "char"
77
- return TChar
78
- when "String"
79
- return TString
80
- end
81
- raise UnsupportedType.new str
82
- end
83
- end
@@ -1,3 +0,0 @@
1
- module Gettc
2
- VERSION = "1.10"
3
- end
@@ -1,29 +0,0 @@
1
- require "test/unit"
2
- require "gettc/download"
3
- include Gettc
4
-
5
- class DownloadTest < Test::Unit::TestCase
6
- def setup
7
- @account = Account.new "gettc", "algorithm"
8
- end
9
- def test_wrong_account
10
- assert_raises LoginFailed do
11
- Downloader.new Account.new "username", "password"
12
- end
13
- end
14
- def test_wrong_id
15
- assert_raises IDNotAvailable do
16
- (Downloader.new @account).download_problem 1000000
17
- end
18
- end
19
- def test_download_ok
20
- ids = [10297, 10324, 10329, 10330, 10505, 10685, 10686, 10690, 11264,
21
- 11266, 11303, 11315, 11322, 11350, 11357, 11419, 8763, 8819, 9995]
22
- downloader = Downloader.new @account
23
- 3.times do
24
- id = ids[rand ids.size]
25
- html = downloader.download_problem id
26
- assert_match "<h3>Problem Statement</h3>", html
27
- end
28
- end
29
- end
@@ -1,31 +0,0 @@
1
- require "test/unit"
2
- require "fileutils"
3
- require "tmpdir"
4
-
5
- require "gettc/generate"
6
- include Gettc
7
-
8
- class GenerateTest < Test::Unit::TestCase
9
- def setup
10
- @source_d = File.join File.dirname(__FILE__), "../../dist"
11
- @target_d = File.join Dir.tmpdir, "gettc"
12
- FileUtils.mkdir @target_d unless 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 unless File.exists? prob_d
28
- assert_raise ProblemDirExists do @generator.generate prob end
29
- FileUtils.rmdir prob_d
30
- end
31
- end
@@ -1,104 +0,0 @@
1
- require "test/unit"
2
- require "gettc/parse"
3
- include Gettc
4
-
5
- class ParseTest < Test::Unit::TestCase
6
- def setup
7
- downloader = Downloader.new Account.new "gettc", "algorithm"
8
- @parser = Parser.new downloader
9
- @data_d = File.join File.dirname(__FILE__), "../../temp/download_problem_statement"
10
- end
11
- def get_problem_raw prob
12
- return File.read File.join @data_d, prob + ".htm"
13
- end
14
- def test_indexes
15
- assert_equal [0, 3], @parser.send(:indexes, "abcde", "bc")
16
- assert_equal nil, @parser.send(:indexes, "abcde", "f")
17
- end
18
- def test_filter
19
- assert_equal "Lorem Ipsum", @parser.send(:filter, "<xml> Lorem Ipsum </xml> ")
20
- assert_equal "2^(3)", @parser.send(:filter, "2<sup>3</sup>")
21
- assert_equal "*hi*", @parser.send(:filter, " <b>hi</b>")
22
- html = <<-END
23
- <img src=
24
- "http://www.topcoder.com/contest/problem/CirclesCountry/case1.gif">
25
- END
26
- assert_equal "![image](images/case1.gif)", @parser.send(:filter, html)
27
- end
28
- def test_PageNumbers
29
- html = get_problem_raw "PageNumbers"
30
- prob = @parser.parse html
31
- assert_equal "PageNumbers", prob.name
32
- assert_equal 5, prob.definitions.size
33
- assert_equal 1, prob.notes.size
34
- assert_equal 1, prob.constraints.size
35
- assert_equal 5, prob.examples.size
36
- assert_equal 118, prob.systests.size
37
- assert_equal 0, prob.images.size
38
- end
39
- def test_CirclesCountry
40
- html = get_problem_raw "CirclesCountry"
41
- prob = @parser.parse html
42
- assert_equal "CirclesCountry", prob.name
43
- assert_equal 5, prob.definitions.size
44
- assert_equal 0, prob.notes.size
45
- assert_equal 7, prob.constraints.size
46
- assert_equal 5, prob.examples.size
47
- assert_equal 228, prob.systests.size
48
- assert_equal 4, prob.images.size
49
- end
50
- def test_TheTournamentDivOne
51
- html = get_problem_raw "TheTournamentDivOne"
52
- prob = @parser.parse html
53
- assert_equal "TheTournamentDivOne", prob.name
54
- assert_equal 5, prob.definitions.size
55
- assert_equal 0, prob.notes.size
56
- assert_equal 4, prob.constraints.size
57
- assert_equal 4, prob.examples.size
58
- assert_equal 0, prob.systests.size
59
- assert_equal 0, prob.images.size
60
- end
61
- def test_FunnyGames
62
- html = get_problem_raw "FunnyGames"
63
- prob = @parser.parse html
64
- assert_equal "FunnyGames", prob.name
65
- assert_equal 5, prob.definitions.size
66
- assert_equal 0, prob.notes.size
67
- assert_equal 6, prob.constraints.size
68
- assert_equal 5, prob.examples.size
69
- assert_equal 0, prob.systests.size
70
- assert_equal 0, prob.images.size
71
- end
72
- def test_BuildingRoads
73
- html = get_problem_raw "BuildingRoads"
74
- prob = @parser.parse html
75
- assert_equal "BuildingRoads", prob.name
76
- assert_equal 5, prob.definitions.size
77
- assert_equal 2, prob.notes.size
78
- assert_equal 10, prob.constraints.size
79
- assert_equal 5, prob.examples.size
80
- assert_equal 0, prob.systests.size
81
- assert_equal 2, prob.images.size
82
- end
83
- def test_Acronyms
84
- html = get_problem_raw "Acronyms"
85
- prob = @parser.parse html
86
- assert_equal "Acronyms", prob.name
87
- assert_equal 5, prob.definitions.size
88
- assert_equal 1, prob.notes.size
89
- assert_equal 8, prob.constraints.size
90
- assert_equal 8, prob.examples.size
91
- assert_equal 39, prob.systests.size
92
- assert_equal 0, prob.images.size
93
- end
94
- def test_BackyardTrees
95
- html = get_problem_raw "BackyardTrees"
96
- prob = @parser.parse html
97
- assert_equal "BackyardTrees", prob.name
98
- assert_equal 5, prob.definitions.size
99
- assert_equal 0, prob.notes.size
100
- assert_equal 4, prob.constraints.size
101
- assert_equal 6, prob.examples.size
102
- assert_equal 2, prob.images.size
103
- end
104
- end
@@ -1,54 +0,0 @@
1
- require "test/unit"
2
- require "gettc/signature"
3
- include Gettc
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 UnsupportedType 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
17
- parse_signature "int 0zero"
18
- end
19
- sig = parse_signature "String[] a_valid_nam3"
20
- assert_equal TArray.new(TString), sig.type
21
- assert_equal "a_valid_nam3", sig.name
22
- end
23
- def test_parse_method_signature
24
- assert_raise CannotParseSignature do
25
- parse_method_signature "there are no brackets"
26
- end
27
- assert_raise CannotParseSignature do
28
- parse_method_signature "int main(int main())"
29
- end
30
- assert_raise CannotParseSignature do
31
- parse_method_signature "int main(oops forget to close bracket"
32
- end
33
- method = " int leastBorders(String[] X , int[] Y, double[] R,"
34
- method += " char my_x1, long y1 , float x2, int[][] y2) "
35
- sigs = parse_method_signature method
36
- assert_equal 8, sigs.size
37
- assert_equal TInt, sigs[0].type
38
- assert_equal "leastBorders", sigs[0].name
39
- assert_equal TArray.new(TString), sigs[1].type
40
- assert_equal "X", sigs[1].name
41
- assert_equal TArray.new(TInt), sigs[2].type
42
- assert_equal "Y", sigs[2].name
43
- assert_equal TArray.new(TDouble), sigs[3].type
44
- assert_equal "R", sigs[3].name
45
- assert_equal TChar, sigs[4].type
46
- assert_equal "my_x1", sigs[4].name
47
- assert_equal TLong, sigs[5].type
48
- assert_equal "y1", sigs[5].name
49
- assert_equal TFloat, sigs[6].type
50
- assert_equal "x2", sigs[6].name
51
- assert_equal TArray.new(TArray.new(TInt)), sigs[7].type
52
- assert_equal "y2", sigs[7].name
53
- end
54
- end