gettc 1.6.2 → 1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+ package topcoder
2
+
3
+ import (
4
+ "bufio"
5
+ "fmt"
6
+ "io"
7
+ "reflect"
8
+ )
9
+
10
+ func writeOne(bw *bufio.Writer, x interface{}) error {
11
+ switch y := x.(type) {
12
+ case bool:
13
+ fmt.Fprintf(bw, "%t", y)
14
+ case byte:
15
+ fmt.Fprintf(bw, "'%c'", y)
16
+ case string:
17
+ fmt.Fprintf(bw, "\"%s\"", y)
18
+ case int, int64:
19
+ fmt.Fprintf(bw, "%d", y)
20
+ case float32, float64:
21
+ fmt.Fprintf(bw, "%f", y)
22
+ default:
23
+ slice := reflect.ValueOf(x)
24
+ if slice.Kind() != reflect.Slice {
25
+ return NewErrType(slice.Type())
26
+ }
27
+ bw.WriteByte('[')
28
+ for i := 0; i < slice.Len(); i++ {
29
+ if i != 0 {
30
+ bw.WriteString(", ")
31
+ }
32
+ elem := slice.Index(i)
33
+ if err := writeOne(bw, elem.Interface()); err != nil {
34
+ return err
35
+ }
36
+ }
37
+ bw.WriteByte(']')
38
+ }
39
+ return nil
40
+ }
41
+
42
+ func Write(w io.Writer, xs ...interface{}) error {
43
+ bw := bufio.NewWriter(w)
44
+ for i, x := range xs {
45
+ if i != 0 {
46
+ bw.WriteString(", ")
47
+ }
48
+ if err := writeOne(bw, x); err != nil {
49
+ return err
50
+ }
51
+ }
52
+ bw.Flush()
53
+ return nil
54
+ }
@@ -0,0 +1,19 @@
1
+ package topcoder
2
+
3
+ import (
4
+ "bytes"
5
+ "testing"
6
+ )
7
+
8
+ func writer() *bytes.Buffer {
9
+ return bytes.NewBufferString("")
10
+ }
11
+
12
+ func TestShow(t *testing.T) {
13
+ out := writer()
14
+ Write(out, "Joe", 20, 7.5, false, []string{"Math", "CompSci"}, [][]int{{7, 8}, {}})
15
+ ret := out.String()
16
+ if ret != "\"Joe\", 20, 7.500000, false, [\"Math\", \"CompSci\"], [[7, 8], []]" {
17
+ t.Errorf("Write() produces <" + ret + ">")
18
+ }
19
+ }
@@ -1,11 +1,5 @@
1
1
  require "gettc/types"
2
2
 
3
- class String
4
- def uncapitalize
5
- return self[0, 1].downcase + self[1..-1]
6
- end
7
- end
8
-
9
3
  module Gettc
10
4
  class Type
11
5
  def to_haskell
@@ -73,14 +67,21 @@ module Gettc
73
67
  end
74
68
  end
75
69
  class HaskellEngine
76
- attr_accessor :func, :vars
70
+ attr_reader :func, :vars, :declare, :input, :output
77
71
  def initialize func, vars
78
- @func = Signature.new func.type, func.name.uncapitalize
72
+ @func = Signature.new func.type, uncapitalize(func.name)
79
73
  @vars = vars.map do |var|
80
- Signature.new var.type, var.name.uncapitalize
74
+ Signature.new var.type, uncapitalize(var.name)
81
75
  end
76
+ compute_declare
77
+ compute_input
78
+ compute_output
79
+ end
80
+ private
81
+ def uncapitalize str
82
+ return str[0, 1].downcase + str[1..-1]
82
83
  end
83
- def declare
84
+ def compute_declare
84
85
  ret = ""
85
86
  ret << func.name << " :: "
86
87
 
@@ -91,9 +92,9 @@ module Gettc
91
92
  temp = vars.map do |var| var.name end
92
93
  ret << temp.join(" ") << " = " << func.type.dumb_haskell
93
94
 
94
- return ret
95
+ @declare = ret
95
96
  end
