Python中根据子字符串的顺序识别子字符串并返回响应
我是 Python 的初学者,我正在 Google Code University 在线自学。字符串操作的练习之一如下:
# E. not_bad
# Given a string, find the first appearance of the
# substring 'not' and 'bad'. If the 'bad' follows
# the 'not', replace the whole 'not'...'bad' substring
# with 'good'.
# Return the resulting string.
# So 'This dinner is not that bad!' yields:
# This dinner is good!
def not_bad(s):
# +++your code here+++
return
我被困住了。我知道可以使用 ls = s.split(' ')
将其放入列表中,然后删除各种元素进行排序,但我认为这可能只是为自己创造额外的工作。本课程尚未涵盖 RegEx,因此解决方案不涉及 re。帮助?
这是我尝试过的,但它并不能在所有情况下正确给出输出:
def not_bad(s):
if s.find('not') != -1:
notindex = s.find('not')
if s.find('bad') != -1:
badindex = s.find('bad') + 3
if notindex > badindex:
removetext = s[notindex:badindex]
ns = s.replace(removetext, 'good')
else:
ns = s
else:
ns = s
else:
ns = s
return ns
这是输出,它在 1/4 的测试用例中工作:
not_bad
X got: 'This movie is not so bad' expected: 'This movie is good'
X got: 'This dinner is not that bad!' expected: 'This dinner is good!'
OK got: 'This tea is not hot' expected: 'This tea is not hot'
X got: "goodIgoodtgood'goodsgood goodbgoodagooddgood goodygoodegoodtgood
goodngoodogoodtgood" expected: "It's bad yet not"
测试用例:
print 'not_bad'
test(not_bad('This movie is not so bad'), 'This movie is good')
test(not_bad('This dinner is not that bad!'), 'This dinner is good!')
test(not_bad('This tea is not hot'), 'This tea is not hot')
test(not_bad("It's bad yet not"), "It's bad yet not")
更新:此代码解决了问题:
def not_bad(s):
notindex = s.find('not')
if notindex != -1:
if s.find('bad') != -1:
badindex = s.find('bad') + 3
if notindex < badindex:
removetext = s[notindex:badindex]
return s.replace(removetext, 'good')
return s
感谢大家帮助我找到解决方案(而不仅仅是给我答案)!我很感激!
I am a beginner in Python, I am teaching myself off of Google Code University online. One of the exercises in string manipulation is as follows:
# E. not_bad
# Given a string, find the first appearance of the
# substring 'not' and 'bad'. If the 'bad' follows
# the 'not', replace the whole 'not'...'bad' substring
# with 'good'.
# Return the resulting string.
# So 'This dinner is not that bad!' yields:
# This dinner is good!
def not_bad(s):
# +++your code here+++
return
I'm stuck. I know it could be put into a list using ls = s.split(' ')
and then sorted with various elements removed, but I think that is probably just creating extra work for myself. The lesson hasn't covered RegEx yet so the solution doesn't involve re. Help?
Here's what I tried, but it doesn't quite give the output correctly in all cases:
def not_bad(s):
if s.find('not') != -1:
notindex = s.find('not')
if s.find('bad') != -1:
badindex = s.find('bad') + 3
if notindex > badindex:
removetext = s[notindex:badindex]
ns = s.replace(removetext, 'good')
else:
ns = s
else:
ns = s
else:
ns = s
return ns
Here is the output, it worked in 1/4 of the test cases:
not_bad
X got: 'This movie is not so bad' expected: 'This movie is good'
X got: 'This dinner is not that bad!' expected: 'This dinner is good!'
OK got: 'This tea is not hot' expected: 'This tea is not hot'
X got: "goodIgoodtgood'goodsgood goodbgoodagooddgood goodygoodegoodtgood
goodngoodogoodtgood" expected: "It's bad yet not"
Test Cases:
print 'not_bad'
test(not_bad('This movie is not so bad'), 'This movie is good')
test(not_bad('This dinner is not that bad!'), 'This dinner is good!')
test(not_bad('This tea is not hot'), 'This tea is not hot')
test(not_bad("It's bad yet not"), "It's bad yet not")
UPDATE: This code solved the problem:
def not_bad(s):
notindex = s.find('not')
if notindex != -1:
if s.find('bad') != -1:
badindex = s.find('bad') + 3
if notindex < badindex:
removetext = s[notindex:badindex]
return s.replace(removetext, 'good')
return s
Thanks everyone for helping me discover the solution (and not just giving me the answer)! I appreciate it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,我认为是时候进行一次小审查了;-)
您的代码中有一个错误:
notindex > badindex
应改为notindex <坏索引。更改后的代码似乎工作正常。
另外,我对您的代码有一些评论:
例如,函数的头部可以替换为
return
inside of your function 多次。因此,您的代码尾部可能会显着减少:
最后我想表明您可以使用
split
解决此问题。但这似乎并不是更好的解决方案。Well, I think that it is time to make a small review ;-)
There is an error in your code:
notindex > badindex
should be changed intonotindex < badindex
. The changed code seems to work fine.Also I have some remarks about your code:
For example, the head of your function could be replaced by
return
inside of your function several times.As a result tail of your code could be significantly reduced:
Finally i want to indicate that you can solve this problem using
split
. But it does not seem to be better solution.像这样分解:
Break it down like this:
既然你正在尝试学习,我不想给你答案,但我会首先在 python 文档中查找一些字符串函数,包括替换和索引。
另外,如果您有一个好的 IDE,它可以帮助您显示哪些方法附加到对象,甚至自动显示这些方法的帮助字符串。我倾向于使用 Eclipse 来处理大型项目,而使用轻量级的 Spyder 来处理小型项目
Since you are trying to learn, I don't want to hand you the answer, but I would start by looking in the python documentation for some of the string functions including replace and index.
Also, if you have a good IDE it can help by showing you what methods are attached to an object and even automatically displaying the help string for those methods. I tend to use Eclipse for large projects and the lighter weight Spyder for small projects
http://docs.python.org/library/stdtypes.html#string-methods
我怀疑他们希望您使用 string.find 来定位各种子字符串:
因为您正在尝试自学(荣誉,顺便说一句:)我不会发布完整的解决方案,但还要注意您可以使用索引来获取子字符串:
希望这足以让您开始!如果没有,请在这里评论,我可以发布更多内容。 :)
http://docs.python.org/library/stdtypes.html#string-methods
I suspect that they're wanting you to use string.find to locate the various substrings:
Since you're trying to teach yourself (kudos, BTW :) I won't post a complete solution, but also note that you can use indexing to get substrings:
Hope that's enough to get you started! If not, just comment here and I can post more. :)