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. HTTP methods 与 RESTful API

    Note GET, primarily used to select resources. Other options for an API method include: POST, primari ...

  2. 符号表实现(Symbol Table Implementations)

    符号表的实现有很多方式,下面介绍其中的几种. 乱序(未排序)数组实现 这种情况,不需要改变数组,操作就在这个数组上执行.在最坏的情况下插入,搜索,删除时间复杂度为O(n). 有序(已排序)数组实现 这 ...

  3. jquery图片3D旋绕效果 rotate3Di的操作

    这是一个图片效果,很简单实用,只需要一个"rotate3Di.js"的插件就行, 关于rotate的用法有如下几种: $(选择器).rotate3Di(30); //把图片3D旋转 ...

  4. VS2012/2013编辑器问题

    1. Visual Studio 2013 'Could not evaluate Expression' Debugger Abnormality 解决办法:http://weblog.west-w ...

  5. tomcat动态映射路径

    写了一个工具类,将上传文件功能保存文件的目录移到webapps目录外面,通过动态生成xml映射文件到tomcat\conf\Catalina\localhost目录下从而实现目录映射.可以被http直 ...

  6. 如何安装CocoaPods

    转自 http://www.99css.com/1321/ 在 iOS 项目开发中,经常会用到第三方的源代码,CocoaPods 就是为了方便管理这些源码的工具. 在官方教程里面,安装看起来非常简单 ...

  7. 与Jquery Mobile的第一次亲密接触

    Jquery Mobile闻名已久,今天终于有亲密接触的机会. 通过动手写的demo,对它有了一个基本的认识: 自带的UI组件用起来简洁,方便:对旧版本的浏览器或移动设备能做到很好的优雅降级,而不影响 ...

  8. 由mysql数据库基础上的php程序实现单词的查询、删除、更改和查询

    我做了一个php程序,将表单数据添加到数据库,借用mysql扩展库函数实现对mysql数据库的操作,能够实现添加单词.删除单词.更新和查询单词.运行环境是普通的mysql数据库和php.Apache服 ...

  9. JavaScript获取元素样式

    原生的JavaScript获取写在标签内部的样式很简单: <div class="test" id="test" style="width:10 ...

  10. (转)WCF中调用WebService出错,大家帮忙看看,回答就有分

    http://bbs.csdn.net/topics/390542345 在WCF项目里面添加了一个WebService引用,然后在我们调用这个WCF服务时,老出错,提示在 ServiceModel  ...