Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with test
#1
Hi, i'm currently following a google online course on python, and I am having truble solving a programming problem. I am studying strings at the moment. I am at the beginning of it, it is a very easy problem and probably a very dumb mistake, but I am really having trouble here. I tried rewatching the lessons and still couldn't figure it out.
It is my first time using a forum, I will try to be as clear as possible to explain my problem, hoping i'm using it the way it is supposed to be used.

The question reads:

The replace_ending function replaces the old string in a sentence with the new string, but only if the sentence ends with the old string. If there is more than one occurrence of the old string in the sentence, only the one at the end is replaced, not all of them. For example, replace_ending("abcabc", "abc", "xyz") should return abcxyz, not xyzxyz or xyzabc. The string comparison is case-sensitive, so replace_ending("abcabc", "ABC", "xyz") should return abcabc (no changes made).

This is what I have tried:


def replace_ending(sentence, old, new):
	# Check if the old string is at the end of the sentence 
	if old in sentence:
		# Using i as the slicing index, combine the part
		# of the sentence up to the matched string at the 
		# end with the new string
		i = len(old)
		new_sentence = sentence [:-i] + "{new}".format(new = new)
		return new_sentence

	# Return the original sentence if there is no match 
	return sentence
print(replace_ending("It's raining cats and cats", "cats", "dogs"))
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts"))
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april"))
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April"))
# Should display "The weather is nice in April"

And when I run it i get:

It's raining cats and dogs
She sells seashells by thedonuts
The weather is nice in May
The weather is nice in April

I am not understanding why the second line is wrong. I state "if new not in sentence" ( i tried if old in sentence before, but it didn't work for some reason)
but in that case, in which new is in sentence, it still changes it. Can someone help me understand what is wrong?
Thanks in advance

Attached Files

Thumbnail(s)
   
Reply
#2
When posting code, you need to post it using the python tags - the blue and yellow icon on the edit box. Otherwise you lose the proper indentation.

So, hard to read without the indentation, but I do not see where you return new_sentence. I don't think you posted the current code.
Jacopo_98 likes this post
Reply
#3
Have a look at this. In particular .endswith() and .rstrip().

Your function could be made to take two arguments: def replace_ending(sentence, replace): the replace being a tuple that holds old and new.

Do you know how to unpack a Tuple using the index number?
Jacopo_98 likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
(Feb-07-2023, 05:04 PM)Jacopo_98 Wrote: Hi, i'm currently following a google online course on python, and I am having truble solving a programming problem. I am studying strings at the moment. I am at the beginning of it, it is a very easy problem and probably a very dumb mistake, but I am really having trouble here. I tried rewatching the lessons and still couldn't figure it out.
It is my first time using a forum, I will try to be as clear as possible to explain my problem, hoping i'm using it the way it is supposed to be used.

The question reads:

The replace_ending function replaces the old string in a sentence with the new string, but only if the sentence ends with the old string. If there is more than one occurrence of the old string in the sentence, only the one at the end is replaced, not all of them. For example, replace_ending("abcabc", "abc", "xyz") should return abcxyz, not xyzxyz or xyzabc. The string comparison is case-sensitive, so replace_ending("abcabc", "ABC", "xyz") should return abcabc (no changes made).

This is what I have tried:


def replace_ending(sentence, old, new):
	# Check if the old string is at the end of the sentence 
	if old in sentence:
		# Using i as the slicing index, combine the part
		# of the sentence up to the matched string at the 
		# end with the new string
		i = len(old)
		new_sentence = sentence [:-i] + "{new}".format(new = new)
		return new_sentence

	# Return the original sentence if there is no match 
	return sentence
print(replace_ending("It's raining cats and cats", "cats", "dogs"))
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts"))
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april"))
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April"))
# Should display "The weather is nice in April"

And when I run it i get:

It's raining cats and dogs
She sells seashells by thedonuts
The weather is nice in May
The weather is nice in April

I am not understanding why the second line is wrong. I state "if new not in sentence" ( i tried if old in sentence before, but it didn't work for some reason)
but in that case, in which new is in sentence, it still changes it. Can someone help me understand what is wrong?
Thanks in advance
You were right, it was the wrong code, now i posted it with the indentation you said, sorry for the mistake
Reply
#5
(Feb-07-2023, 05:38 PM)rob101 Wrote: Have a look at this. In particular .endswith() and .rstrip().

Your function could be made to take two arguments: def replace_ending(sentence, replace): the replace being a tuple that holds old and new.

Do you know how to unpack a Tuple using the index number?

Hi, thanks for the web page, it will be very useful in the future, but I do not think that this is the solution, also, I have to use the function they gave me, I am not supposed to change it
Reply
#6
What do you mean by " I have to use the function they gave me, I am not supposed to change it"? You wrote the "replace_ending()" function in your original post. You can change that code, correct?

replace_ending() has a bug. The function is supposed to replace old with new, but only if old is at the end of the sentence. Your function does not test if old is at the end of the sentence. It only tests if old is in the sentence. That is why you have a problem with:
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts"))
There should be no substitution because "seashells" is not the last word in the sentence.

Your code creates a funny sentence ending with "thedonuts" because "seashells" has 9 characters and "seashore" has 8. Your function blindly removes the last 9 characters from the sentence; " seashore" (notice the space at the front) and appends "donuts".

Fix your function so it is correct, and it will pass all the tests.
Reply
#7
(Feb-07-2023, 07:18 PM)Jacopo_98 Wrote: Hi, thanks for the web page, it will be very useful in the future, but I do not think that this is the solution, also, I have to use the function they gave me, I am not supposed to change it

Fair enough, but it's far less complicated if you use the builtins; there's no for: loop, just is simple if: branch, with one string concatenation.
Jacopo_98 likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#8
(Feb-07-2023, 07:22 PM)deanhystad Wrote: What do you mean by " I have to use the function they gave me, I am not supposed to change it"? You wrote the "replace_ending()" function in your original post. You can change that code, correct?

replace_ending() has a bug. The function is supposed to replace old with new, but only if old is at the end of the sentence. Your function does not test if old is at the end of the sentence. It only tests if old is in the sentence. That is why you have a problem with:
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts"))
There should be no substitution because "seashells" is not the last word in the sentence.

Your code creates a funny sentence ending with "thedonuts" because "seashells" has 9 characters and "seashore" has 8. Your function blindly removes the last 9 characters from the sentence; " seashore" (notice the space at the front) and appends "donuts".

Fix your function so it is correct, and it will pass all the tests.

thank you very much, this explanation solved it. By i couldn't change it i meant that it was "pre written" I was changing/making parts of the code to make it work but i was not supposed to change that part
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020