





'''simple docstring'''






# This code is adapted from OpenAI's release
# https://github.com/openai/human-eval/blob/master/human_eval/execution.py

import contextlib
import faulthandler
import io
import multiprocessing
import os
import platform
import signal
import tempfile


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=multiprocessing.Manager()
  _UpperCamelCase       			=manager.list()

  _UpperCamelCase       			=multiprocessing.Process(target=__SCREAMING_SNAKE_CASE  ,      args=(check_program, result, timeout)   )
  p.start()
  p.join(timeout=timeout + 1   )
  if p.is_alive():
    p.kill()

  if not result:
    result.append('''timed out'''   )

  return {
      "task_id": task_id,
      "passed": result[0] == "passed",
      "result": result[0],
      "completion_id": completion_id,
  }


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  with create_tempdir():
    # These system calls are needed when cleaning up tempdir.
    import os
    import shutil

    _UpperCamelCase       			=shutil.rmtree
    _UpperCamelCase       			=os.rmdir
    _UpperCamelCase       			=os.chdir

    # Disable functionalities that can make destructive changes to the test.
    reliability_guard()

    # Run program.
    try:
      _UpperCamelCase       			={}
      with swallow_io():
        with time_limit(__SCREAMING_SNAKE_CASE   ):
          exec(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
      result.append('''passed'''   )
    except TimeoutException:
      result.append('''timed out'''   )
    except BaseException as e:
      result.append(f'''failed: {e}'''   )

    # Needed for cleaning up.
    _UpperCamelCase       			=rmtree
    _UpperCamelCase       			=rmdir
    _UpperCamelCase       			=chdir


@contextlib.contextmanager
def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  def signal_handler(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):
    raise TimeoutException('''Timed out!'''   )

  signal.setitimer(signal.ITIMER_REAL  ,      __SCREAMING_SNAKE_CASE   )
  signal.signal(signal.SIGALRM  ,      __SCREAMING_SNAKE_CASE   )
  try:
    yield
  finally:
    signal.setitimer(signal.ITIMER_REAL  ,      0   )


@contextlib.contextmanager
def 					_a   ():


  """simple docstring"""

  _UpperCamelCase       			=WriteOnlyStringIO()
  with contextlib.redirect_stdout(__SCREAMING_SNAKE_CASE   ):
    with contextlib.redirect_stderr(__SCREAMING_SNAKE_CASE   ):
      with redirect_stdin(__SCREAMING_SNAKE_CASE   ):
        yield


@contextlib.contextmanager
def 					_a   ():


  """simple docstring"""

  with tempfile.TemporaryDirectory() as dirname:
    with chdir(__SCREAMING_SNAKE_CASE   ):
      yield dirname







class 		UpperCAmelCase   (				lowercase_):


  """simple docstring"""







  pass







class 		UpperCAmelCase   (				io.StringIO):


  """simple docstring"""







  def   UpperCamelCase__      (				self    :					Tuple							,	*UpperCamelCase__    :					Optional[Any]							,	**UpperCamelCase__    :					Tuple  )     ->						Tuple:
    raise OSError


  def   UpperCamelCase__      (				self    :					Optional[Any]							,	*UpperCamelCase__    :					Optional[int]							,	**UpperCamelCase__    :					Union[str, Any]  )     ->						int:
    raise OSError


  def   UpperCamelCase__      (				self    :					Union[str, Any]							,	*UpperCamelCase__    :					Dict							,	**UpperCamelCase__    :					List[str]  )     ->						List[Any]:
    raise OSError




  def   UpperCamelCase__      (				self    :					Any							,	*UpperCamelCase__    :					Optional[Any]							,	**UpperCamelCase__    :					str  )     ->						Tuple:
    return False







class 		UpperCAmelCase   (				contextlib._RedirectStream):  # type: ignore


  """simple docstring"""







  lowerCAmelCase_					=				"""stdin"""




@contextlib.contextmanager
def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  if root == ".":
    yield
    return
  _UpperCamelCase       			=os.getcwd()
  os.chdir(__SCREAMING_SNAKE_CASE   )
  try:
    yield
  except BaseException as exc:
    raise exc
  finally:
    os.chdir(__SCREAMING_SNAKE_CASE   )


def 					_a   (__SCREAMING_SNAKE_CASE=None   ):


  """simple docstring"""

  if maximum_memory_bytes is not None:
    import resource

    resource.setrlimit(resource.RLIMIT_AS  ,      (maximum_memory_bytes, maximum_memory_bytes)   )
    resource.setrlimit(resource.RLIMIT_DATA  ,      (maximum_memory_bytes, maximum_memory_bytes)   )
    if not platform.uname().system == "Darwin":
      resource.setrlimit(resource.RLIMIT_STACK  ,      (maximum_memory_bytes, maximum_memory_bytes)   )

  faulthandler.disable()

  import builtins

  _UpperCamelCase       			=None
  _UpperCamelCase       			=None

  import os

  _UpperCamelCase       			='''1'''

  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None

  import shutil

  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None

  import subprocess

  _UpperCamelCase       			=None  # type: ignore

  _UpperCamelCase       			=None

  import sys

  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None
  _UpperCamelCase       			=None













'''simple docstring'''






def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=[[] for _ in range(__SCREAMING_SNAKE_CASE   )]
  _UpperCamelCase       			=key - 1

  if key <= 0:
    raise ValueError('''Height of grid can\'t be 0 or negative'''   )
  if key == 1 or len(__SCREAMING_SNAKE_CASE   ) <= key:
    return input_string

  for position, character in enumerate(__SCREAMING_SNAKE_CASE   ):
    _UpperCamelCase       			=position % (lowest * 2)  # puts it in bounds
    _UpperCamelCase       			=min(__SCREAMING_SNAKE_CASE  ,      lowest * 2 - num   )  # creates zigzag pattern
    temp_grid[num].append(__SCREAMING_SNAKE_CASE   )
  _UpperCamelCase       			=[''''''.join(__SCREAMING_SNAKE_CASE   ) for row in temp_grid]
  _UpperCamelCase       			=''''''.join(__SCREAMING_SNAKE_CASE   )

  return output_string


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=[]
  _UpperCamelCase       			=key - 1

  if key <= 0:
    raise ValueError('''Height of grid can\'t be 0 or negative'''   )
  if key == 1:
    return input_string

  _UpperCamelCase       			=[[] for _ in range(__SCREAMING_SNAKE_CASE   )]  # generates template
  for position in range(len(__SCREAMING_SNAKE_CASE   )   ):
    _UpperCamelCase       			=position % (lowest * 2)  # puts it in bounds
    _UpperCamelCase       			=min(__SCREAMING_SNAKE_CASE  ,      lowest * 2 - num   )  # creates zigzag pattern
    temp_grid[num].append('''*'''   )

  _UpperCamelCase       			=0
  for row in temp_grid:  # fills in the characters
    _UpperCamelCase       			=input_string[counter : counter + len(__SCREAMING_SNAKE_CASE   )]
    grid.append(list(__SCREAMING_SNAKE_CASE   )   )
    counter += len(__SCREAMING_SNAKE_CASE   )

  _UpperCamelCase       			=''''''  # reads as zigzag
  for position in range(len(__SCREAMING_SNAKE_CASE   )   ):
    _UpperCamelCase       			=position % (lowest * 2)  # puts it in bounds
    _UpperCamelCase       			=min(__SCREAMING_SNAKE_CASE  ,      lowest * 2 - num   )  # creates zigzag pattern
    output_string += grid[num][0]
    grid[num].pop(0   )
  return output_string


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			={}
  for key_guess in range(1  ,      len(__SCREAMING_SNAKE_CASE   )   ):  # tries every key
    _UpperCamelCase       			=decrypt(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
  return results


if __name__ == "__main__":
      import doctest

      doctest.testmod()






