can you help me solve practice 5.2 editing guide in python data structures pls | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can you help me solve practice 5.2 editing guide in python data structures pls

27th Feb 2021, 3:32 AM
Lion me
Lion me - avatar
12 Answers
+ 3
Lion Fret hi, Did the solution provided by XXX worked for you. I did something quite similar to your code and tried the XXX suggestion but it still doesn't work. There are 5 tests case and the 4th one is failing. I can't check what's kind of error it is as the case stay locked/hidden. I can't figure out what could go wrong as every input is of type string , this couldn't be a data type error, so what else could cause an error ? Thanks for reading. Anyway @XXX I'm not pro either and it seems I can Access at least one of the practice by course but as soon as I choose one to do the others appears locked...
15th May 2021, 12:01 AM
EmmanueLZ.
EmmanueLZ. - avatar
+ 2
text = "I weigh 80 kilograms. He weighs 65 kilograms." a1=input() a2=input() b=text.replace(a1,a2) c=b.count(a2) print(c) print(b)#pls check my code
27th Feb 2021, 4:17 AM
Lion me
Lion me - avatar
+ 2
Well, it works now. Originally I added a if wordToFind in text: #the logic here I removed it and reorder the count function as XXX suggested and it worked.(Though I made some tests in the playground and it worked whatever was the count on => wordToFind before the text replacement or replacer after text replacement but without the if statement). So finally I guess the 4th test case was that the wordToFind input was not in the text and the count of occurence was 0. Thanks.
15th May 2021, 12:23 AM
EmmanueLZ.
EmmanueLZ. - avatar
+ 1
It should work now. Although, if it doesn't work, that would be because the replacement string is already present in the original string. The fix would be simply - put the line c = text.count(a1) before the 3rd line and remove the 4th line, that is c = b.count..... That way, you will be counting the number of occurences pf text to be replaced before the actual replacement takes place
27th Feb 2021, 10:53 AM
XXX
XXX - avatar
0
That practise problem is for pro users only. Please copy the question here and also, post your attempt at solving it.
27th Feb 2021, 3:38 AM
XXX
XXX - avatar
0
but i am pro users
27th Feb 2021, 3:42 AM
Lion me
Lion me - avatar
0
Lion Fret I am not. And that is why, I can't see the question. That's why I am asking you to copy the question here and also post your attempt at solving it.
27th Feb 2021, 3:43 AM
XXX
XXX - avatar
0
You are making a text editor and need to implement find/replace functionality. The code declares a text string. You need to take two string inputs, which represent the substring to find and what to replace it with in the text. Your program needs to output the number of replacements made, along with the new text. For example, for the given text "I weigh 80 kilograms. He weighs 65 kilograms.": Sample Input kilograms kg Sample Outputu 2 I weigh 80 kg. He weighs 65 kg. The program replaced 2 occurrences of the word kilograms with kg.
27th Feb 2021, 3:45 AM
Lion me
Lion me - avatar
27th Feb 2021, 3:53 AM
XXX
XXX - avatar
0
thanks
27th Feb 2021, 1:34 PM
Lion me
Lion me - avatar
0
For me this code worked: text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." enter = input() repl = input() if enter in text: new_text = text.replace(enter,repl) print(new_text.count(repl)) print(new_text) else: print("0") print(text)
18th Jan 2022, 2:21 PM
Martin Valdivia
0
Editing Guide text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." substituted= input() replace_text= input() the_replacement= text.replace(substituted,replace_text) print(text.count(substituted)) print(the_replacement)
15th May 2023, 5:39 PM
AMINE MOUTAOUAKKIL