Skip to content

Instantly share code, notes, and snippets.

@jorgebg
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save jorgebg/11c421414b8843ce2d7a to your computer and use it in GitHub Desktop.

Select an option

Save jorgebg/11c421414b8843ce2d7a to your computer and use it in GitHub Desktop.
Tuenti Challenge 5 Template
wget https://gist.githubusercontent.com/jorgebg/11c421414b8843ce2d7a/raw/program && chmod +x ./program
usage: program [-h] [--concurrent] [--verbose]

Read cases from STDIN and print results to STDOUT

examples:
  ./program -v < sampleInput
  ./program < testInput.txt > testOutput.txt
  ./program -cv < testInput.txt > testOutput.txt

optional arguments:
  -h, --help        show this help message and exit
  --concurrent, -c
  --verbose, -v
#!/usr/bin/python3
# Python >= 3.4
"""
usage: program [-h] [--concurrent] [--verbose]
Read cases from STDIN and print results to STDOUT
examples:
./program -v < sampleInput
./program < testInput.txt > testOutput.txt
./program -cv < testInput.txt > testOutput.txt
optional arguments:
-h, --help show this help message and exit
--concurrent, -c
--verbose, -v
"""
### Helpers
pass
### Case Solver
def solve(line):
logger = logging.getLogger(line)
args = map(int, line.split(' '))
logger.info('Start %s' % line)
raise NotImplementedError()
logger.info('End %s' % line)
return result
### Main
import sys, time, logging, argparse, concurrent.futures
class Program:
"""
Read cases from STDIN and print results to STDOUT
examples:
./program -v < sampleInput
./program < testInput.txt > testOutput.txt
./program -cv < testInput.txt > testOutput.txt
"""
def __init__(self, solver):
self.solver = solver
def main(self):
args = self.parseargs()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
ncases = int(sys.stdin.readline())
cases = [line.strip() for line in sys.stdin.readlines()]
if len(cases) is not ncases:
raise ValueError()
if args.concurrent:
with concurrent.futures.ProcessPoolExecutor() as executor:
results = executor.map(self.handlecase, cases)
else:
results = map(self.handlecase, cases)
for result in results:
self.showresult(*result)
def handlecase(self, case):
start = time.time()
value = self.solver(case)
seconds = time.time() - start
return seconds, case, value
def showresult(self, seconds, case, value):
print(value)
logging.info("{}s | {} = {}".format(seconds, case, value))
def parseargs(self):
parser = argparse.ArgumentParser(
'program',
description = self.__doc__,
formatter_class = argparse.RawTextHelpFormatter
)
for flag in ['concurrent','verbose']:
parser.add_argument('--%s' % flag, '-%s' % flag[0], action='store_true')
return parser.parse_args()
if __name__ == '__main__':
Program(solve).main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment