[[problem: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부분에서 런타임 에러가 뜨네요. 미처 생각치 못한 예외 케이스가 있는 것 같은데 알려주실 수 있을까요?
~~~ python
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 + '()')
fleo0917
[[problem:UBW]] 문제를 보자마자 매직코드가 참 pythonic하다 싶어서 아예 파이썬 코드로 바꿔보기로 했습니다. 예를 들면 예제 입력인 아래 코드를
아래와 같은 파이썬 코드로 바꾸고,
exec함수를 통해 파이썬 인터프리터로 직접 실행하는 겁니다. 파이썬코드는 함수선언이나 루프가 비어있어서는 안되기 때문에 함수선언과 루프의 바로 아래에 더미 함수를 집어넣었습니다.예제와 같은 간단한 코드는 문제가 없는데, 제출을 해보면 exec부분에서 런타임 에러가 뜨네요. 미처 생각치 못한 예외 케이스가 있는 것 같은데 알려주실 수 있을까요?
~~~ python
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 + '()')
print('\n'.join(pycode))
10년 전