快受提取批合并字符串第三题

输入

将第二个字符串每隔四个字母插入,最后剩余的部分全都插入到最尾端。
输入str1 =“1 2 3 4 5 6 7 8 9”
输入str2=“a b c d e”

输出

1 2 3 4 a 5 6 7 8 b 9 c d e

python实现

import math

def mergeStr(line1,line2):
    line3=""
    length1=len(line1)
    length2=len(line2)
    if(length1==0):
        return line2
    if(length2==0):
        return line1
    line1Matrix=[]
    row=(math.ceil(length1/4))
    col=4
    for r in range(0,row):
        line1List=[]
        for c in range(0,col):
            for i in range(0,length1):
                if((r*4+c)==i):
                    line1List.append(line1[i])
        line1Matrix.append(line1List)
        
    for r in range(0,row):
        if r<(row-1):            
            line1Matrix[r].append(line2[r])
        if r==(row-1):
            for i in range(row-1,length2):
                line1Matrix[r].append(line2[i])
    for r in range(0,len(line1Matrix)):
        for c in range(0,len(line1Matrix[r])):
            line3=line3+line1Matrix[r][c]+" "
    return line3
if __name__=='__main__':
    line1=input().split()
    line2=input().split()
    line3=mergeStr(line1,line2)
    print(line3)

你可能感兴趣的:(python,算法,合并字符串)