2018校招深信服编程题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huanhuanq1209/article/details/82808146

第一题:

顺时针输出矩阵元素:

def printMatrix(matrix):
    res = []
    while matrix:
        res += matrix.pop(0)
        if matrix and matrix[0]:
            for row in matrix:
                res.append(row.pop())
        if matrix:
            res += matrix.pop()[::-1]
        if matrix and matrix[0]:
            for row in matrix[::-1]:
                res.append(row.pop(0))
    return res
if __name__=='__main__':
    a = raw_input()
    print a
    result = []
    b = a.split('],[')
    print b
    for i in range(len(b)):
        if i == 0:
            t = b[i].lstrip('[[')
            cat = t.split(',')
            result.append(cat)
        elif i == len(b) - 1:
            t = b[i].rstrip(']]')
            cat = t.split(',')
            result.append(cat)
        else:
            cat = b[i].split(',')
            result.append(cat)
    print ','.join(printMatrix(result))

# [[2,3,6],[5,5,9],[3,6,9]]

运行结果:

知识点:

Python join()方法


描述

Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

语法

join()方法语法:

str.join(sequence)

参数

  • sequence -- 要连接的元素序列。

返回值

返回通过指定字符连接序列中元素后生成的新字符串。

实例

以下实例展示了join()的使用方法:

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- str = "-"; seq = ("a", "b", "c"); # 字符串序列 print str.join( seq );

以上实例输出结果如下:

a-b-c

猜你喜欢

转载自blog.csdn.net/huanhuanq1209/article/details/82808146