파이썬 2.7, 2013 점수
이 프로그램은 참조 용으로 사용할 수 있으며 다음 코드를 가져 와서 수정 한 다음 자체 솔루션에 게시 할 수 있습니다.
가늠자에 따르면, 인용 처리에도 정규 표현식을 사용해야했을 수도 있지만 현재 상태에서는 사람들을 문제에 빠뜨리기 시작하는 것으로 충분할 것이라고 생각합니다.
Python 2.7을 선택한 이유 : exec키워드 를 통해 프로그램이 충돌하는지 테스트하는 것이 더 쉬울 것이라고 생각했습니다 .
이 코드는 다음과 같이 프로그램을 수행 in.txt합니다.
나는 인용 파서를 작성하여 참여하고 싶은 사람을 위해 볼 롤링을 얻어야한다고 생각했다. 이 문제의 복잡성.
참고 : 이 축소기에는 여전히 개선의 여지가 충분합니다. 그들은처럼, 내 키워드를 사용하는 때 당신은 들여 쓰기, 변수 이름, 괄호를 제거 함께 놀러 수처럼 print나 yield.
import re
with open("in.txt","r") as fi:
code = fi.read()
class QuoteHandler():
def __init__(self):
pass
def loadCode(self,code):
quoteFlag = False
currentQuoteChar = ""
ignoreNext = False
inEndLineComment=False
startLocation = 0
self.reAddStrings = []
outStr = ""
for i, character in enumerate(code):
if ignoreNext:
ignoreNext = False
elif inEndLineComment:
if character in "\r\n":
inEndLineComment=False
elif character == "#" and not quoteFlag:
inEndLineComment = True
elif character in "'\"" and (currentQuoteChar == character or not quoteFlag):
if quoteFlag:
self.reAddStrings.append(code[startLocation+1:i])
else:
currentQuoteChar = character
startLocation = i
quoteFlag = not quoteFlag
elif character == "\\":
ignoreNext = True
if not inEndLineComment and not quoteFlag:
outStr+=character
return outStr
def find_all_locations(self,substr,code):
return [m.start() for m in re.finditer(substr, code)]
def unloadCode(self,code):
temp = self.reAddStrings[::-1]
for i, location in enumerate(list(self.find_all_locations('"',code))[::-1]):
code = code[:location] + "\"" + temp[i] + code[location:]
return code
def applyRegexes(code):#\w here?
operatorRegexCleaner = ["([\d\/*\-\"=,'+{}:[\](\)])","[ \t]+","(\w)"]
regexes = [
[''.join(operatorRegexCleaner),r"\1\2"],
[''.join(operatorRegexCleaner[::-1]),r"\1\2"],#removes whitespace between operators
["\n\s*\n","\n"]#removes empty lines
]
for regex in regexes:
code = re.sub(regex[0],regex[1],code)
return code
qh = QuoteHandler()
code = qh.loadCode(code)
code = applyRegexes(code)
code = qh.unloadCode(code)
print(code)
exec(code)
프로그램 출력 :
def factor(factor_number):
for n in range(2,factor_number):
if factor_number % n==0:
yield(n)
def gcd(a,b):
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b (so that when
b is divided by it, the result comes out positive).
"""
while b:
a,b=b,a%b
return a
class Apricot:
def __init__(self):
self.mold=False
def get(self):
return self.mold
def update(self):
self.mold=not self.mold
def blue(self):return5
def tell_me_about_these_numbers(*a):
print("%d is the first number!" % a[0])
print("{} / 3 is {}".format(a[0],a[0]/3.))
myFavorate=Apricot()
for number in a:
print list(factor(number))
myFavorate.update()
print[gcd(a,b)for a,b in zip(a[:-1],a[1:])]
print(myFavorate.get())
tell_me_about_these_numbers(5,6,9,45,200)
print"Let's play with scope!"
a,b=10,9
def randomFunction(a):
print(a)
randomFunction(b)
print(a)
for a in range(100):
b+=a
print(a)
print(b)
li=[]
for i in range(10):
li.append(i*2)
print(li)
print([i*2for i in range(10)])
a=c=b=d=e=f=g=h=i=j=k=l=m=n=o=p=q=r=s=t=u=v=w=x=y=z=5
print(a)
a-=1
print(a)
g=10
print(str(10**g+5)[::-1])
def blue_fish(a):
def blue_fish(a):
def blue_fish(a):
return a
a+=1
return blue_fish(a)
a-=1
return blue_fish(a)
print(blue_fish(10))
def blue_fish(a):
if a==0:
return"0"
return"1" +blue_fish(a-1)
print(blue_fish(5))
blue_fish=lambda a,b,c:a*b*c
print(blue_fish(1,2,3))
blue_fish=lambda*a:reduce(lambda a,b:a*b,a)
print(blue_fish(1,2,3))
print(max([[6,1],[5,2],[4,3],[3,4],[2,5],[1,6]],key=lambda a:a[1]))
print(zip(*[[1],[2],[3],[4],[5]]))
print"Now let's test to see if you handle quotes correctly:"
print"test \'many diffent\' \"types of \" quotes, even with \' \" trailing quotes"
print"""
Multi line quotes are great too!
"""
a=""" ::
one more multi-line quote won't hurt
"""
print a