Xs and Os Referee

 1 def checkio(game_result):
2 winner = 'D'
3
4 for row in game_result:
5 if row[0] == row[1] == row[2] and row[0] != '.':
6 winner = row[0]
7
8 for col in range(0, 3):
9 if game_result[0][col] == game_result[1][col] == game_result[2][col] and game_result[0][col] != '.':
10 winner = game_result[0][col]
11
12 if game_result[0][0] == game_result[1][1] == game_result[2][2] and game_result[0][0] != '.':
13 winner = game_result[0][0]
14
15 if game_result[0][2] == game_result[1][1] == game_result[2][0] and game_result[0][2] != '.':
16 winner = game_result[0][2]
17
18 return winner

此题的结论是python支持形如此等模式的判断: row[0] == row[1] == row[2], 即支持连等

再来看看大神代码

1 def checkio(result):
2 rows = result
3 cols = map(''.join, zip(*rows))
4 diags = map(''.join, zip(*[(r[i], r[2 - i]) for i, r in enumerate(rows)]))
5 lines = rows + list(cols) + list(diags)
6
7 return 'X' if ('XXX' in lines) else 'O' if ('OOO' in lines) else 'D'

zip函数可将两个数组柔和在一起,如学生姓名name = ['bob', 'jenny'], 成绩grade = [80, 90], zip(name, grade) = [('bob', 80), ('jenny', 90)], 在函数调用中使用*list/tuple的方式表示将list/tuple分开,作为位置参数传递给对应函数(前提是对应函数支持不定个数的位置参数), 如test = ["XXX", "OOO", "..."], zip(*test) = [('X', 'O', '.'), ('X', 'O', '.'), ('X', 'O', '.')]

map函数接受两个参数, 第一个为函数名, 第二个为可迭代对象, 如array = [1, 2, 3], map(str, array) = ['1', '2', '3'], 即对第二个对象应用第一函数

Xs and Os Referee的更多相关文章

  1. Check iO:初学Python

    The end of other For language training our Robots want to learn about suffixes. In this task, you ar ...

  2. python3 os模块

    os模块就是对操作系统进行操作,这个模块提供了一种使用操作系统相关功能的可移植方式.1.系统信息 posix.uname_result(sysname='Linux', nodename='liang ...

  3. JMS学习(六)-ActiveMQ的高可用性实现

    原文地址:http://www.cnblogs.com/hapjin/p/5663024.html 一,ActiveMQ高可用性的架构 ActiveMQ的高可用性架构是基于Master/Slave 模 ...

  4. Android开发8——利用pull解析器读写XML文件

    一.基本介绍 对XML解析有SAX和DOM等多种方式,Android中极力推荐xmlpull方式解析xml.xmlpull不仅可用在Android上同样也适用于javase,但在javase环境中需自 ...

  5. Tic-Tac-Toe

    Description Kim likes to play Tic-Tac-Toe. Given a current state, and now Kim is going to take his n ...

  6. foj Problem 2283 Tic-Tac-Toe

                                                                                                    Prob ...

  7. 2017福建省赛 L Tic-Tac-Toe 模拟

    Kim likes to play Tic-Tac-Toe. Given a current state, and now Kim is going to take his next move. Pl ...

  8. FZU Tic-Tac-Toe -.- FZU邀请赛 FZU 2283

    Problem L Tic-Tac-Toe Accept: 94    Submit: 184Time Limit: 1000 mSec    Memory Limit : 262144 KB  Pr ...

  9. 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

    题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...

随机推荐

  1. Row Cache Objects

    This latch comes into play when user processes are attempting to access or update the cached data di ...

  2. 【转】Ubuntu命令行下安装、卸载、管理软件包的方法

    原文网址:http://oss.org.cn/html/47/n-67447.html 一.Ubuntu中软件安装方法 1.APT方式 (1)普通安装:apt-get install softname ...

  3. Android apk获取系统权限

    Android在apk内部,即通过java代码来进行修改系统文件或者修改系统设置等等,这样需要获取系统权限. 通过直接配置apk运行在System进程内 1. 在应用程序的AndroidManifes ...

  4. poj 2456 Aggressive cows(二分搜索之最大化最小值)

    Description Farmer John has built a <= N <= ,) stalls. The stalls are located along a straight ...

  5. WPF与输入法冲突研究之一:百度输入法会导致WPF程序的崩溃!

    在学习和使用了WPF一段时间之后,有点感觉WPF是个不太成熟的框架,不知道是我学的太肤浅,还是WPF得BUG太多! >>>>>>>模拟场景<<&l ...

  6. C#使用WebKitBrowser.dll填坑记

    .Net 自带的 Webbrowser 有着太多的平台限制.对于用户体验之上的今天,这无疑是一个噩梦, 然后就开始找 .Net下的WebKitBrowser.dll (后面提供下载) 从开源网站下到程 ...

  7. JMeter创建FTP测试

    FTP服务主要提供上传和下载功能.有时间需要我们测试服务器上传和下载的性能.在这里我通过JMeter做一个FTP测试计划的例子. * 使用的是JMeter2.4版本. * 测试的服务器是IP:124. ...

  8. TTB 基本

    中文名 ,线程构建模块 外文名 Thread Building Blocks 缩    写 TBB 开    发 intel 目录 1线程构建模块 2黑体亮温 3斜交载重轮胎 4串联球轴承     1 ...

  9. Samba的ADS域模式和RPC域模式

    对于Samba服务器,有两种域安全模式,加入到Windows 2000或者Windows 2003域控制器(DC‘s)控制的域中: RPC 模式 RPC(远程过程调用)模式的域成员是"NT4 ...

  10. java基础之集合

    集合的定义,集合的应用,集合的分类,集合的遍历,集合的特性