diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-07-03 14:26:33 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2013-07-03 14:26:33 +0200 |
commit | 1a1515b5eb60e4dc049256d0498f4fb0f0c29b23 (patch) | |
tree | 80a041c7c7cdc32c144343c40e2332fd0042efce /01-knapsack/solverJava.py | |
parent | 7f64bf1e3102bc074dc96841f0d09b4e1b428699 (diff) | |
download | coursera-1a1515b5eb60e4dc049256d0498f4fb0f0c29b23.zip coursera-1a1515b5eb60e4dc049256d0498f4fb0f0c29b23.tar.gz |
Discrete : add 01-knapsack assignment
Diffstat (limited to '01-knapsack/solverJava.py')
-rwxr-xr-x | 01-knapsack/solverJava.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/01-knapsack/solverJava.py b/01-knapsack/solverJava.py new file mode 100755 index 0000000..1023219 --- /dev/null +++ b/01-knapsack/solverJava.py @@ -0,0 +1,41 @@ +#!/usr/bin/python2 +# -*- coding: utf-8 -*- + +import os +from subprocess import Popen, PIPE + + +def solveIt(inputData): + + # Writes the inputData to a temporay file + + tmpFileName = 'tmp.data' + tmpFile = open(tmpFileName, 'w') + tmpFile.write(inputData) + tmpFile.close() + + # Runs the command: java Solver -file=tmp.data + + process = Popen(['java', 'Solver', '-file=' + tmpFileName], + stdout=PIPE) + (stdout, stderr) = process.communicate() + + # removes the temporay file + + os.remove(tmpFileName) + + return stdout.strip() + + +import sys + +if __name__ == '__main__': + if len(sys.argv) > 1: + fileLocation = sys.argv[1].strip() + inputDataFile = open(fileLocation, 'r') + inputData = ''.join(inputDataFile.readlines()) + inputDataFile.close() + print solveIt(inputData) + else: + print 'This test requires an input file. Please select one from the data directory. (i.e. python solver.py ./data/ks_4_0)' + |