input and contests

Interactive input

Try interactive keyboard input:

python py2scratch.py examples/input_demo.py input_demo.sb3

Load the project, click the green flag, and answer the prompts. Keep the hidden Python input decoder sprite when editing projects that use interactive input.

Competitive programming

Compile the included Dijkstra example:

python py2scratch.py --stdin-file examples/contest_demo.in examples/contest_demo.py contest_demo.sb3

Load contest_demo.sb3 in Scratch and click the green flag. It runs Dijkstra’s algorithm on the input in examples/contest_demo.in and prints 0 3 5 6 11 -1. The first input line contains the number of vertices, the number of directed edges, and the source vertex. Each remaining line contains an edge’s endpoints and weight.

To test your own solution, write the sample input to a UTF-8 text file and embed it at compile time.

python py2scratch.py --stdin-file sample.in solve.py solve.sb3

You can use input(), sys.stdin.readline(), or sys.stdin.read().split() to read text. For a buffered contest template, use map(int, sys.stdin.buffer.read().split()). The green flag rewinds embedded input. Choose text or buffered reads for each run. With no embedded file, input() and sys.stdin.readline() prompt for one line.

Use Scratch to step through small cases. Run large cases and measure contest time limits with Python. The same .py file also runs directly with CPython.

Module Available APIs
sys Text and buffered stdin, text stdout, recursion limit, maxsize, and exit
bisect bisect_left, bisect_right, bisect, and all three insort variants with key
heapq Min-heap operations, merge, nlargest, nsmallest, and lexicographic tuple priorities
collections deque with bounded capacity and operations at both ends, Counter with count arithmetic, defaultdict, OrderedDict, namedtuple with tuple indexing, unpacking, ordering, and tuple keys
math gcd, lcm, isqrt, comb, perm, factorial, prod, inf, nan, finite checks, logarithms, trigonometry, isclose, hypot, and dist
itertools accumulate, batched, tee, chain, combinations, combinations_with_replacement, compress, count, cycle, dropwhile, filterfalse, groupby, islice, pairwise, permutations, product, repeat, starmap, takewhile, zip_longest
typing Function, class and variable annotations, generic aliases, union types, TypeVar, Optional, Literal, cast, metadata access
dataclasses Mutable records with generated constructors, default factories, comparison, ordering, keyword-only fields, fields, asdict, astuple, replace, make_dataclass
cmath Complex literals and arithmetic, integer powers, conjugation, polar coordinates, logarithms, square roots, trigonometric and hyperbolic functions, finite checks
time Scratch-backed wall clock, monotonic timers, sleep, UTC conversion, and selected date formatting
timeit Callable statements and setup functions, repeat, and automatic iteration counts
functools cache, lru_cache, reduce, partial, cmp_to_key
operator Common arithmetic, comparison, and indexing functions, itemgetter, attrgetter, methodcaller
statistics mean, median, median_low, median_high, mode, multimode
string ASCII, digit, punctuation, whitespace, and printable constants

The compiler bundles these implementations into the project. Container adapters cover the listed operations with Scratch runtime types. Text and bytes cover the common operations needed to parse contest input, including whitespace splitting, slicing, and numeric conversion.