use python3 #!/usr/bin/python3 \'\'\'CPSC 254 - Assignment 09\'\'\' from test im
ID: 3758824 • Letter: U
Question
use python3
#!/usr/bin/python3
'''CPSC 254 - Assignment 09'''
from test import test
def not_bad(sentence):
'''Given a string s, 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. See examples in the
main function.
Keyword arguments:
sentence -- an arbitrary string
Returns: string
'''
# +++ADD code here+++
if __name__ == '__main__':
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")
Explanation / Answer
from test import test
def not_bad(sentence):
start=0;
end=0;
if 'not' in sentence:
start = sentence.find('not')
if 'bad' in sentence:
end = sentence.find('bad')
if (end > start):
end=end+3
return (sentence[:start]+'good'+sentence[end:])
else:
return sentence
if __name__ == '__main__':
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")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.