OpenViSTAI · Agents · Blockchain · Crypto
Menu
OPENVIST RECIPES

Find compressed gaps in increasing event offsets with Python

Python

Report missing ranges between supplied integer event offsets without expanding every missing number.

Python code excerpt: Find compressed gaps in increasing event offsets with Python. Version 0.1.0. Read the full recipe for code, sources and recorded test scope.

Report missing ranges between supplied integer event offsets without expanding every missing number. This is a local data check under the documented conventions; it does not execute tools or contact a service.

Before you start

  • Save the code as recipe.py and run python3.12 recipe.py with a JSON array supplied through stdin. No CLI arguments, including a help option, are accepted.
  • Uses only json, sys and math. It does not open input paths, write files, execute external commands or contact a network.
  • Input must be strict UTF-8 JSON without a leading UTF-8 BOM. Malformed JSON, NaN, Infinity, -Infinity and floating-point overflow are rejected during parsing.
  • The array must contain at most 1000 integers from 0 through 1000000000. Booleans and floating-point numbers are rejected, including floats that represent whole numbers. Values must be strictly increasing.
  • Each gap has inclusive start and end boundaries. Gaps are emitted in ascending order, and missing_count counts only missing offsets between supplied values. Nothing is inferred before the first or after the last offset. Empty and singleton arrays produce no gaps.
  • Successful processing exits 0, writes exactly one JSON object followed by a newline to stdout, and writes nothing to stderr. Invalid input, arguments or stdin read failures exit 2 with no stdout and the generic lowercase diagnostic error: invalid input on stderr.

Build and check

  1. Use Python 3.12. Save the code as recipe.py.
  2. Save the supplied JSON example as input.json, encoded as UTF-8 without a BOM.
  3. Run: python3 recipe.py < input.json
  4. Compare the output with the expected result; handle exit code 2 as invalid input.

Limits to understand

  • Reads at most 65537 bytes and rejects input exceeding 65536 bytes. Stdin can block waiting for EOF; no timeout is implemented.
  • The raw input, decoded text and parsed input are held in memory. Valid-input processing takes linear time in the number of supplied offsets and stores at most 999 compressed gaps, without expanding individual missing offsets.
  • Python's JSON parser nesting limits and integer-conversion limits may also reject input.
  • Successful output can reveal sensitive offset ranges and counts. Diagnostics do not disclose input values.
  • Output-write failures, including failures writing or flushing stdout, are outside the exit-code and diagnostic guarantees.

Run it

python3 recipe.py < input.json

Example input

[1, 4, 5]

Expected output

{"gaps": [{"start": 2, "end": 3}], "missing_count": 2}
INSPECT THE SCRIPT

Inspect, copy and run the documented checks.

This browser preview builds code locally. It does not contact an API or run the code.

Default example ready.

Python example
import json
import sys
import math


def reject_constant(_value):
    raise ValueError()


def finite_float(text):
    value = float(text)
    if not math.isfinite(value):
        raise ValueError()
    return value


def main():
    try:
        if len(sys.argv) != 1:
            raise ValueError()

        raw = sys.stdin.buffer.read(65537)
        if len(raw) > 65536 or raw.startswith(b'\xef\xbb\xbf'):
            raise ValueError()

        text = raw.decode('utf-8', errors='strict')
        values = json.loads(
            text,
            parse_constant=reject_constant,
            parse_float=finite_float,
        )

        if not isinstance(values, list) or len(values) > 1000:
            raise ValueError()

        previous = None
        for value in values:
            if isinstance(value, bool) or not isinstance(value, int):
                raise ValueError()
            if value < 0 or value > 1000000000:
                raise ValueError()
            if previous is not None and value <= previous:
                raise ValueError()
            previous = value

        gaps = []
        missing_count = 0
        previous = None
        for value in values:
            if previous is not None and value > previous + 1:
                start = previous + 1
                end = value - 1
                gaps.append({'start': start, 'end': end})
                missing_count += end - start + 1
            previous = value

        output = json.dumps(
            {'gaps': gaps, 'missing_count': missing_count},
            separators=(',', ':'),
            allow_nan=False,
        ) + '\n'
    except Exception:
        sys.stderr.write('error: invalid input\n')
        return 2

    sys.stdout.write(output)
    sys.stdout.flush()
    return 0


if __name__ == '__main__':
    sys.exit(main())
What to verify after running
  • The supplied example must match the documented JSON output.
  • Error cases must exit 2 with no stdout. Review sensitive successful output before sharing it.

Request or format preview

Illustrative preview. No provider response, price or balance is fetched.

Change a setting to inspect the preview.

Official sources and related profiles

    Code license: MIT. Keep the license notice with your copy.

    Example version 0.1.0 · Documentation reviewed 2026-10-14.

    Six independent CLI fixtures and 10 curated supplementary checks passed in the pinned offline Python 3.12 container. This does not establish exhaustive correctness, production performance or compatibility with an external provider.