然后用函数修饰符把Codejam模版又写了一遍。哈哈哈哈
更新:用带参数的函数修饰符重写了。
'''
Created on 2012-12-11
@author: festony
'''
import sys
import time
__all__ = [ \
'rec_proc_time', \
'multi_cases', \
'redir_stdin', \
'output_into_file', \
'read_n_strs'\
]
default_input_file_path = '..\\inout\\testin.txt'
default_output_file_path = '..\\inout\\testout.txt'
# decorators.
def rec_proc_time(func_name=None):
def real_rec_proc_time(func):
def modified_func(*arg, **args):
start_time = time.clock()
r = func(*arg, **args)
case_num = ''
if arg:
case_num = ' ' + str(arg[0])
print "Function {%s%s} process time: %g sec(s)" % \
(func_name, case_num, time.clock() - start_time)
return r
return modified_func
return real_rec_proc_time
def multi_cases(func):
def modified_func(*arg, **args):
case_total_num = int(sys.stdin.readline().rstrip('\r\n'))
r = ''
for i in range(case_total_num):
r += func(i+1) + '\n'
return r
return modified_func
def redir_stdin(input_file_path=default_input_file_path):
def real_redir_stdin(func):
def modified_func(*arg, **args):
oldstdin = sys.stdin
inputfile = open(input_file_path, 'r')
sys.stdin = inputfile
r = func(*arg, **args)
sys.stdin = oldstdin
return r
return modified_func
return real_redir_stdin
def output_into_file(output_file_path=default_output_file_path):
def real_output_into_file(func):
def modified_func(*arg, **args):
outputfile = open(output_file_path, 'w')
r = func(*arg, **args)
outputfile.write(r)
print r
return r
return modified_func
return real_output_into_file
# commonly used functions
def read_n_strs():
"""
Read strings from stdin.
The number of strings read in is the first input.
e.g. input:
3
aaaa
bbbbb b
ccc
dd dddd
result in read first 4 strings then returns them.
"""
n = int(sys.stdin.readline().rstrip('\r\n'))
r = list()
for i in range(n):
r.append(sys.stdin.readline().rstrip('\r\n'))
return r
# Test
if __name__ == '__main__':
@rec_proc_time('Whole') # Record process time of the whole case runs
@redir_stdin() # Redirect std input
@output_into_file() # Copy the output into the output file.
@multi_cases # Run it N cases.
@rec_proc_time('Case') # Record process time of each single case runs
def process_func(case_num):
print 'Round', case_num
return 'Case #%d: %s' % (case_num, '0')
# Run it!
process_func()