UBW 런타임 에러

  • fleo0917
    fleo0917

    UBW 문제를 보자마자 매직코드가 참 pythonic하다 싶어서 아예 파이썬 코드로 바꿔보기로 했습니다. 예를 들면 예제 입력인 아래 코드를

    def hello
     loop 5
      world
    def world
     loop 2
      loop 4
       Blade
    def main
     loop 3
      hello
      world

    아래와 같은 파이썬 코드로 바꾸고,

    def hello():
        DUMMY__()
        for _1 in range(5):
            DUMMY__()
            world()
    def world():
        DUMMY__()
        for _4 in range(2):
            DUMMY__()
            for _5 in range(4):
                DUMMY__()
                Blade()
    def main():
        DUMMY__()
        for _8 in range(3):
            DUMMY__()
            hello()
            world()
    

    exec함수를 통해 파이썬 인터프리터로 직접 실행하는 겁니다. 파이썬코드는 함수선언이나 루프가 비어있어서는 안되기 때문에 함수선언과 루프의 바로 아래에 더미 함수를 집어넣었습니다.

    예제와 같은 간단한 코드는 문제가 없는데, 제출을 해보면 exec부분에서 런타임 에러가 뜨네요. 미처 생각치 못한 예외 케이스가 있는 것 같은데 알려주실 수 있을까요?

    def Blade():
        global cnt
        cnt += 1
        if cnt > 100000:
            raise
    
    
    def DUMMY__():
        pass
    
    
    for T in range(int(input())):
        code = [input() for _ in range(int(input()))]
        pycode = []
        for i, line in enumerate(code):
            striped = line.lstrip()
            indent = ' ' * (4*(len(line) - len(striped)))
            if striped.startswith('def'):
                pycode.append(indent + striped + '():')
                pycode.append(indent + ' '*4 + 'DUMMY__()')
            elif striped.startswith('loop'):
                loop = line.split()[-1]
                pycode.append(indent + 'for %s in range(%s):' % ('_'+str(i), loop))
                pycode.append(indent + ' '*4 + 'DUMMY__()')
            else:
                pycode.append(indent + striped + '()')
    
        cnt = 0
    #    print('\n'.join(pycode))
        exec('\n'.join(pycode))
    
        try:
            main()
        except:
            print(-1)
        else:
            print(cnt)
    


    8년 전
5개의 댓글이 있습니다.
  • kcm1700
    kcm1700

    인덴트 너무 깊어지면 실행 안 해줘요.


    8년 전 link
  • fleo0917
    fleo0917

    감사합니다.. 거기서 걸리면 정직하게 푸는 수 밖에 없네요. ㅜㅜ


    8년 전 link
  • Kureyo
    Kureyo

    인덴트가 깊어질때마다 world_1이런 함수로 다시 정리해서 해보는건 어떨까요(...)


    8년 전 link
  • fleo0917
    fleo0917

    여길 넘어도 maximum recursion depth exceededtime limit exceeded가 기다리고 있을 게 빤하네요...


    8년 전 link
  • Kureyo
    Kureyo

    ㅜㅜ


    8년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.