Python, 1110 byte
Toán tử quá tải không phải là xấu, phải không ??
from re import*
class V(str):
def __add__(s,r):return s[:-1]+chr(ord(s[-1])+r)
class S(str):
def __str__(s):return "'"+s+"'"if '"'in s else'"'+s+'"'
def __repr__(s):return str(s)
def __add__(s,r):s=str(s)[1:-1];return S('['+s+']'if type(r)==L else '"+"' if(s,r)==('','')else s+r)
class I(int):
def __add__(s,r):return type(r)(int(s)+int(r))if s!=r else V('DONE')
def __div__(s,r):return N if r==0 else int(s)/int(r)
def __pos__(s):return s+c*10
def __mul__(s,r):return V('NaN.'+'0'*13+'13')if r==1 else int(s)*int(r)
class L(list):
def __add__(s,r):return V(str(r==s[-1]+1).upper())
def RANGE(a,b=0):return 2*(a,S(chr(ord(a)+1)))if b==0 else tuple([a]+[b-1,a+2]*((b-a)/4)+[b-1,b])
def FLOOR(n):return V('|\n|\n|\n|___%s___'%n)
def colorsrgb(c):
m={'blue':V('#0000FF')}
return m.get(c,N)
def colorssort():return V('rainbow')
N=V('NaN')
c=1
while True:
try:l=raw_input('[%d] >'%c)
except:break
l=sub(r'(?<!"|\.)(\d+)(?!\.|\d)',r'I(\1)',l)
l=sub(r'"(.*?)"',r'S("\1")',l)
l=sub(r'\[(.*?)\]',r'L([\1])',l)
l=sub(r'/\(','*(',l)
l=sub('s\.','s',l)
for x in str(eval(l)).split('\n'):print ' =',x
c+=1
Mục tiêu của tôi không phải là chiến thắng nhiều (rõ ràng) vì nó làm cho nó càng chung chung càng tốt. Rất ít được mã hóa cứng. Hãy thử những thứ như thế RANGE(10)
, 9*1
và RANGE("A")
, (2/0)+14
và "123"
cho kết quả thú vị!
Đây là một phiên mẫu:
ryan@DevPC-LX:~/golf/xktp$ python xktp.py
[1] >ryan@DevPC-LX:~/golf/xktp$ python xktp.py
[1] >1+1
= DONE
[2] >2+"2"
= "4"
[3] >"2"+2
Traceback (most recent call last):
File "xktp.py", line 31, in <module>
for x in str(eval(l)).split('\n'):print ' =',x
File "<string>", line 1, in <module>
File "xktp.py", line 7, in __add__
def __add__(s,r):s=str(s)[1:-1];return S('['+s+']'if type(r)==L else '"+"' if(s,r)==('','')else s+r)
TypeError: cannot concatenate 'str' and 'I' objects
ryan@DevPC-LX:~/golf/xktp$ python xktp.py
[1] >ryan@DevPC-LX:~/golf/xktp$
ryan@DevPC-LX:~/golf/xktp$
ryan@DevPC-LX:~/golf/xktp$
ryan@DevPC-LX:~/golf/xktp$ python xktp.py
[1] >2+"2"
= "4"
[2] >"2"+[]
= "[2]"
[3] >"2"+[1, 2, 3]
= "[2]"
[4] >(2/0)
= NaN
[5] >(2/0)+2
= NaP
[6] >(2/0)+14
= Na\
[7] >""+""
= '"+"'
[8] >[1,2,3]+2
= FALSE
[9] >[1,2,3]+4
= TRUE
[10] >[1,2,3,4,5,6,7]+9
= FALSE
[11] >[1,2,3,4,5,6,7]+8
= TRUE
[12] >2/(2-(3/2+1/2))
= NaN.000000000000013
[13] >9*1
= NaN.000000000000013
[14] >RANGE(" ")
= (" ", "!", " ", "!")
[15] >RANGE("2")
= ("2", "3", "2", "3")
[16] >RANGE(2)
Traceback (most recent call last):
File "xktp.py", line 31, in <module>
for x in str(eval(l)).split('\n'):print ' =',x
File "<string>", line 1, in <module>
File "xktp.py", line 15, in RANGE
def RANGE(a,b=0):return 2*(a,S(chr(ord(a)+1)))if b==0 else tuple([a]+[b-1,a+2]*((b-a)/4)+[b-1,b])
TypeError: ord() expected string of length 1, but I found
ryan@DevPC-LX:~/golf/xktp$ python xktp.py
[1] >ryan@DevPC-LX:~/golf/xktp$ # oops
ryan@DevPC-LX:~/golf/xktp$ python xktp.py
[1] >RANGE("2")
= ("2", "3", "2", "3")
[2] >RANGE(2*1)
Traceback (most recent call last):
File "xktp.py", line 31, in <module>
for x in str(eval(l)).split('\n'):print ' =',x
File "<string>", line 1, in <module>
File "xktp.py", line 15, in RANGE
def RANGE(a,b=0):return 2*(a,S(chr(ord(a)+1)))if b==0 else tuple([a]+[b-1,a+2]*((b-a)/4)+[b-1,b])
TypeError: ord() expected a character, but string of length 19 found
ryan@DevPC-LX:~/golf/xktp$ python xktp.py # oops again
[1] >RANGE(1,20)
= (1, 19, 3, 19, 3, 19, 3, 19, 3, 19, 20)
[2] >RANGE(1,5)
= (1, 4, 3, 4, 5)
[3] >RANGE(10,20)
= (10, 19, 12, 19, 12, 19, 20)
[4] >RANGE(10,200)
= (10, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 12, 199, 200)
[5] >+2
= 52
[6] >+"99"
Traceback (most recent call last):
File "xktp.py", line 31, in <module>
for x in str(eval(l)).split('\n'):print ' =',x
File "<string>", line 1, in <module>
TypeError: bad operand type for unary +: 'S'
ryan@DevPC-LX:~/golf/xktp$ python xktp.py # oops again and again!
[1] >FLOOR(200)
= |
= |
= |
= |___200___
[2] >2+2
= DONE
[3] >3+#
Traceback (most recent call last):
File "xktp.py", line 31, in <module>
for x in str(eval(l)).split('\n'):print ' =',x
File "<string>", line 1
I(3)+#
^
SyntaxError: unexpected EOF while parsing
ryan@DevPC-LX:~/golf/xktp$ python xktp.py
[1] >3+3
= DONE
[2] >ryan@DevPC-LX:~/golf/xktp$
10.5
không?