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. j2ee开源项目——IT学习者博客(itxxzblog v1.0)

    大家好,我是IT学习者-螃蟹,已经有近一周的时间没有更新文章了,作为回报,今天起将更新一个大件,也就是螃蟹还在进行中的IT学习者博客. IT学习者博客的初期设计已经完成,功能也已经完成了大半,具备了当 ...

  2. cf437C The Child and Toy

    C. The Child and Toy time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. POJ3580---SuperMemo (Splay)

    各种操作,区间更新,求最值.翻转.插入.删除.当然是Splay这种神器了. 主要是 revolve这个操作,其实也就是3个区间翻转放到一块, 比如 REVOLVE x y T,T %= (y-x+1) ...

  4. js设计模式系列之(一)请节约你的请求-代理模式

    What’s the proxy pattern? 代理模式其实就是将违反单一性原则的类给抽离出来,尽量满足开放和封闭的原则. 相当于一个类的行为只是一种,但是你可以给这个类添加额外的行为.比如: 一 ...

  5. Maven--生命周期和插件(四)

    <Maven--搭建开发环境(一)> <Maven--构建企业级仓库(二)> <Maven—几个需要补充的问题(三)> <Maven—生命周期和插件(四)&g ...

  6. c语言结构体4之结构体引用

    struct mystruct{ char str[23];}; 1结构体变量不能整体引用 struct data m: printf("%s",m);//m是结构体变量 2 st ...

  7. for的用法

    第一次看到这么用,哈哈,就记下 for (var control = ["程", "陈", "是"]; control[0]; contro ...

  8. 多封装,少开放。强烈建议C++标准添加class之间的注入机制

    近日在改动了一下下引擎代码(为了自己的组件),发现有些接口是仅仅有特定类及其内部函数才去訪问,却不使用友元声明的形式进行数据訪问--当然使用了普通非virtual的形式也就是意味着不建议重载. 故此: ...

  9. python 之 Paramiko学习

    paramiko模块,基于SSH用于连接远程服务器并执行相关操作. 一.安装 pip3 install paramiko 二.使用 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码 ...

  10. 学习okhttp wiki--Connections.

    Connections 尽管你只提供了URL,OkHttp使用三种类型来创建它和你的web服务器的连接:URL,地址(Address)和路由(Route). URLs URLs (例如 https:/ ...