Merged String Checker Codewars 答题

在一次面试中,你被要求编写一个算法,检查字符串s是否能由part1和part2按顺序拼接而成。例如,'codewars'可以由'cdw'和'oears'按顺序组成。你需要编写一个函数,根据输入的s, part1, part2判断条件是否满足。你首先考虑使用正则表达式来解决这个问题。" 116030434,10544612,matplotlib.pyplot.plot详解与示例,"['Python', '数据可视化', 'matplotlib', '绘图']

At a job interview, you are challenged to write an algorithm to check if a given string, s, can be formed from two other strings, part1 and part2.

The restriction is that the characters in part1 and part2 should be in the same order as in s.

The interviewer gives you the following example and tells you to figure out the rest from the given test cases.

For example:

‘codewars’ is a merge from ‘cdw’ and ‘oears’:

s:  c o d e w a r s   = codewars

part1: c d w = cdw
part2: o e a r s = oears

字符串融合,编写一个函数,给定三个参数,s,part1,part2
判断s是不是有part1和part2组合而成的,并且part1和part2的字母顺序应该和S里的字母顺序一致。
函数嵌套可以解决这个问题,但是我第一时间想到的是正则匹配。

import re

def is_merge(s, part1, part2):
    if s == part1 + part2:
        return True
    if len(part1)+len(part2) != len(s) or sorted(list(s)) != sorted(list(part1+part2)):
        return False
    list1 = []
    list2 = []
    for a in part1:
        if a.isalnum():
            list1.append(a)
        else:
            a = '\\' + a
            list1.append(a)
    for b in part2:
        if b.isalnum():
            list2.append(b)
        else:
            b = '\\' + b
            list2.append(b)
    getRegex1 = re.compile('.*'+'.*'.join(list1)+'.*')
    getRegex2 = re.compile('.*'+'.*'.join(list2)+'.*')
    return True if getRegex1.match(s) and getRegex2.match(s) else False
`merged_cells`通常用于处理Excel表格中的合并单元格,在Python的`openpyxl`库中有所涉及。 ### 含义 `merged_cells`用于表示Excel工作表中的合并单元格信息。`sheet.merged_cells.ranges`变量是`MergedCellRange`的集合,`merged_cells`返回的是一个列表,每一个元素是合并单元格的位置信息的数组,数组包含四个元素(起始行,结束行,起始列,结束列),结束的行和列都加了1 [^3]。 ### 使用场景 - **获取合并单元格属性**:可以使用`merged_cells.range`接口回传包含所有合并单元格的属性集合,包括`min_row/max_row`(单元格最小最大行)、`min_col/max_col`(单元格最小最大列)等 [^1]。 - **找出合并单元格索引信息**:通过`sheet.merged_cells.ranges`找出所有合并单元格的索引信息,例如`mc_range_list = [str(item) for item in sheet.merged_cells.ranges]`可以将合并单元格的索引信息以字符串形式存储在列表中 [^3]。 - **处理读取合并单元格报错**:当使用`openpyxl`读取合并单元格使用`merged_cell_ranges`函数报错时,可使用`sheet.merged_cells.ranges`替代 [^2]。 - **合并单元格操作**:在Python中使用`openpyxl`对Excel文件进行操作时,可利用`merged_cells`信息进行合并单元格的操作,如在`merge_cells_in_excel`函数中,根据合并索引表对指定范围的单元格进行合并操作 [^4]。 ### 代码示例 ```python import openpyxl # 加载 Excel 文件 workbook = openpyxl.load_workbook('example.xlsx') # 获取活动工作表 sheet = workbook.active # 获取合并单元格范围列表 mc_range_list = [str(item) for item in sheet.merged_cells.ranges] print(mc_range_list) # 遍历合并单元格范围列表 for item in sheet.merged_cells.ranges: print(f"起始行: {item.min_row}, 结束行: {item.max_row}, 起始列: {item.min_col}, 结束列: {item.max_col}") ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值