96
- def input
97
+ def compute_input
97
98
  ret = "getVars :: TC.Parser ("
98
99
  temp = @vars.map do |var| var.type.to_haskell end
99
100
  ret << temp.join(", ") << ")\n"
@@ -111,14 +112,14 @@ module Gettc
111
112
  temp = @vars.map do |var| var.name end
112
113
  ret << temp.join(", ")
113
114
  ret << ")"
114
- return ret
115
+ @input = ret
115
116
  end
116
- def output
117
+ def compute_output
117
118
  ret = ""
118
119
  ret << @func.name << " "
119
120
  temp = vars.map do |var| var.name end
120
121
  ret << temp.join(" ")
121
- return ret
122
+ @output = ret
122
123
  end
123
124
  end
124
125
  end
@@ -80,7 +80,7 @@ module Gettc
80
80
  return type unless type.is_a? TArray
81
81
  return get_elem_type type.subtype
82
82
  end
83
- attr_accessor :name, :boxed_name, :type, :boxed_type
83
+ attr_reader :name, :boxed_name, :type, :boxed_type
84
84
  def initialize sig
85
85
  @sig = sig
86
86
  @name = sig.name
@@ -152,21 +152,25 @@ module Gettc
152
152
  end
153
153
  end
154
154
  class JavaEngine
155
- attr_accessor :func, :vars
155
+ attr_accessor :declare, :input, :output
156
156
  def initialize func, vars
157
157
  @func = func
158
- @vars = vars
158
+ @vars = vars
159
+ compute_declare
160
+ compute_input
161
+ compute_output
159
162
  end
160
- def declare
163
+ private
164
+ def compute_declare
161
165
  ret = "public "
162
166
  ret << @func.to_java << "("
163
167
  indent = " " * ret.size
164
168
  temp = @vars.map do |var| var.to_java end
165
169
  ret << temp.join(",\n#{indent}")
166
170
  ret << ")"
167
- return ret
171
+ @declare = ret
168
172
  end
169
- def input
173
+ def compute_input
170
174
  temp = @vars.map do |var|
171
175
  ret = ""
172
176
  if var.type.is_a? TArray
@@ -186,9 +190,9 @@ module Gettc
186
190
  end
187
191
  ret
188
192
  end
189
- return temp.join "\nreader.next();\n\n"
193
+ @input = temp.join "\nreader.next();\n\n"
190
194
  end
191
- def output
195
+ def compute_output
192
196
  ret = ""
193
197
  caller = "solver." + @func.name + "("
194
198
  temp = @vars.map do |var| var.name end
@@ -201,7 +205,7 @@ module Gettc
201
205
  else
202
206
  ret << "writer.write(" << caller << ");"
203
207
  end
204
- return ret
208
+ @output = ret
205
209
  end
206
210
  end
207
- end
211
+ end
@@ -1,4 +1,5 @@
1
- require "gettc/types"
1
+ require "gettc/types"
2
+ require "mkmf"
2
3
 
3
4
  module Gettc
4
5
  class Type
@@ -25,22 +26,44 @@ module Gettc
25
26
  end
26
27
  end
27
28
  class PythonEngine
28
- attr_accessor :func, :vars
29
+ attr_reader :arglist, :input, :shebang
29
30
  def initialize func, vars
30
- @func = func
31
- @vars = vars
32
- end
33
- def vars_list
34
- temp = @vars.map do |var|
31
+ temp = vars.map do |var|
35
32
  var.name
36
33
  end
37
- return temp.join ", "
38
- end
39
- def input
40
- temp = @vars.map do |var|
34
+ @arglist = temp.join ", "
35
+
36
+ temp = vars.map do |var|
41
37
  var.name + ' = reader.next("' + var.type.to_s + '")'
42
38
  end
