Pysap#3.1 Pythonでショートコーディング
Upcoming SlideShare
Loading in...5
×
 

Pysap#3.1 Pythonでショートコーディング

on

  • 1,255 views

2011/07/30 発表したスライド

2011/07/30 発表したスライド
sourceは、
https://bitbucket.org/tututen/pysap3.1/

Statistics

Views

Total Views
1,255
Views on SlideShare
1,206
Embed Views
49

Actions

Likes
1
Downloads
2
Comments
0

4 Embeds 49

http://d.hatena.ne.jp 40
https://twitter.com 7
https://twimg0-a.akamaihd.net 1
http://tututen.hatenablog.jp 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Pysap#3.1 Pythonでショートコーディング Pysap#3.1 Pythonでショートコーディング Presentation Transcript

  • Python momo_*( tututen) 12011 7 30
  • 22011 7 30
  • • • Python • • C 32011 7 30 View slide
  • ←→ 294km 42011 7 30 View slide
  • • • • • 52011 7 30
  • ATTENTION 62011 7 30
  • • PEP8 • Python • Python • 72011 7 30
  • 82011 7 30
  • • 92011 7 30
  • • • ( ) (hatena keyword ) 102011 7 30
  • • • ( ) (hatena keyword ) • wiki 112011 7 30
  • Python 122011 7 30
  • Python (wikipedia ) 132011 7 30
  • 142011 7 30
  • 152011 7 30
  • • range(0, max_value, 2) 162011 7 30
  • • range(0, max_value, 2) • [i * 2 for i in range(num)] 172011 7 30
  • • range(0, max_value, 2) • [i * 2 for i in range(num)] • a = [] for i in range(num): a.append(i * 2) 182011 7 30
  • 192011 7 30
  • 202011 7 30
  • 3 212011 7 30
  • 3 • OX • • • • 222011 7 30
  • 3 • • • • O or X • • • • 232011 7 30
  • 3 • • • • O or X • • • • 242011 7 30
  • 252011 7 30
  • • • 1 2 • 262011 7 30
  • • • 1 2 ‣ 1 • 272011 7 30
  • • • 1 2 ‣ 1 • ‣ 282011 7 30
  • • field = [] for i in range(9): field.append(‘_‘) 292011 7 30
  • • field = [] for i in range(9): field.append(‘_‘) • field = [‘_‘ for i in range(9)] 302011 7 30
  • • field = [] for i in range(9): field.append(‘_‘) • field = [‘_‘ for i in range(9)] • field = [‘_‘] * 9 312011 7 30
  • 3 • • • • O or X • • • • 322011 7 30
  • 332011 7 30
  • • • O or X ‣ − 342011 7 30
  • • turn = 1 for x range(3): for y range(3): print field[x + y * 3], print ‘’ if turn == 1: print ‘o turn’ else : print ‘x turn’ 352011 7 30
  • • turn = 1 for x range(3): for y range(3): print field[x + y * 3], print ‘’ if turn == 1: print ‘o turn’ else : print ‘x turn’ 362011 7 30
  • • turn = 1 print “%s%s%s¥n%s%s%s¥n%s%s%s” % tuple(field) if turn == 1: print ‘o turn’ else : print ‘x turn’ 372011 7 30
  • • turn = 1 print “%s%s%s¥n%s%s%s¥n%s%s%s” % tuple(field) if turn == 1: print ‘o turn’ else : print ‘x turn’ 382011 7 30
  • • turn = 1 print “%s%s%s¥n%s%s%s¥n%s%s%s” % tuple(field) marks = ‘_ox’ print ‘%s turn’ % marks[turn] ✴marks[1] == ‘o’ ✴marks[-1] == marks[2] == ‘x’ 392011 7 30
  • 3 • • • • O or X • • • • 402011 7 30
  • 412011 7 30
  • • raw_input OK • int • • 0<=x<=2 0<=y<=2 422011 7 30
  • • while(True): try: x = int(raw_input(‘x(0-2) -> ‘)) y = int(raw_input(‘y(0-2) -> ‘)) except: continue if 0 <= x <= 2 and 0 <= y <= 2: break 432011 7 30
  • • while(True): try: x = int(raw_input(‘x(0-2) -> ‘)) y = int(raw_input(‘y(0-2) -> ‘)) except: continue if 0 <= x <= 2 and 0 <= y <= 2: break 442011 7 30
  • • while(True): inp = lambda n: raw_input(‘%s(0-2) -> ‘%n) x, y = inp(‘x’), inp(‘y’) try: x = int(x) y = int(y) except: continue vr = lambda n: 0 <= n <= 2 if vr(x) and vr(y): break 452011 7 30
  • • while(True): inp = lambda n: raw_input(‘%s(0-2) -> ‘%n) x, y = inp(‘x’), inp(‘y’) vr = lambda n: 0 <= n <= 2 if vr(x) and vr(y): break try: x = int(x) y = int(y) except: continue 462011 7 30
  • • while(True): inp = lambda n: raw_input(‘%s(0-2) -> ‘%n) x, y = inp(‘x’), inp(‘y’) vr = lambda n: len(n)==1 and ‘0’ <= n <= ‘2’ if vr(x) and vr(y): break try: x = int(x) y = int(y) except: continue 472011 7 30
  • • while(True): inp = lambda n: raw_input(‘%s(0-2) -> ‘%n) x, y = inp(‘x’), inp(‘y’) vr = lambda n: len(n)==1 and ‘0’ <= n <= ‘2’ if vr(x) and vr(y): break try: x = int(x) y = int(y) except: continue 482011 7 30
  • • while(True): inp = lambda n: raw_input(‘%s(0-2) -> ‘%n) x, y = inp(‘x’), inp(‘y’) vr = lambda n: len(n)==1 and ‘0’ <= n <= ‘2’ if vr(x) and vr(y): break x, y = int(x), int(y) 492011 7 30
  • • inp = lambda n: raw_input(‘%s(0-2) -> ‘%n) vr = lambda n: len(n)==1 and ‘0’ <= n <= ‘2’ while(True): x, y = inp(‘x’), inp(‘y’) if vr(x) and vr(y): break x, y = int(x), int(y) 502011 7 30
  • • while(True): try: x = int(raw_input(‘x(0-2) -> ‘)) y = int(raw_input(‘y(0-2) -> ‘)) except: continue if 0 <= x <= 2 and 0 <= y <= 2: break 512011 7 30
  • • inp = lambda n: raw_input(‘%s(0-2) -> ‘%n) vr = lambda n: len(n)==1 and ‘0’ <= n <= ‘2’ while(True): x, y = inp(‘x’), inp(‘y’) if vr(x) and vr(y): break x, y = int(x), int(y) 522011 7 30
  • 3 • • • • O or X • • • • 532011 7 30
  • O or X 542011 7 30
  • • • 552011 7 30
  • • if field[x + y * 3] != ‘_’: continue 562011 7 30
  • • if field[x + y * 3] != ‘_’: continue • if field[x + y * 3] - ‘_’: continue 572011 7 30
  • • if field[x + y * 3] != ‘_’: continue • if field[x + y * 3] - ‘_’: continue • 582011 7 30
  • 3 • • • • O or X • • • • 592011 7 30
  • 602011 7 30
  • •8 3 • 612011 7 30
  • •8 3 • • OX 622011 7 30
  • • 0 1 2 3 4 5 6 7 8 632011 7 30
  • • 0 1 2 n,n+1,n+2 3 4 5 n,n+3,n+6 6 7 8 ???? 642011 7 30
  • • 0 1 2 n,n+1,n+2 3 4 5 n,n+3,n+6 6 7 8 ???? 652011 7 30
  • • 0 1 2 n,4,8-n 3 4 5 n,n+1,n+2 6 7 8 n,n+3,n+6 662011 7 30
  • • 0 1 2 n,4,8-n 3 4 5 n,n+1,n+2 6 7 8 n,n+3,n+6 672011 7 30
  • • n,4,8-n 0 1 2 3 4 5 n,n+1,n+2 6 7 8 n,n+3,n+6 682011 7 30
  • • for i in range(3): if (field[i] != ‘_’ and field[i] == field[i + 1] and field[i] == field[i + 2]): judge = True if (field[4] != ‘_’ and field[4] == field[i] and field[4] == field[8 - i]): judge = True ... 692011 7 30
  • • for i in range(3): if (field[i] != ‘_’ and field[i] == field[i + 1] and field[i] == field[i + 2]): judge = True if (field[4] != ‘_’ and field[4] == field[i] and field[4] == field[8 - i]): judge = True ... 702011 7 30
  • • mark = ‘ ox’[turn] for i in range(3): if [mark]*3 == [field[i], field[i+1],field[i+2]]: judge = True if [mark]*3 == [field[i],field[4],field[8-i]]: judge = True if [mark]*3 == [field[i], field[i+3],field[i+6]]: judge = True ... 712011 7 30
  • • mark = ‘ ox’[turn] jl = lambda line: [mark]*3 == line for i in range(3): if jl([field[i], field[i+1],field[i+2]]): judge = True if jl([field[i],field[4],field[8-i]]): judge = True if jl([field[i], field[i+3],field[i+6]]): judge = True ... 722011 7 30
  • • mark = ‘ ox’[turn] jl = lambda line: [mark]*3 == line for i in range(3): if jl([field[i], field[i+1],field[i+2]]): judge = True if jl([field[i],field[4],field[8-i]]): judge = True if jl([field[i], field[i+3],field[i+6]]): judge = True ... 732011 7 30
  • • mark = ‘ ox’[turn] jl = lambda line: [mark]*3 == line for i in range(3): if jl(field[i*3:i*3+3]): judge = True if jl(field[i:9-i:4-i]): judge = True if jl(field[i::3]): judge = True 742011 7 30
  • • mark = ‘ ox’[turn] jl = lambda line: [mark]*3 == line judge = False for i in range(3): judge = judge or jl(field[i*3:i*3+3]) judge = judge or jl(field[i:9-i:4-i]) judge = judge or jl(field[i::3]) 752011 7 30
  • 3 • • • • O or X • • • • 762011 7 30
  • 772011 7 30
  • • for if 782011 7 30
  • • for if • Pythonista 792011 7 30
  • • space_flag = False for val in field: if val == ‘_’: space_flag = True if not space_flag: break 802011 7 30
  • • space_flag = False for val in field: if val == ‘_’: space_flag = True if not space_flag: break 812011 7 30
  • • space_flag = False for val in field: if val == ‘_’: space_flag = True if not space_flag: break • if not ‘_’ in field: break 822011 7 30
  • 832011 7 30
  • • • ‣ turn*=-1 ‣ turn=-turn 842011 7 30
  • • • ‣ turn*=-1 ‣ turn=-turn ‣ t*=-1 ‣ t=-t 852011 7 30
  • 862011 7 30
  • • judge • mark 872011 7 30
  • • if judge: print ‘%s win’ % mark else: print ‘draw’ 882011 7 30
  • • if judge: print ‘%s win’ % mark else: print ‘draw’ • print (‘%s win’ % mark) if judge else ‘draw’ 892011 7 30
  • • Python 902011 7 30
  • • Python • 912011 7 30
  • • Python • • 922011 7 30
  • Fin 932011 7 30