def bad(string): bad_input = string.find('bad') if bad_input == -1: return string not_input = string.find('not') if not_input == -1: return string if bad_input < not_input: return string s1 = string[:not_input - 1] s2 = string[bad_input + 4:] elif len(s2) == 0: return s1 + ' good' + s2 elif len(s1) == 0: return 'good ' + s2 else: return s1 + ' good ' + s2 print(bad('This is not bad.')) assert bad('a') == 'a' assert bad('bad') == 'bad' assert bad('not') == 'not' assert bad('bad and not') == 'bad and not' print(bad('This is not so bad')) assert bad('This is not so bad') == 'This is good' assert bad('This is not hihihihihihihihihi bad') == 'This is good' print bad('This is not so bad and this is good') assert bad('This is not so bad and this is good') == 'This is good and this is good' print bad('Not bad') assert bad('Not bad') == 'good'
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question