语法练习:combo_string

这是一个关于Python编程的语法练习,题目要求根据两个不同长度的字符串a和b,创建一个新字符串,格式为short+long+short。文章展示了示例解答及预期运行结果,所有测试用例均正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

语法练习:combo_string

题目:combo_string

Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on the outside and the longer string on the inside. The strings will not be the same length, but they may be empty (length 0).

combo_string(‘Hello’, ‘hi’) → ‘hiHellohi’
combo_string(‘hi’, ‘Hello’) → ‘hiHellohi’
combo_string(‘aaa’, ‘b’) → ‘baaab’

我的解答:

def combo_string(a, b):
  if len(a) > len(b):
    return b + a + b
  else:
    return a + b + a

Expected Run
combo_string(‘Hello’, ‘hi’) → ‘hiHellohi’ ‘hiHellohi’ OK
combo_string(‘hi’, ‘Hello’) → ‘hiHellohi’ ‘hiHellohi’ OK
combo_string(‘aaa’, ‘b’) → ‘baaab’ ‘baaab’ OK
combo_string(‘b’, ‘aaa’) → ‘baaab’ ‘baaab’ OK
combo_string(‘aaa’, ‘’) → ‘aaa’ ‘aaa’ OK
combo_string(‘’, ‘bb’) → ‘bb’ ‘bb’ OK
combo_string(‘aaa’, ‘1234’) → ‘aaa1234aaa’ ‘aaa1234aaa’ OK
combo_string(‘aaa’, ‘bb’) → ‘bbaaabb’ ‘bbaaabb’ OK
combo_string(‘a’, ‘bb’) → ‘abba’ ‘abba’ OK
combo_string(‘bb’, ‘a’) → ‘abba’ ‘abba’ OK
combo_string(‘xyz’, ‘ab’) → ‘abxyzab’ ‘abxyzab’ OK
other tests
OK

All Correct

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值