鱼C论坛

 找回密码
 立即注册
查看: 756|回复: 6

救救孩子

[复制链接]
发表于 2018-9-30 06:54:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Complete the swapEnds() function, which takes three arguments: a string of 3 or more characters, plus two positive integers representing index positions within that string. The function returns a new string according to the following process:
• If either index value is invalid (greater than or equal to the length of the string), or if the index values are equal to one another, return the original string
• If the first index is greater than the second index, swap their values (see below for a tip on how to do this easily)
• Finally, return a new string that consists of the following sections of the original string, in this order:
1. All of the characters from the original string, from the character after the second index through the end 2. All of the characters from the first index through the second index
3. All of the characters from the beginning of the string up to (but not including) the first index
For example, consider the string “sesquipedalian”, with a first index value of 10 and a second index value of 5. The first index is greater than the second index, so we exchange their values to get 5 and 10. We now have three substrings:
• “ian” (the characters from index 11 through the end of the string)
• “ipedal” (the characters from index 5 through index 10)
• “sesqu” (the characters from the beginning of the string up to, but not including, index 5)
Combining these strings using the + operator, we get a final result of “ianipedalsesqu”.
Hint: Python provides a simple way to exchange the values of two variables using something called multiple assignment:
a, b = b, a
Examples:
Function Call
Return Value
swapEnds("space:  the final frontier", 5, 14)
l frontier:  the finaspace
swapEnds("these are the voyages", 12, 7)
voyagesre thethese a(with a leading space)
swapEnds("where no one has gone before", 8, 42)
where no one has gone before(no change)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-30 09:14:00 | 显示全部楼层
def swapEnds(s,a,b):
    if a==b or a>=len(s) or b>=len(s) :
        return s
    else:
        if a>b:
            a,b=b,a
        return s[b+1:]+s[a:b+1]+s[:a]
例子第一个错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-30 09:20:42 | 显示全部楼层

抱紧大佬的大腿
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-30 09:32:46 | 显示全部楼层

大佬,我还有一题不会在另一个贴子上,能不能麻烦您帮我看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-30 09:52:24 | 显示全部楼层
Crystal是仙女 发表于 2018-9-30 09:32
大佬,我还有一题不会在另一个贴子上,能不能麻烦您帮我看看

嗯,你确认没问题把最佳答案设置上
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-30 10:29:07 | 显示全部楼层
塔利班 发表于 2018-9-30 09:52
嗯,你确认没问题把最佳答案设置上

大佬,我得学多久才能像你一样?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-30 10:40:44 | 显示全部楼层
Crystal是仙女 发表于 2018-9-30 10:29
大佬,我得学多久才能像你一样?

你一直学,不到一个月应该就可以,不要像我三心二意的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-7 12:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表