Composing squared strings

  • 2019 年 11 月 8 日
  • 筆記

版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接:https://blog.csdn.net/weixin_42449444/article/details/86373282

Instructions:

A squared string is a string of n lines, each substring being n characters long. We are given two n-squared strings.

Example:

s1 = "abcdnefghnijklnmnop" s2 = "qrstnuvwxnyz12n3456"

Let us build a new string strng of size (n + 1) x n in the following way:

  • The first line of strng has the first char of the first line of s1 plus the chars of the last line of s2.
  • The second line of strng has the first two chars of the second line of s1 plus the chars of the penultimate line of s2 except the last char.
  • and so on until the nth line of strng has the n chars of the nth line of s1 plus the first char of the first line of s2.

Calling this function compose(s1, s2) we have:

compose(s1, s2) -> "a3456nefyz1nijkuvnmnopq"  or printed:  abcd    qrst  -->  a3456  efgh    uvwx       efyz1  ijkl    yz12       ijkuv  mnop    3456       mnopq

Solution:

額,我真的是個菜雞,刷這道7kyu的題很吃力呀,題目大意就是給定一個字符串把它組成一個方形字符串。第一行string有s1的第一行加s2的最後一行的字符的第一個字符。第二行string有s1第二行的前兩個字符加上s2的倒數第二行的字符,除了最後一個字符。依此類推,直到第n行string具有s1的第n行的n個字符加上s2的第一行的第一個字符。

def compose(s1, s2):      s1 = s1.split("n")      s2 = s2.split("n")[::-1]      l = []      for i in range(len(s1)):          l.append(s1[i][:i+1] + s2[i][:(len(s1)-i)])      return "n".join(l)