43
- return temp.join "\nreader.next()\n"
39
+ @input = temp.join "\nreader.next()\n"
40
+
41
+ @shebang = "#! "
42
+ @shebang << python3_path
43
+ end
44
+ private
45
+ def python3_path
46
+ default = "/usr/bin/env python"
47
+
48
+ version = system_q("python --version").split
49
+ if version.size != 2 || version[0].upcase != "PYTHON" then
50
+ return default
51
+ end
52
+
53
+ version = version[1].split(".")[0].to_i
54
+ if version >= 3 then
55
+ return default
56
+ end
57
+
58
+ python3 = system_q "which python3"
59
+ return python3.empty? ? default : python3
60
+ rescue
61
+ return default
62
+ end
63
+ def system_q command
64
+ IO.popen command, err: [:child, :out] do |f|
65
+ return f.gets
66
+ end
44
67
  end
45
68
  end
46
- end
69
+ end
@@ -17,10 +17,6 @@ setup :
17
17
  main : <%= prob.name %>Runner.cpp <%= prob.name %>.cpp setup
18
18
  $(COMPILER) -o $(OUTPUT_D)/<%= prob.name %>Runner $<
19
19
 
20
- test : <%= prob.name %>Test.cpp <%= prob.name %>.cpp setup
21
- $(COMPILER) -lboost_unit_test_framework -o $(OUTPUT_D)/<%= prob.name %>Test $<
22
- $(OUTPUT_D)/<%= prob.name %>Test
23
-
24
20
  demo : main
25
21
  $(call run,verbose,demo)
26
22
 
@@ -31,4 +27,4 @@ sysv : main
31
27
  $(call run,verbose,sys)
32
28
 
33
29
  clean :
34
- rm -rf $(OUTPUT_D)
30
+ rm -rf $(OUTPUT_D)
@@ -5,7 +5,7 @@ using namespace std;
5
5
  class <%= prob.name %> {
6
6
  public:
7
7
  <%= engine = CppEngine.new func, vars
8
- engine.declare %> {
8
+ engine.declare.gsub /^/, " " %> {
9
9
  return <%= func.type.dumb_cpp %>;
10
10
  }
11
- };
11
+ };
@@ -0,0 +1,29 @@
1
+ GETTC_HOME ?= $(HOME)/.gettc
2
+ OUTPUT_D =../../build/go
3
+ DATA_D = ../../data
4
+ RUNNER = sh ../../bin/runner.sh
5
+
6
+ run = $(RUNNER) $1 $(OUTPUT_D)/<%= prob.name %>Runner $(DATA_D)/$2 $(OUTPUT_D)/$2
7
+
8
+ default : usage
9
+
10
+ usage :
11
+ @cat $(GETTC_HOME)/usage
12
+
13
+ setup :
14
+ if [ ! -d $(OUTPUT_D) ]; then mkdir -p $(OUTPUT_D); fi
15
+
16
+ main : <%= prob.name %>Runner.go <%= prob.name %>/<%= prob.name %>.go setup
17
+ export GOPATH="$$GOPATH<%= File::PATH_SEPARATOR %>$(GETTC_HOME)/include/go" && go build -o $(OUTPUT_D)/<%= prob.name %>Runner <%= prob.name %>Runner.go
18
+
19
+ demo : main
20
+ $(call run,verbose,demo)
21
+
22
+ sys : main
23
+ $(call run,quiet,sys)
24
+
25
+ sysv : main
26
+ $(call run,verbose,sys)
27
+
28
+ clean :
29
+ rm -rf $(OUTPUT_D)
@@ -0,0 +1,7 @@
1
+ package <%= prob.name %>
2
+ <%
3
+ engine = GoEngine.new func, vars
4
+ %>
5
+ func <%= engine.func_name %>(<%= engine.declare.join ", " %>) <%= func.type.to_go %> {
6
+ return <%= func.type.dumb_go %>
7
+ }
@@ -0,0 +1,34 @@
1
+ package main
2
+
3
+ import (
4
+ "./<%= prob.name %>"
5
+ "fmt"
6
+ "os"
7
+ "topcoder"
8
+ )
9
+ <%
10
+ engine = GoEngine.new func, vars
11
+ %>
12
+ func main() {
13
+ if len(os.Args) < 3 {
14
+ fmt.Fprintf(os.Stderr, "Usage: %s <input_file> <output_file>", os.Args[0])
15
+ os.Exit(-1)
16
+ }
17
+
18
+ var (
19
+ fileHandle *os.File
20
+ errIO error
21
+ <%= engine.declare.join "\n#{' ' * 8}" %>
22
+ )
23
+
24
+ if fileHandle, errIO = os.Open(os.Args[1]); errIO != nil {
25
+ panic(errIO)
26
+ }
27
+ topcoder.Read(fileHandle, <%= engine.input %>)
28
+ fileHandle.Close()
29
+
30
+ if fileHandle, errIO = os.Create(os.Args[2]); errIO != nil {
31
+ panic(errIO)
32
+ }
33
+ topcoder.Write(fileHandle, <%= prob.name %>.<%= engine.func_name %>(<%= engine.output %>))
34
+ }
@@ -17,10 +17,6 @@ setup :
17
17
  main : <%= prob.name %>Runner.hs <%= prob.name %>.hs setup
