美文网首页
关于Matrix Script的解法以及正则总结

关于Matrix Script的解法以及正则总结

作者: A_You | 来源:发表于2019-06-11 23:43 被阅读0次

问题参照:

解题思路

  • 将N * M矩阵转转化为具有顺序的一维数组
  • 将一维数组按照顺序拼接字符串
  • 利用正则过滤字符串

code

#!/bin/python3

import math
import os
import random
import re
import sys
if __name__ == '__main__':
    nm = input().split()
    n = int(nm[0])
    m = int(nm[1])
    matrix = []
    for _ in range(n):
        matrix_item = input()
        matrix.append(matrix_item)
    row_column = ["0"]*(n*m)
    row_number = 0
    for index, row in enumerate(matrix):
        for column_index in range(len(row)):
            row_column[column_index*n + index] = row[column_index]
    string = ''
    for i in range(len(row_column)):
        string = string + row_column[i]
    string = "".join('%s' %id for id in row_column)
    string=  (re.sub(r'\b[^a-zA-Z0-9]+\b', ' ', string))
    print(string)

正则表达式浅析

  • \b
    \b 匹配一个单词边界,也就是指单词和空格间的位置。例如,「er\b」可以匹配「never」中的「er」,但不能匹配「verb」中的「er」
  • '+'
    Causes the resulting RE to match 1 or more repetitions of the preceding RE. ab+ will match ‘a’ followed by any non-zero number of ‘b’s; it will not match just ‘a’.
  • '[]'
    里面可以存储一系列(正则)字符

更多参照:https://docs.python.org/3.1/library/re.html

相关文章

  • 关于Matrix Script的解法以及正则总结

    问题参照: https://www.hackerrank.com/challenges/matrix-script...

  • 2020-05-15

    正则匹配 单词 if (/\b(script|abc)\b/.test(password)) { }

  • 矩阵分解

    MF和正则化MF 参考python-matrix-factorization/ 正则化MF就是在MF的损失函数上加...

  • Golang 正则表达式

    参考golang之正则校验golang正则使用总结

  • PHP正则表达式的应用

    关于PCRE的介绍以及实现正则表达式功能的所有说明,都可以在官方手册中看到:正则表达式(兼容 Perl) 一、认识...

  • Matrix的使用总结

    心得: 1:如果已经有set之类的进行变换的方法了,那么后面不能再有set之类的进行变换的方法了,不然前面的set...

  • Android Matrix总结

    Matrix地址:https://github.com/Tencent/matrix[https://github...

  • pipeline报org.jenkinsci.plugins.s

    今天在pipeline中使用正则表达式,构建pipeline报如下错误: 原因:Jenkins的Script Se...

  • 关于正则表达式

    刷题的时候发现了关于正则表达式的题,完全忘干净了好好总结一下 正则基础知识点 元字符 元字符是构造正则表达式的一种...

  • 判断一个字符串是否是合法ip

    此题目可以使用正则表达式匹配或者字符串匹配方法进行判断 思路: python2.7正则表达式解法: python2...

网友评论

      本文标题:关于Matrix Script的解法以及正则总结

      本文链接:https://www.haomeiwen.com/subject/authfctx.html