gettc 1.5 → 1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,24 @@
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,2 +1,3 @@
1
1
  username: gettc
2
2
  password: algorithm
3
+ version: 1.6
@@ -1,58 +1,58 @@
1
- require 'topcoder/types'
1
+ require "gettc/types"
2
2
 
3
- module TopCoder
3
+ module Gettc
4
4
  class Type
5
5
  def to_cpp
6
6
  if self == TInt then
7
- return 'int'
7
+ return "int"
8
8
  elsif self == TLong then
9
- return 'int64'
9
+ return "int64"
10
10
  elsif self == TFloat then
11
- return 'float'
11
+ return "float"
12
12
  elsif self == TDouble then
13
- return 'double'
13
+ return "double"
14
14
  elsif self == TChar then
15
- return 'char'
15
+ return "char"
16
16
  elsif self == TString then
17
- return 'string'
17
+ return "string"
18
18
  elsif self == TBoolean then
19
- return 'bool'
19
+ return "bool"
20
20
  elsif is_a? TArray then
21
- ret = 'vector<' << subtype.to_cpp
22
- ret << ' ' if subtype.is_a? TArray
23
- ret << '>'
21
+ ret = "vector<" << subtype.to_cpp
22
+ ret << " " if subtype.is_a? TArray
23
+ ret << ">"
24
24
  return ret
25
25
  else
26
- return 'unknown'
26
+ return "unknown"
27
27
  end
28
28
  end
29
29
  def dumb_cpp
30
30
  if self == TInt then
31
- return '0'
31
+ return "0"
32
32
  elsif self == TLong then
33
- return '0'
33
+ return "0"
34
34
  elsif self == TFloat then
35
- return '0'
35
+ return "0"
36
36
  elsif self == TDouble then
37
- return '0'
37
+ return "0"
38
38
  elsif self == TChar then
39
- return "'$'"
39
+ return "\"$\""
40
40
  elsif self == TString then
41
- return '"$"'
41
+ return "\"$\""
42
42
  elsif self == TBoolean then
43
- return 'true'
43
+ return "true"
44
44
  elsif is_a? TArray then
45
45
  return "#{to_cpp}()"
46
46
  else
47
- return 'Nil'
47
+ return "Nil"
48
48
  end
49
49
  end
50
50
  end
51
51
  class Signature
52
52
  def to_cpp declaring = false
53
53
  ret = type.to_cpp
54
- ret << ' '
55
- ret << 'const &' if declaring and type.obj?
54
+ ret << " "
55
+ ret << "const &" if declaring and type.obj?
56
56
  ret << name
57
57
  return ret
58
58
  end
@@ -64,27 +64,27 @@ module TopCoder
64
64
  @vars = vars
65
65
  end
66
66
  def declare
67
- ret = @func.to_cpp
68
- ret << '('
69
- indent = ' ' * ret.size
67
+ ret = " " + @func.to_cpp
68
+ ret << "("
69
+ indent = " " * ret.size
70
70
  temp = @vars.map do |var| var.to_cpp true end
71
71
  ret << temp.join(",\n#{indent}")
72
- ret << ')'
72
+ ret << ")"
73
73
  return ret
74
74
  end
75
75
  def input
76
76
  temp = @vars.map do |var|
77
77
  x = var.to_cpp
78
- x << '; read(ifs, '
78
+ x << "; tc::read(ifs, "
79
79
  x << var.name
80
- x << ');'
80
+ x << ");"
81
81
  end
82
- return temp.join " next(ifs);\n"
82
+ return temp.join " tc::next(ifs);\n"
83
83
  end
84
84
  def output
85
- ret = 'show(ofs, ' << @func.name << '('
85
+ ret = "tc::show(ofs, solver." << @func.name << "("
86
86
  temp = @vars.map do |var| var.name end
87
- ret << temp.join(', ') << '));'
87
+ ret << temp.join(", ") << "));"
88
88
  end
89
89
  end
90
90
  end
@@ -14,8 +14,7 @@ typedef unsigned int uint;
14
14
  typedef long long int64;
15
15
  typedef unsigned long long uint64;
16
16
 