18
18
  $(COMPILER) -o $(OUTPUT_D)/<%= prob.name %>Runner $<
19
19
 
20
- test : <%= prob.name %>Test.hs <%= prob.name %>.hs setup
21
- $(COMPILER) -o $(OUTPUT_D)/<%= prob.name %>Test $<
22
- $(OUTPUT_D)/<%= prob.name %>Test
23
-
24
20
  demo : main
25
21
  $(call run,verbose,demo)
26
22
 
@@ -31,4 +27,4 @@ sysv : main
31
27
  $(call run,verbose,sys)
32
28
 
33
29
  clean :
34
- rm -rf $(OUTPUT_D)
30
+ rm -rf $(OUTPUT_D)
@@ -59,22 +59,6 @@ java -cp "`dirname $0`${path.separator}${lib}" <%= prob.name %>Runner $$*
59
59
  <arg value="${output.dir}/sys"/>
60
60
  </exec>
61
61
  </target>
62
-
63
- <target name="compile-test">
64
- <mkdir dir="${output.dir}"/>
65
- <javac srcdir="." destdir="${output.dir}"
66
- includes="<%= prob.name %>.java,<%= prob.name %>Test.java"
67
- includeantruntime="yes">
68
- <classpath location="${output.dir}"/>
69
- </javac>
70
- </target>
71
- <target name="test" depends="compile-test">
72
- <junit fork="yes" haltonfailure="yes">
73
- <classpath location="${output.dir}"/>
74
- <formatter type="plain" usefile="false" />
75
- <test name="<%= prob.name %>Test"/>
76
- </junit>
77
- </target>
78
62
 
79
63
  <target name="clean">
80
64
  <delete dir="${output.dir}"/>
@@ -24,4 +24,4 @@ sysv : setup
24
24
  $(call run,verbose,sys)
25
25
 
26
26
  clean :
27
- rm -rf $(OUTPUT_D)
27
+ rm -rf $(OUTPUT_D)
@@ -1,4 +1,4 @@
1
1
  <%
2
2
  engine = PythonEngine.new func, vars
3
- %>def <%= func.name %>(<%= engine.vars_list %>):
4
- return <%= func.type.dumb_python %>
3
+ %>def <%= func.name %>(<%= engine.arglist %>):
4
+ return <%= func.type.dumb_python %>
@@ -1,5 +1,5 @@
1
- #! /usr/bin/env python
2
-
1
+ <%= engine = PythonEngine.new func, vars
2
+ engine.shebang %>
3
3
  import os, sys
4
4
  import <%= prob.name %>
5
5
 
@@ -15,10 +15,9 @@ def main():
15
15
  input = fi.read()
16
16
  reader = tc.Reader(input)
17
17
 
18
- <%= engine = PythonEngine.new func, vars
19
- engine.input.gsub(/^/, ' ' * 8) %>
18
+ <%= engine.input.gsub(/^/, ' ' * 8) %>
20
19
 
21
- result = <%= prob.name %>.<%= func.name %>(<%= engine.vars_list %>)
20
+ result = <%= prob.name %>.<%= func.name %>(<%= engine.arglist %>)
22
21
  with open(sys.argv[2], "w") as fo:
23
22
  fo.write(tc.write(result, "<%= func.type %>"))
24
23