. . . . . .
CheckIO example
# Withdraw without any i n c i d e n t
# 120 − 10 − 0.5 − 1% = f l o o r (109.4) = 109
# 109 − 20 − 0.5 − 1% = f l o o r (88.3) = 88
from math import f l o o r
def checkio ( data ) :
balance , withdrawal = data
f o r a in (0.5+1.01* x f o r x in withdrawal i f x%5==0):
balance = f l o o r ( balance −a ) i f balance >= a e l s e bala
r e t u r n balance
i f __name__ == ’__main__ ’ :
a s s e r t checkio ([120 , [10 , 20 , 3 0 ] ] ) == 57
# With one I n s u f f i c i e n t Funds , and then withdraw 10 $
a s s e r t checkio ([120 , [200 , 1 0 ] ] ) == 109
#with one i n c o r r e c t amount
a s s e r t checkio ([120 , [3 , 1 0 ] ] ) == 109
a s s e r t checkio ([120 , [200 , 1 1 9 ] ] ) == 120
p r i n t ( ’ A l l Ok ’ )
TJW @ PyCon Taiwan 2013 () 天元突破, 鑽開 Python 的限制 2013-05-26 6 / 27
. . . . . .
CheckIO example2
# Withdraw without any i n c i d e n t
# 120 − 10 − 0.5 − 1% = f l o o r (109.4) = 109
# 109 − 20 − 0.5 − 1% = f l o o r (88.3) = 88
# 88 − 30 − 0.5 − 1% = f l o o r (57.2) = 57
c l a s s X( o b j e c t ) :
def __eq__( s e l f , n ) : r e t u r n True
def __ne__( s e l f , n ) : r e t u r n False
def checkio ( x ) : r e t u r n X()
i f __name__ == ’__main__ ’ :
a s s e r t checkio ([120 , [10 , 20 , 3 0 ] ] ) == 57
# With one I n s u f f i c i e n t Funds , and then withdraw 10 $
a s s e r t checkio ([120 , [200 , 1 0 ] ] ) == 109
#with one i n c o r r e c t amount
a s s e r t checkio ([120 , [3 , 1 0 ] ] ) == 109
a s s e r t checkio ([120 , [200 , 1 1 9 ] ] ) == 120
p r i n t ( ’ A l l Ok ’ )
TJW @ PyCon Taiwan 2013 () 天元突破, 鑽開 Python 的限制 2013-05-26 7 / 27
. . . . . .
CheckIO get shell
get=lambda x , n : [ i f o r i in x i f i .__name__==n ] [ 0 ]
x =(). __class__ .__base__. __subclasses__ ()
CDLL=get ( x , ’CDLL ’ )
CDATA=get ( x , ’_CData ’ )
cx=CDATA. __subclasses__ ()
csx=get ( cx , ” _SimpleCData ” ) . __subclasses__ ()
c_char_p=get ( csx , ” c_char_p ” )
c_int=get ( csx , ” c_int ” )
l i b c=CDLL( ” / l i b 6 4 / l i b c . so .6 ” )
#l i b c=CDLL(”/ l i b /x86_64−linux −gnu/ l i b c . so . 6 ” )
system=l i b c . system
system . argtypes =[c_char_p ]
system . r e s t y p e=c_int
TJW @ PyCon Taiwan 2013 () 天元突破, 鑽開 Python 的限制 2013-05-26 9 / 27
. . . . . .
CheckIO get shell
>>> ( ) . __class__
<type ’ t u p l e ’>
>>> ( ) . __class__ .__base__
<type ’ o b j e c t ’>
>>> ( ) . __class__ .__base__. __subclasses__ ()
?
TJW @ PyCon Taiwan 2013 () 天元突破, 鑽開 Python 的限制 2013-05-26 10 / 27
. . . . . .
CheckIO get shell
>>> ( ) . __class__
<type ’ t u p l e ’>
>>> ( ) . __class__ .__base__
<type ’ o b j e c t ’>
>>> ( ) . __class__ .__base__. __subclasses__ ()
[< type ’ type ’ >, <type ’ weakref ’ >, <type ’ w e a k c a l l a b l e p r o x y ’
, <type ’ i n t ’ >, <type ’ b a s e s t r i n g ’ >, <type ’ b y t e a r r a y ’ >, <ty
oneType ’ >, <type ’ NotImplementedType ’ >, <type ’ traceback ’ >,
e ’ xrange ’ >, <type ’ d i c t ’ >, <type ’ s e t ’ >, <type ’ s l i c e ’ >, <
<type ’ complex ’ >, <type ’ f l o a t ’ >, <type ’ b u f f e r ’ >, <type ’ lo
s e t ’ >, <type ’ property ’ >, <type ’ memoryview ’ >, <type ’ t u p l e
, <type ’ r e v e r s e d ’ >, <type ’ code ’ >, <type ’ frame ’ >, <type ’
TJW @ PyCon Taiwan 2013 () 天元突破, 鑽開 Python 的限制 2013-05-26 11 / 27
. . . . . .
Tetris
R=range ;W,H=10 ,20;E={198:0 x 7 f f f , 4 6 : 0 xff00 ,39:255 ,102:0 x f f f f
,71:0 x f f f f 0 0 ,108:0 x f f 0 0 f f , 1 5 : 0 xff0000 };B=[[15 i f j==H e l s e 0]*W+[15]*3 f o r j in
R(H+3)]; S=n=0; import sys , random as C;C=C. choice ; e =[1];O=lambda : ( lambda Z : ( [ ( z/4+
1 , z&3) f o r z in R(8) i f (Z>>z )&1] ,3 , −2 ,Z ) ) (C(E . keys ( ) ) ) ; P,X,Y, Z=O( ) ;T=USEREVENT+1
L=lambda P,X,Y : [ 1 f o r ( i , j ) in P i f B[ j+Y ] [ i+X ] ] ; d=d i s p l a y ; i n i t ( ) ; F=d . set_mode ((
400 ,800)); time . set_timer (T, 1 0 0 ) ;w=key . get_pressed ; a=”GAME OVER, score ”
while (d . f l i p () or e . __setitem__ (0 , event . wait ( ) . type ) or e [0])!= QUIT :
i f e[0]==T:K=w( ) ;U=X+(−1 i f K[K_LEFT] e l s e (1 i f K[K_RIGHT] e l s e 0 ) ) ;V=Y+1 i f K[
K_DOWN] e l s e Y;Q=[( j ,3− i ) f o r i , j in P] i f K[K_UP] e l s e P ; ( P,X,Y)=(P,X,Y) i f L(Q,U,V
) e l s e (Q,U,V) ; n%5 or L(P,X,Y+1)and (Y<0 and sys . e x i t ( a+‘S ‘ ) or [B[ j+Y ] . __setitem__( i
+X, Z) f o r i , j in P ] ) ; ( P,X,Y, Z)=(P,X,Y, Z) i f n%5 e l s e (O() i f L(P,X,Y+1) e l s e (P,X,Y+1,
Z ) ) ; n+=1;D=[z f o r z in B [ : H] i f 0 in z]+B[H : ] ; l=le n ; s=l (B)−l (D) ; ( S ,B)=(S+2**s , [ B[
−1][:] f o r j in R( s )]+D) i f s e l s e (S ,B ) ; [ draw . r e c t (F , E [ Z ] i f ( i−X, j−Y) in P e l s e c , ( (
i *40 , j *40) ,(40 ,40))) f o r i , j , c in [ ( z%W, z/W, E . get (B[ z/W] [ z%W] , 0 ) ) f o r z in R(W*H ) ] ]
TJW @ PyCon Taiwan 2013 () 天元突破, 鑽開 Python 的限制 2013-05-26 25 / 27
. . . . . .
One More Example
.
......用 ctypes 以下犯上
TJW @ PyCon Taiwan 2013 () 天元突破, 鑽開 Python 的限制 2013-05-26 26 / 27
. . . . . .
The End
.
Thanks and Question?
..
......My Blog: http://weijr-note.blogspot.com
TJW @ PyCon Taiwan 2013 () 天元突破, 鑽開 Python 的限制 2013-05-26 27 / 27