Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I can't figure out the bug in this function replace_first. The function list is here: https://ecpyfac.ecornell.com/docs/introcs/strings.html This function has an error in it and you

I can't figure out the bug in this function replace_first. The function list is here: https://ecpyfac.ecornell.com/docs/introcs/strings.html

image text in transcribed
This function has an error in it and you test it to find the error. Collapse E3: Debugging Discovered Errors 7 8 Author: Walker M. White 9 Date : February 20, 2019 25. Isolate and Fix the Second Bug 10 11 import introcs Fortunately, the watches are still in the function |replace_first , so you can use them to 12 help you with this new bug. Again, look at the value in each variable. Is before what you 13 expect? How about after ? Which ever one was wrong first must be the source of the 14 v def replace_first (word, a, b) : 15 error. It is now time to fix the second bug. 16 Returns: a copy of word with the FIRST instance of a replaced by b Once you think you have removed the bug, run the test script again. If you did it correctly, 17 the error message should disappear. 18 Example: replace_first ( 'crane' , 'a' , 'o' ) returns 'crone' 19 Example: replace_first('poll', 'l', 'o') returns 'pool' Check the Bug Fix 20 Example: replace_first ( 'crane' , 'cr', 'b') returns 'bane' You may run this test multiple times. 21 22 Parameter word: The string to copy and replace Check It! 23 Precondition: word is a string 24 LAST RUN on 12/18/2020, 4:45:09 PM 25 Parameter a: The substring to find in word XJ Function ' replace_first' fails on the test ('crane', 'a', '0' ). 26 Precondition: a is a valid substring of word 27 28 Parameter b: The substring to use in place of a 29 Precondition: b is a string 30 31 pos = introcs . rfind_str (word, a) 32 print (pos) 33 before = word [pos: ] 34 print (before) 35 after = word [pos :+1] 36 print (after) 37 result = before+b+after 38 print (result) 39 return result

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions