Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add knapsack solver script for binary executable; #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions knapsack/solverShell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
from subprocess import Popen, PIPE

def solve_it(input_data):

# Writes the inputData to a temporay file

tmp_file_name = 'tmp.data'
tmp_file = open(tmp_file_name, 'w')
tmp_file.write(input_data)
tmp_file.close()

# NB! dont forget to compile binary, e.g rustc knapsack.rs
# Runs the command: cat tmp_file_name | ./knapsack
# and pipes the content of a tmp_file directly into executable
process = Popen(['cat ' + tmp_file_name + ' | ./knapsack'],
stdout=PIPE, shell = True, universal_newlines = True
)
(stdout, stderr) = process.communicate()

# removes the temporay file
os.remove(tmp_file_name)

return stdout.strip()


import sys

if __name__ == '__main__':
if len(sys.argv) > 1:
file_location = sys.argv[1].strip()
with open(file_location, 'r') as input_data_file:
input_data = input_data_file.read()
print(solve_it(input_data))
else:
print('This test requires an input file. Please select one from the data directory. (i.e. python solver.py ./data/ks_4_0)')