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. javascript学习笔记——chrome等提示找不到“getElementsByTagName”的一种解决方法

    最近学习是写了一个小网页,前台有个下拉框是通过后天的xml配置的,在写好代码后使用发现在IE9以及之前的IE浏览器都可以正常获取,但是IE10,chrome和firefox都会在获取一个标签时报get ...

  2. JAVA模拟表单提交

    这是我网上搜的,自己使用也蛮方便,所以上传供大家分享. package wzh.Http;   import java.io.BufferedReader; import java.io.IOExce ...

  3. input文本框获取焦点和失去焦点判断

    onBlur:当输入框失去焦点后 onFocus:当输入框获得焦点后 这两个JavaScript事件是写在html标签中的例如: <input type="text" onB ...

  4. [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    Spring Boot——2分钟构建spring web mvc REST风格HelloWorld http://projects.spring.io/spring-boot/ http://spri ...

  5. IOS深入学习(9)之Objective-C

    1 前言 今天我们来解除一篇有关Objective-C的介绍文章,详情如下. 原文链接:http://blog.csdn.net/developer_zhang/article/details/120 ...

  6. [RxJS] Starting a Stream with SwitchMap & switchMapTo

    From an event map to another event we can use switchMap(), switchMap() accept an function which retu ...

  7. php:兄弟连之面向对象版图形计算器1

    曾经看细说PHP的时候就想做这个,可是一直没什么时间,这次总算忙里偷闲搞了代码量比較多的project. 首先,文档结构,都在一个文件夹下就好了,我的就例如以下. 一開始,进入index.php文件. ...

  8. js过滤

    datagrid:                loadFilter: function (data) {                    return loadFilter(data);   ...

  9. silverlight visifire控件图表制作——silverlight 后台方法打印

    一.后台方法 1.添加引用:using System.Windows.Printing; 2.全局变量://定义图片和文本打印变量  PrintDocument printImage; 3.构造方法体 ...

  10. .net 实现注册邮箱验证激活

    没事上网当了个注册邮箱验证激活的代码,用起来感觉还不错,特意和大家要一起分享一下 下面是主要实现代码: uing System.Net.Mail; public partial class jquer ...