Python                momo_*(        tututen)                           12011   7   30
22011   7   30
•                • Python                •                •          C                               32011   7   30
←→   294km                         42011   7   30
•       •            •            •                52011   7   30
ATTENTION                    62011   7   30
•              PEP8                • Python                • Python                •                           72011   7  ...
82011   7   30
•                    92011   7   30
•                •                    (   )                            (hatena keyword   )                            1020...
•                •                    (   )                            (hatena keyword   )                •               ...
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 ra...
192011   7   30
202011   7   30
3                    212011   7   30
3                •       OX                •                    •                    •                    •               ...
3                •                •                •                •        O or X                •                •     ...
3                •                •                •                •        O or X                •                •     ...
252011   7   30
•                •   1   2                •                            262011   7   30
•                •   1   2                    ‣   1                •                            272011   7   30
•                •   1   2                    ‣   1                •                    ‣                            28201...
• field = []                  for i in range(9):                     field.append(‘_‘)                                   292...
• field = []                  for i in range(9):                     field.append(‘_‘)                • field = [‘_‘ for i in...
• field = []                  for i in range(9):                     field.append(‘_‘)                • field = [‘_‘ for i in...
3                •                •                •                •        O or X                •                •     ...
332011   7   30
•                • O or X                  ‣ −                           342011   7   30
• turn = 1                  for x range(3):                      for y range(3):                         print field[x + y ...
• turn = 1                  for x range(3):                      for y range(3):                         print field[x + y ...
• turn = 1                  print “%s%s%s¥n%s%s%s¥n%s%s%s”                      % tuple(field)                  if turn == ...
• turn = 1                  print “%s%s%s¥n%s%s%s¥n%s%s%s”                      % tuple(field)                  if turn == ...
• turn = 1                  print “%s%s%s¥n%s%s%s¥n%s%s%s”                     % tuple(field)                  marks = ‘_ox...
3                •                •                •                •        O or X                •                •     ...
412011   7   30
• raw_input   OK                • int                •                •                    0<=x<=2    0<=y<=2             ...
• while(True):                     try:                         x = int(raw_input(‘x(0-2) -> ‘))                         y...
• while(True):                     try:                         x = int(raw_input(‘x(0-2) -> ‘))                         y...
• while(True):                     inp = lambda n: raw_input(‘%s(0-2) -> ‘%n)                     x, y = inp(‘x’), inp(‘y’...
• while(True):                     inp = lambda n: raw_input(‘%s(0-2) -> ‘%n)                     x, y = inp(‘x’), inp(‘y’...
• while(True):                     inp = lambda n: raw_input(‘%s(0-2) -> ‘%n)                     x, y = inp(‘x’), inp(‘y’...
• while(True):                     inp = lambda n: raw_input(‘%s(0-2) -> ‘%n)                     x, y = inp(‘x’), inp(‘y’...
• while(True):                     inp = lambda n: raw_input(‘%s(0-2) -> ‘%n)                     x, y = inp(‘x’), inp(‘y’...
• inp = lambda n: raw_input(‘%s(0-2) -> ‘%n)                  vr = lambda n: len(n)==1 and ‘0’ <= n <= ‘2’                ...
• while(True):                     try:                         x = int(raw_input(‘x(0-2) -> ‘))                         y...
• inp = lambda n: raw_input(‘%s(0-2) -> ‘%n)                  vr = lambda n: len(n)==1 and ‘0’ <= n <= ‘2’                ...
3                •                •                •                •        O or X                •                •     ...
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] - ‘_’: ...
• if field[x + y * 3] != ‘_’:                       continue                •                    if field[x + y * 3] - ‘_’: ...
3                •                •                •                •        O or X                •                •     ...
602011   7   30
•8   3                •                         612011   7   30
•8        3                •                •                     OX                              622011   7   30
•                    0   1   2                    3   4   5                    6   7   8                                63...
•                    0   1   2                                     n,n+1,n+2                    3   4   5                 ...
•                    0   1   2                                     n,n+1,n+2                    3   4   5                 ...
•                    0   1   2                                      n,4,8-n                    3   4   5                  ...
•                    0   1   2                                      n,4,8-n                    3   4   5                  ...
•                                      n,4,8-n                    0   1   2                    3   4   5        n,n+1,n+2 ...
• for i in range(3):                        if (field[i] != ‘_’ and                            field[i] == field[i + 1] and  ...
• for i in range(3):                        if (field[i] != ‘_’ and                            field[i] == field[i + 1] and  ...
• mark = ‘ ox’[turn]                  for i in range(3):                      if [mark]*3 == [field[i], field[i+1],field[i+2]...
• mark = ‘ ox’[turn]                  jl = lambda line: [mark]*3 == line                  for i in range(3):              ...
• mark = ‘ ox’[turn]                  jl = lambda line: [mark]*3 == line                  for i in range(3):              ...
• mark = ‘ ox’[turn]                  jl = lambda line: [mark]*3 == line                  for i in range(3):              ...
• mark = ‘ ox’[turn]                  jl = lambda line: [mark]*3 == line                  judge = False                  f...
3                •                •                •                •        O or X                •                •     ...
772011   7   30
• for   if                         782011   7   30
• for   if                •             Pythonista                         792011   7   30
• space_flag = False                  for val in field:                      if val == ‘_’:                          space_fl...
• space_flag = False                  for val in field:                      if val == ‘_’:                          space_fl...
• space_flag = False                  for val in field:                      if val == ‘_’:                          space_fl...
832011   7   30
•                •                    ‣ turn*=-1                    ‣ turn=-turn                                   842011 ...
•                •                    ‣ turn*=-1                    ‣ turn=-turn                    ‣ t*=-1               ...
862011   7   30
• judge                •              mark                          872011   7   30
• if judge:                     print ‘%s win’ % mark                  else:                     print ‘draw’             ...
• if judge:                     print ‘%s win’ % mark                  else:                     print ‘draw’             ...
•   Python                     902011   7   30
•   Python                •                     912011   7   30
•   Python                •                •                     922011   7   30
Fin                 932011   7   30
Upcoming SlideShare
Loading in …5
×

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

1,950 views

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,950
On SlideShare
0
From Embeds
0
Number of Embeds
319
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

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

  1. 1. Python momo_*( tututen) 12011 7 30
  2. 2. 22011 7 30
  3. 3. • • Python • • C 32011 7 30
  4. 4. ←→ 294km 42011 7 30
  5. 5. • • • • 52011 7 30
  6. 6. ATTENTION 62011 7 30
  7. 7. • PEP8 • Python • Python • 72011 7 30
  8. 8. 82011 7 30
  9. 9. • 92011 7 30
  10. 10. • • ( ) (hatena keyword ) 102011 7 30
  11. 11. • • ( ) (hatena keyword ) • wiki 112011 7 30
  12. 12. Python 122011 7 30
  13. 13. Python (wikipedia ) 132011 7 30
  14. 14. 142011 7 30
  15. 15. 152011 7 30
  16. 16. • range(0, max_value, 2) 162011 7 30
  17. 17. • range(0, max_value, 2) • [i * 2 for i in range(num)] 172011 7 30
  18. 18. • 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
  19. 19. 192011 7 30
  20. 20. 202011 7 30
  21. 21. 3 212011 7 30
  22. 22. 3 • OX • • • • 222011 7 30
  23. 23. 3 • • • • O or X • • • • 232011 7 30
  24. 24. 3 • • • • O or X • • • • 242011 7 30
  25. 25. 252011 7 30
  26. 26. • • 1 2 • 262011 7 30
  27. 27. • • 1 2 ‣ 1 • 272011 7 30
  28. 28. • • 1 2 ‣ 1 • ‣ 282011 7 30
  29. 29. • field = [] for i in range(9): field.append(‘_‘) 292011 7 30
  30. 30. • field = [] for i in range(9): field.append(‘_‘) • field = [‘_‘ for i in range(9)] 302011 7 30
  31. 31. • field = [] for i in range(9): field.append(‘_‘) • field = [‘_‘ for i in range(9)] • field = [‘_‘] * 9 312011 7 30
  32. 32. 3 • • • • O or X • • • • 322011 7 30
  33. 33. 332011 7 30
  34. 34. • • O or X ‣ − 342011 7 30
  35. 35. • 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
  36. 36. • 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
  37. 37. • 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
  38. 38. • 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
  39. 39. • 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
  40. 40. 3 • • • • O or X • • • • 402011 7 30
  41. 41. 412011 7 30
  42. 42. • raw_input OK • int • • 0<=x<=2 0<=y<=2 422011 7 30
  43. 43. • 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
  44. 44. • 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
  45. 45. • 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
  46. 46. • 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
  47. 47. • 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
  48. 48. • 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
  49. 49. • 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
  50. 50. • 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
  51. 51. • 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
  52. 52. • 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
  53. 53. 3 • • • • O or X • • • • 532011 7 30
  54. 54. O or X 542011 7 30
  55. 55. • • 552011 7 30
  56. 56. • if field[x + y * 3] != ‘_’: continue 562011 7 30
  57. 57. • if field[x + y * 3] != ‘_’: continue • if field[x + y * 3] - ‘_’: continue 572011 7 30
  58. 58. • if field[x + y * 3] != ‘_’: continue • if field[x + y * 3] - ‘_’: continue • 582011 7 30
  59. 59. 3 • • • • O or X • • • • 592011 7 30
  60. 60. 602011 7 30
  61. 61. •8 3 • 612011 7 30
  62. 62. •8 3 • • OX 622011 7 30
  63. 63. • 0 1 2 3 4 5 6 7 8 632011 7 30
  64. 64. • 0 1 2 n,n+1,n+2 3 4 5 n,n+3,n+6 6 7 8 ???? 642011 7 30
  65. 65. • 0 1 2 n,n+1,n+2 3 4 5 n,n+3,n+6 6 7 8 ???? 652011 7 30
  66. 66. • 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
  67. 67. • 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
  68. 68. • 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
  69. 69. • 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
  70. 70. • 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
  71. 71. • 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
  72. 72. • 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
  73. 73. • 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
  74. 74. • 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
  75. 75. • 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
  76. 76. 3 • • • • O or X • • • • 762011 7 30
  77. 77. 772011 7 30
  78. 78. • for if 782011 7 30
  79. 79. • for if • Pythonista 792011 7 30
  80. 80. • space_flag = False for val in field: if val == ‘_’: space_flag = True if not space_flag: break 802011 7 30
  81. 81. • space_flag = False for val in field: if val == ‘_’: space_flag = True if not space_flag: break 812011 7 30
  82. 82. • 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
  83. 83. 832011 7 30
  84. 84. • • ‣ turn*=-1 ‣ turn=-turn 842011 7 30
  85. 85. • • ‣ turn*=-1 ‣ turn=-turn ‣ t*=-1 ‣ t=-t 852011 7 30
  86. 86. 862011 7 30
  87. 87. • judge • mark 872011 7 30
  88. 88. • if judge: print ‘%s win’ % mark else: print ‘draw’ 882011 7 30
  89. 89. • if judge: print ‘%s win’ % mark else: print ‘draw’ • print (‘%s win’ % mark) if judge else ‘draw’ 892011 7 30
  90. 90. • Python 902011 7 30
  91. 91. • Python • 912011 7 30
  92. 92. • Python • • 922011 7 30
  93. 93. Fin 932011 7 30

×