-
-
Save almog/490f1b1acba36ff9cbbe6ce8e91b80a2 to your computer and use it in GitHub Desktop.
Google Code Jam Python template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import os | |
import bisect | |
import collections | |
import collections.abc | |
import itertools | |
import math | |
import operator | |
import re | |
import sys | |
import fileinput | |
import builtins | |
from pprint import pprint | |
from collections import defaultdict | |
from collections import Counter | |
from functools import reduce | |
# Python 3.7 shim | |
math.prod = lambda iterable: reduce(operator.mul, iterable) | |
# ------------------------ | |
# Be creative here | |
# ------------------------ | |
def parse_case(f): | |
return pass | |
def solve(target): | |
return pass | |
# ------------------------ | |
# No touchy | |
# ------------------------ | |
class InputReader(collections.abc.Iterable): | |
_cases = None | |
INPUT_FILE_NAME = f'{sys.argv[0]}_input' # Use extenshion-less file names | |
def __init__(self, parse_case, inp=None): | |
self._read(parse_case, inp or self._default_input()) | |
def _default_input(self): | |
default_file_names = [self.INPUT_FILE_NAME] if os.path.isfile(self.INPUT_FILE_NAME) else [] | |
files_list = [*sys.argv[1:], *default_file_names, '-'] | |
return fileinput.input(files_list) | |
def _read(self, parse_case, inp): | |
t = int(next(inp)) | |
self._cases = [parse_case(inp) for _ in range(t)] | |
assert len(self._cases) == t | |
def __iter__(self): | |
return iter(self._cases) | |
def main(): | |
inp = InputReader(parse_case) | |
for i, c in enumerate(inp, start=1): | |
builtins.print('Case #{}: {}'.format(i, solve(c))) | |
# ------------------------ | |
# Utilities | |
# ------------------------ | |
def print(*args): raise RuntimeError("To avoid accidental debug printing on submisisons, use log()") | |
def log(*args): | |
if "DEBUG" in os.environ: | |
builtins.print(*args) | |
def bsearch(a, x): | |
''' | |
Locate the leftmost value exactly equal to x. | |
https://docs.python.org/3/library/bisect.html#module-bisect | |
''' | |
i = bisect.bisect_left(a, x) | |
if i != len(a) and a[i] == x: | |
return i | |
raise ValueError | |
def defdict(x=0): | |
return collections.defaultdict(lambda: x) | |
def mapadd(xs, y): | |
return [x + y for x in xs] | |
def readint(f): | |
return int(readline(f)) | |
def readints(f, expected=None, sep=None): | |
line = readline(f) | |
xs = [int(e) for e in line.split(sep)] | |
if expected is not None: | |
assert len(xs) == expected, '{} != {}'.format(len(xs), expected) | |
return xs | |
def readline(f): | |
return next(f).strip() | |
def rwh_primes2(n): | |
''' | |
Input n >= 6, returns a list of primes, 2 <= p < n. | |
http://stackoverflow.com/revisions/33356284/2 | |
''' | |
# TODO: Silence PEP8's E221 warning. | |
zero = bytearray([0]) | |
size = n // 3 + (n % 6 == 2) | |
sieve = zero + bytearray([1]) * (size - 1) | |
top = int(math.sqrt(n)) // 3 | |
for i in range(top + 1): | |
if sieve[i]: | |
k = (3*i + 1) | 1 | |
ksq = k * k | |
k2 = k * 2 | |
start = (ksq + k2 * (2 - (i & 1))) // 3 | |
ksqd3 = ksq // 3 | |
sieve[ksqd3::k2] = zero * ((size - ksqd3 - 1) // k2 + 1) | |
sieve[start::k2] = zero * ((size - start - 1) // k2 + 1) | |
ans = [2, 3] | |
poss = itertools.chain.from_iterable( | |
itertools.zip_longest(*[range(i, n, 6) for i in (1, 5)]) | |
) | |
ans.extend(itertools.compress(poss, sieve)) | |
return ans | |
def strjoin(xs, glue=' ', conv=str): | |
return glue.join(map(conv, xs)) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
INPUT="$1_input" | |
OUTPUT="$1_output" | |
./$1 "./$INPUT" | diff "./$OUTPUT" - |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment