前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Composing squared strings

Composing squared strings

作者头像
喜欢ctrl的cxk
发布2019-11-08 21:04:32
5730
发布2019-11-08 21:04:32
举报
文章被收录于专栏:Don的成长史Don的成长史

版权声明:本文为博主原创文章,遵循 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 = "abcd\nefgh\nijkl\nmnop" s2 = "qrst\nuvwx\nyz12\n3456"

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:

代码语言:javascript
复制
compose(s1, s2) -> "a3456\nefyz1\nijkuv\nmnopq"
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的第一行的第一个字符。

代码语言:javascript
复制
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)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-01-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Instructions:
  • Example:
  • Solution:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档