Composing squared strings

43 篇文章 1 订阅
2 篇文章 0 订阅

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:

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的第一行的第一个字符。

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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜欢ctrl的cxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值