This is my library collection used in coding competitions.
All codes except for mathapi is licensed under StealItPl 1.1.
cjlib.baseN(num, base[, numerals])
: Convertnum
to basebase
withnumerals
as character representatives of values (default to 0-9a-z) sourcecjlib.mathapi
: @neizod's mathapi. Source and docs
Input library. This library only works with only one input at a time. (i.e. input.get
should be called only once)
input.get([default[, skip=True]])
: Read input from first command line argument or usedefault
. Usually the first line is the lines count and will be automatically chopped. Disable this by settingskip=False
.input.line()
: Return next line from the input. (No line ending is included)input.lines([cnt=2])
: Return nextcnt
lines from the input as an array.input.block()
: Return next block in the same format asinput.lines
without the line number count. A block is defined as a line with a number showing how many lines are in this block. Example of a block:
4
2 2 2
2 1 2
2 1 2
2 2 2
Returns ["2 2 2", "2 1 2", "2 1 2", "2 2 2"]
.
input.neof()
: ReturnTrue
if there is still more input. Usually used in awhile
loop.
Task multiprocessing.
Usage:
from cjlib.input import neof, line, get
from cjlib.runner import TaskRunner, DummyRunner
def is_odd(inp):
return inp%2 == 1
r = TaskRunner(is_odd, DummyRunner)
get("""4
1
3
4
5
""")
while neof():
r.add(int(line()))
r.run()
- The second argument to
TaskRunner
is optional and use to specify multiprocessing backend. Defaults toThreadRunner
. Available modules are:DummyRunner
: Does not use multiprocessing. Easiest to debug.MPQRunner
: Usemultiprocessing.Pool
. Fastest but requires more memory.ThreadRunner
: Usethreading.Thread
. (Actually anMPQRunner
withmultiprocessing.dummy.Pool
as thread pool)
MPQRunner
use 4 as hard-coded subprocess count. SupplyprocessCnt=8
toTaskRunner
when usingMPQRunner
to use 8 processes and so on.- The output will be returned in stdout after all backend has finished processing. Status messages are sent to Python's
logging
module with info level and usually be sent to stderr. - To add case number to output, use
r.run(True)
. The output will be in Codejam format:Case #n: out
- Accessing
TaskRunner
in the processing function (in this case,is_odd
) is not safe.