17
- namespace TopCoder
18
- {
17
+ namespace TopCoder {
19
18
  const int _CHUNK_SIZE = 65536;
20
19
 
21
20
  class ParseException: public std::exception {
@@ -28,27 +27,34 @@ namespace TopCoder
28
27
  oss << " (got '" << actual << "')";
29
28
  message = oss.str();
30
29
  }
30
+
31
31
  public:
32
32
  ParseException() : message("ParseException occurred") {}
33
33
  ParseException(std::string const &message) : message(message) {}
34
34
  ~ParseException() throw () {}
35
- virtual const char* what() const throw() { return message.c_str(); }
35
+
36
+ virtual const char* what() const throw() {
37
+ return message.c_str();
38
+ }
36
39
 
37
40
  ParseException(std::string const &required, char actual,
38
41
  std::string const &rest) {
39
42
  buildMessage(required, actual, rest);
40
43
  }
44
+
41
45
  ParseException(char expected, char actual,
42
46
  std::string const &rest) {
43
47
  std::ostringstream oss;
44
48
  oss << "Expect '" << expected << "'";
45
49
  buildMessage(oss.str(), actual, rest);
46
50
  }
51
+
47
52
  ParseException(std::string const &required, std::string const &rest) {
48
53
  std::ostringstream oss;
49
54
  oss << required << " at: <" << rest << ">";
50
55
  message = oss.str();
51
56
  }
57
+
52
58
  ParseException(char expected) {
53
59
  std::ostringstream oss;
54
60
  oss << "Expect '" << expected << " before end of stream";
@@ -65,6 +71,7 @@ namespace TopCoder
65
71
  }
66
72
  return oss.str();
67
73
  }
74
+
68
75
  void _spaces(std::istream &is) {
69
76
  while (is.good()) {
70
77
  int i = is.peek();
@@ -72,43 +79,69 @@ namespace TopCoder
72
79
  else break;
73
80
  }
74
81
  }
82
+
75
83
  void _expect(std::istream &is, char expected) {
76
84
  _spaces(is); char actual = is.peek();
77
85
  if (expected != actual)
78
86
  throw ParseException(expected, actual, _rest(is));
79
87
  is.ignore(1);
80
88
  }
89
+
81
90
  void next(std::istream &is) {
82
91
  _expect(is, ',');
83
92
  }
84
93
 
85
- void read(std::istream &is, int &i) { is >> i; }
86
- void read(std::istream &is, int64 &l) { is >> l; }
87
- void read(std::istream &is, float &f) { is >> f; }
88
- void read(std::istream &is, double &d) { is >> d; }
94
+ void read(std::istream &is, int &i) {
95
+ is >> i;
96
+ }
97
+
98
+ void read(std::istream &is, int64 &l) {
99
+ is >> l;
100
+ }
101
+
102
+ void read(std::istream &is, float &f) {
103
+ is >> f;
104
+ }
105
+
106
+ void read(std::istream &is, double &d) {
107
+ is >> d;
108
+ }
109
+
89
110
  void read(std::istream &is, char &c) {
90
111
  _expect(is, '\'');
91
112
  is.get(c);
92
- if (c == '\'') c = 0;
93
- else _expect(is, '\'');
113
+ if (c == '\'') {
114
+ c = 0;
115
+ } else {
116
+ _expect(is, '\'');
117
+ }
94
118
  }
119
+
95
120
  void read(std::istream &is, std::string &s) {
96
121
  _expect(is, '"');
97
122
  std::ostringstream oss;
98
123
  while (is.good()) {
99
124
  char c; is.get(c);
100
- if (c == '\\') {
125
+ if (c == '"') {
126
+ std::streampos pos = is.tellg();
127
+ _spaces(is);
128
+ if (is.eof()) {
129
+ s = oss.str();
130
+ return;
131
+ }
101
132
  char cc; is.get(cc);
102
- oss << cc;
103
- }
104
- else if (c == '"') {
105
- s = oss.str();
106
- return;
133
+ if (cc == ',' || cc == ']') {
134
+ s = oss.str();
135
+ is.unget();
136
+ return;
137
+ }
138
+ is.seekg(pos);
107
139
  }
108
- else oss << c;
140
+ oss << c;
109
141
  }
110
142
  throw ParseException('"');
111
143
  }
144
+
112
145
  void read(std::istream &is, bool &b) {
113
146
  _spaces(is);
114
147
  std::ostringstream oss;
@@ -130,6 +163,7 @@ namespace TopCoder
130
163
  throw ParseException("Expect a boolean value (true or false)", _rest(is));
131
164
  }
132
165
  }
166
+
133
167
  template <typename T>
134
168
  void read(std::istream &is, std::vector<T> &v) {
135
169
  v.clear();
@@ -143,59 +177,89 @@ namespace TopCoder
143
177
  while (is.good()) {
144
178
  T e; read(is, e); v.push_back(e);
145
179
  _spaces(is); char c; is.get(c);
146
- if (c == ']') return;
147
- if (c != ',')
180
+ if (c == ']') {
181
+ return;
182
+ }
183
+ if (c != ',') {
148
184
  throw ParseException("Expect ',' or ']'", c, _rest(is));
185
+ }
149
186
  }
150
187
  throw ParseException(']');
151
188
  }
152
189
 
153
- void show(std::ostream &os, int i) { os << i; }
154
- void show(std::ostream &os, int64 l) { os << l; }
190
+ void show(std::ostream &os, int i) {
191
+ os << i;
192
+ }
193
+
194
+ void show(std::ostream &os, int64 l) {
195
+ os << l;
196
+ }
197
+
155
198
  void show(std::ostream &os, float f) {
156
199
  os.precision(8);
157
200
  os << f;
158
201
  }
202
+
159
203
  void show(std::ostream &os, double d) {
160
204
  os.precision(16);
161
205
  os << d;
162
206
  }
207
+
163
208
  void show(std::ostream &os, char c) {
164
209
  os << '\'' << c << '\'';
165
210
  }
211
+
166
212
  void show(std::ostream &os, std::string const &s) {
167
213
  os << '"' << s << '"';
168
214
  }
215
+
169
216
  void show(std::ostream &os, bool b) {
170
217
  os << (b ? "true" : "false");
171
218
  }
219
+
172
220
  template <typename T>
173
221
  void show(std::ostream &os, std::vector<T> const &v) {
174
222
  typedef typename std::vector<T>::const_iterator Iterator;
175
223
  bool first = true;
176
224
  os << '[';
177
225
  for (Iterator it = v.begin(); it != v.end(); ++it) {
178
- if (!first) os << ',';
226
+ if (!first) {
227
+ os << ',';
228
+ }
179
229
  show(os, *it);
180
230
  first = false;
181
231
  }
182
232
  os << ']';
183
233
  }
184
234
 
185
- bool same(int i, int j) { return i == j; }
186
- bool same(int64 i, int64 j) { return i == j; }
187
- bool same(char i, char j) { return i == j; }
188
- bool same(std::string const &x, std::string const &y) { return x == y; }
235
+ bool same(int i, int j) {
236
+ return i == j;
237
+ }
238
+
239
+ bool same(int64 i, int64 j) {
240
+ return i == j;
241
+ }
242
+
243
+ bool same(char i, char j) {
244
+ return i == j;
245
+ }
246
+
247
+ bool same(std::string const &x, std::string const &y) {
248
+ return x == y;
249
+ }
250
+
189
251
  bool same(float x, float y) {
190
252
  float p = x * (1.0 - 1e-5);
191
253
  float q = x * (1.0 + 1e-5);
192
254
  return y >= std::min(p, q) && y <= std::max(p, q);
193
255
  }
256
+
194
257
  bool same(double x, double y) {
195
258
  double p = x * (1.0 - 1e-9);
196
259
  double q = x * (1.0 + 1e-9);
197
260
  return y >= std::min(p, q) && y <= std::max(p, q);
198
261
  }
262
+
199
263
  template <typename T>
200
264
  bool same(std::vector<T> const &v1, std::vector<T> const &v2) {
201
265
  typedef typename std::vector<T>::const_iterator Iterator;
@@ -16,67 +16,86 @@ import Text.Parsec
16
16
  import Text.Parsec.String (Parser)
17
17
  import Text.Parsec.Token
18
18
  import Data.Char
19
+ import Data.Maybe
20
+
19
21
 
20
22
  next :: Parser ()
21
- next = do char ','
22
- return ()
23
+ next = do
24
+ char ','
25
+ return ()
23
26
 
24
- parseChar :: Parser Char
25
- parseChar = do char '\''
26
- ret <- anyChar
27
- char '\''
28
- return ret
29
27
 
30
- parseString :: Parser String
31
- parseString = do char '\"'
32
- ret <- many1 (noneOf "\"")
33
- char '\"'
34
- return ret
28
+ parseChar :: Parser Char
29
+ parseChar = do
30
+ char '\''
31
+ ret <- anyChar
32
+ char '\''
33
+ return ret
35
34
 
36
35
  parseBool :: Parser Bool
37
- parseBool = do s <- many1 letter
38
- return ((map toLower s) == "true")
39
-
40
- parseInt :: Parser Int
41
- parseInt = do char '-'
42
- n <- parseInt
43
- return (-n)
44
- <|> do cs <- many1 digit
45
- return (read cs)
46
-
47
- parseLong :: Parser Integer
48
- parseLong = do char '-'
49
- n <- parseLong
50
- return (-n)
51
- <|> do cs <- many1 digit
52
- return (read cs)
53
-
54
- parseFloat :: Parser Float
55
- parseFloat = do char '-'
56
- x <- parseFloat
57
- return (-x)
58
- <|> do cs <- many1 (digit <|> char '.')
59
- return (read cs)
60
-
61
- parseDouble :: Parser Double
62
- parseDouble = do char '-'
63
- x <- parseDouble
64
- return (-x)
65
- <|> do cs <- many1 (digit <|> char '.')
66
- return (read cs)
36
+ parseBool = do
37
+ s <- many1 letter
38
+ case map toLower s of
39
+ "true" -> return True
40
+ "false" -> return False
41
+ otherwise -> fail "expect true or false"
42
+
43
+
44
+ maybeNegative :: Num a => Parser a -> Parser a
45
+ maybeNegative parser = (do
46
+ char '-'
47
+ n <- parser
48
+ return (-n)) <|> parser
49
+
50
+ parseIntegral :: (Integral a, Read a) => Parser a
51
+ parseIntegral = maybeNegative ((many1 digit) >>= (return . read))
52
+
53
+ parseInt = parseIntegral :: Parser Int
54
+ parseLong = parseIntegral :: Parser Integer
55
+
56
+ parseReal :: (RealFloat a, Read a) => Parser a
57
+ parseReal = maybeNegative ((many1 (digit <|> char '.')) >>= (return . read))
58
+
59
+ parseFloat = parseReal :: Parser Float
60
+ parseDouble = parseReal :: Parser Double
61
+
62
+
63
+
64
+ expect :: Char -> Parser ()
65
+ expect c = do
66
+ cc <- optionMaybe (char c)
67
+ if (isJust cc)
68
+ then return ()
69
+ else fail "disappointing"
70
+
71
+ parseClosingQuote :: Parser Char
72
+ parseClosingQuote = do
73
+ char '"' >> spaces
74
+ choice [eof, expect ',', expect ']']
75
+ return '"'
76
+
77
+ parseString :: Parser String
78
+ parseString = do
79
+ char '"'
80
+ ret <- manyTill anyChar (try (lookAhead parseClosingQuote))
81
+ char '"'
82
+ return ret
67
83
 
68
84
 
69
85
  parseElems :: Read a => Parser a -> Parser [a]
70
- parseElems f = do x <- f
71
- spaces
72
- do next
73
- xs <- parseElems f
74
- return (x:xs)
75
- <|> return [x]
76
- <|> return []
86
+ parseElems parser = (do
87
+ spaces
88
+ x <- parser
89
+ spaces
90
+ (do
91
+ next
92
+ xs <- parseElems parser
93
+ return (x:xs)) <|> return [x]) <|> return []
94
+
77
95
 
78
96
  parseList :: Read a => Parser a -> Parser [a]
79
- parseList f = do spaces >> char '['
80
- xs <- parseElems (spaces >> f)
81
- spaces >> char ']'
82
- return xs
97
+ parseList parser = do
98
+ spaces >> char '[' >> spaces
99
+ xs <- parseElems parser
100
+ spaces >> char ']'
101
+ return xs