python zip(), map()

Question:

Tic-Tac-Toe, sometimes also known as Xs andOs, is a game for two players (X and O) who take turns marking the spaces in a3×3 grid. The player who succeeds in placing three respective marks in ahorizontal, vertical, or diagonal rows (NW-SE and NE-SW) wins the game.

But we will not be playing this game. Youwill be the referee for this games results. You are given a result of a gameand you must determine if the game ends in a win or a draw as well as who willbe the winner. Make sure to return "X" if the X-player wins and"O" if the O-player wins. If the game is a draw, return"D".


A game's result is presented as a list ofstrings, where "X" and "O" are players' marks and"." is the empty cell.

Input: A game result as a list ofstrings (unicode).

Output: "X", "O"or "D" as a string.

Precondition:
There is either one winner or a draw.
len(game_result) == 3
all(len(row) == 3 for row in game_result)



Code:

def checkio(game_result):
    g = game_result
    inverse = zip(*g)
    # diags
    if g[0][0] == g[1][1] and g[1][1] == g[2][2] and g[0][0] != '.':
        return g[0][0]
    elif g[0][2] == g[1][1] and g[1][1] == g[2][0] and g[0][2] != '.':
        return g[0][2]
    
    # rows and cols
    else:
        for row in g:
            for col in range(3):
                if row.count(row[col]) == 3 and row[col] != '.':
                    return row[col]
        for row in inverse:
            for col in range(3):
                if row.count(row[col]) == 3 and row[col] != '.':
                    return row[col]
    return 'D'



Check:

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert checkio([
        "X.O",
        "XX.",
        "XOO"]) == "X", "Xs wins"
    assert checkio([
        "OO.",
        "XOX",
        "XOX"]) == "O", "Os wins"
    assert checkio([
        "OOX",
        "XXO",
        "OXX"]) == "D", "Draw"
    assert checkio([
        "O.X",
        "XX.",
        "XOO"]) == "X", "Xs wins again"


扩展:

1. zip([iterable, ...]) zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的list(列表)。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。利用*号操作符,可以将list unzip(解压)

>>> help(zip)
 Help on built-in function zip in module __builtin__:
 
 zip(...)
     zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
     
     Return a list of tuples, where each tuple contains the i-th element
     from each of the argument sequences.  The returned list is truncated
     in length to the length of the shortest argument sequence.

eg.

>>> a = [1,2,3]
>>> b = [4,5,6] 
>>> c = [4,5,6,7,8] 
>>> zipped = zip(a,b)
[(14)(25)(36)] 
>>> zip(a,c) 
[(14)(25)(36)] 
>>> zip(*zipped)
[(123)(456)]

>>> a = [‘jack', 'beginman', 'sony']
>>> b = [2001,2003,2005] 
>>> zipped = zip(a,b)
[('jack', 2001)('beginman'2003), ('sony', 2005)]


2. map(function, sequence[, sequence, ...]) 根据提供的函数对指定序列做映射

参数:

function: 函数(一对一或多对一函数)

sequence[]: 一个或多个序列

返回值:一个集合(map将序列中每一个元素调用function函数,返回包含每次function函数返回值的list)

eg. 对一个序列中的每个元素进行平方运算:

map(lambda x: x ** 2, [1, 2, 3, 4, 5])

返回结果为:

[1, 4, 9, 16, 25]


在参数存在多个序列时,会依次以每个序列中相同位置的元素做参数调用function函数。

eg.要对两个序列中的元素依次求和。

map(lambda x, y: x + y, [1, 3, 5, 7, 9],[2, 4, 6, 8, 10])

返回结果为:

[3, 7, 11, 15, 19]


要注意function函数的参数数量要和map中提供的集合数量相匹配。如果集合长度不相等,会以最小长度对所有集合进行截取。

当函数为None时,操作和zip相似:

map(None, [1, 3, 5, 7, 9], [2, 4, 6, 8,10])

返回结果为:

[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]



3. 二维矩阵变换(行列互换):

>>> a = [[123][456][789]]
>>>[row[col] for row in a] for col in range(len(a[0]))]
>>> zip(*a) [(147)(258)(369)]
>>> map(list,zip(*a)) [[147][258][369]]



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值