Skip to content

Instantly share code, notes, and snippets.

@ognis1205
Created August 15, 2022 04:38
Show Gist options
  • Save ognis1205/3d78bd05fd261b87646ebdb7efa7eb14 to your computer and use it in GitHub Desktop.
Save ognis1205/3d78bd05fd261b87646ebdb7efa7eb14 to your computer and use it in GitHub Desktop.
import sys
from io import StringIO
from re import split
from textwrap import dedent
from traceback import format_exc
class UserInput:
def __init__(self, text=None):
self._io = StringIO(text) if text else sys.stdin
def __enter__(self):
return self
def __exit__(self, *_):
if hasattr(self._io, 'close'):
self._io.close()
def readline(self, parse=str, is_array=False, delimiter=r'\s+', clean=lambda x: x):
if line := self._readline(clean):
return [parse(x) for x in split(delimiter, line)] if is_array else parse(line)
return None
def _readline(self, clean):
if line := self._io.readline():
return clean(line.strip())
return None
INPUT = dedent('''\
[2000,4000]
[1000,2000]
[3000,6000]
[1000,3000]
[2000,5000]
[4000,7000]
[4000,8000]
[4000,9000]
[4000,1100]
[4000,1200]
[3000,1300]
[3000,1400]
[7000,1500]
[7000,1600]
[7000,1700]
[7000,1800]
[7000,1900]
[7000,1110]
[3000,1120]
[3000,1130]
[1000,1140]
[1000,1150]
[1130,1160]
[1160,1170]
''')
def main():
with UserInput(INPUT) as user_input:
while data := user_input.readline(
parse=int,
is_array=True,
delimiter=r'\s*,\s*',
clean=lambda x: x.lstrip('[').rstrip(']')):
print(f'data: {data}')
if __name__ == '__main__':
try:
main()
except:
print(format_exc(), file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment