Xs and Os Referee
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的更多相关文章
- Check iO:初学Python
The end of other For language training our Robots want to learn about suffixes. In this task, you ar ...
- python3 os模块
os模块就是对操作系统进行操作,这个模块提供了一种使用操作系统相关功能的可移植方式.1.系统信息 posix.uname_result(sysname='Linux', nodename='liang ...
- JMS学习(六)-ActiveMQ的高可用性实现
原文地址:http://www.cnblogs.com/hapjin/p/5663024.html 一,ActiveMQ高可用性的架构 ActiveMQ的高可用性架构是基于Master/Slave 模 ...
- Android开发8——利用pull解析器读写XML文件
一.基本介绍 对XML解析有SAX和DOM等多种方式,Android中极力推荐xmlpull方式解析xml.xmlpull不仅可用在Android上同样也适用于javase,但在javase环境中需自 ...
- Tic-Tac-Toe
Description Kim likes to play Tic-Tac-Toe. Given a current state, and now Kim is going to take his n ...
- foj Problem 2283 Tic-Tac-Toe
Prob ...
- 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 ...
- FZU Tic-Tac-Toe -.- FZU邀请赛 FZU 2283
Problem L Tic-Tac-Toe Accept: 94 Submit: 184Time Limit: 1000 mSec Memory Limit : 262144 KB Pr ...
- 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 ...
随机推荐
- Linux企业级项目实践之网络爬虫(9)——通过URL抓取网页内容
基本URL包含模式(或称协议).服务器名称(或IP地址).路径和文件名,如"协议://授权/路径?查询".完整的.带有授权部分的普通统一资源标志符语法看上去如下:协议://用户名: ...
- Fiddler 的几个用法
原文地址:http://wenku.baidu.com/link?url=VGYtzCpGdWzyvGFAIgCVS_KbSh5Oemd4rRYqyJORDchy4jxtEiRNWbp0tqPtlBR ...
- 什么是空间复杂度(What is actually Space Complexity ?)
属于空间复杂度(Space Complexity)在很多情况下被错认为是附属空间(Auxiliary Space),下面是附属空间和空间复杂度的定义. 附属空间(Auxiliary Space)是算法 ...
- centos Minicom通信终端
minicom是linux下的串口通信软件,他使用完全使用键盘操作.它虽然没有windows下的超级终端好用,但是它也是一种串口通信的方法.一.minicom安装在超级终端中输入:sally@sall ...
- C# 二分查询
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 搭建完全分布式的hadoop[转]
hadoop 创建用户及hdfs权限,hdfs操作等常用shell命令 sudo addgroup hadoop#添加一个hadoop组sudo usermod -a -G hadoop larry# ...
- python网络编程之socket
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 查看linux版本和内核信息
一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@localhost ~]# cat /proc/versionLinux version 2.6.32 ...
- Mysql 细节记忆
DELIMITER $$ 和 DELIMITER ; DROP PROCEDURE IF EXISTS `pro_follow_getBookBeforeExpired`$$ DECLARE p_Se ...
- JS高级程序设计学习笔记之数组
数组创建的方式 var str = new Array();放入数字即为设置数组长度 var str = []; 数组的length可读可写 监测数组 Array.isArray()方法确定某个值是不 ...