Censor problem?


#1

hi. I don’t know what to think about my code for the censor task. I tried it and tested it with different strings and the end result is as what I want but the site platform keeps telling me it doesn’t work. can someone explain what i did wrong. If it is possible just a little hint so I can improve the code itself. Thank you.
The code:

def censor(text,word):
  new_string = ""  
  for a in text.split():
    if a != word:
      new_string = new_string + " " + a
    else:
      new_string = new_string + " " + "*" * len(word)
  return new_string   

print censor("this hack is wack hack","hack")
print censor("hey hey hey","hey")

#2

you have a leading space, which isn’t allowed.

we can see this if we replace spaces with underscores:

print censor("hey hey hey","hey")
# output:
_***_***_***

#3

so all i had to do is reverse the order from <<< new_string = new_string + " " + a >>> to <<< new_string = new_string + a + " " >>> ( # and the same for the “*” * len(word) )… hmmm but now if the censored word is the last i have a leading space at the end… interesting :smile:


#4

no, because that would give a trailing space:

print censor("hey hey hey","hey")
# output:
***_***_***_

which still isn’t allowed.


#5

I just found the solution and the site is ok with it. Thank you a lot for helping me. Have a nice day forward
Here is the updated code:

def censor(text,word):
  new_string = ""  
  for a in text.split():
    if a != word:
      new_string = new_string + a + " "
    else:
      new_string = new_string + "*" * len(word) + " "
  return new_string[:len(new_string)-1]  

print censor("this hack is wack hack","hack")
print censor("hey hey hey","hey")

#6

but are you happy with the solution?


#7

:joy::joy: for now I am pretty happy. I know that there is another method,better than mine.Should i try to find it now or later after I learn more lines of code in Python?


#8

That is up to you. I think there are gains to be made with what you know, but you can do it later


#9

Then up I go and try to rewrite it. wish me luck…


#10

Consider what data type to use to store censored string (new_string), just because the final results needs to be a string, doesn’t mean it needs to be the whole time

and things like text.split(), do you want to split the string each iteration of the loop?

The core of your logic is fine. Using a for loop, determining wether to censor or just append a, that is all fine.

a could have a more logic variable name like current_word or something.


#11

Here is another method i found… but it kinda looks the same?

def censor2(text,word):
  text_list=list(text.split())
  for current_word in text_list:
    if current_word == word:
      text_list[text_list.index(current_word)] = "*" * len(word)
  return " ".join(text_list)  

#12

yes, but it works differently. It updates words in the list which require censoring.

while censor1 was creating a new string to which it added everything (both censored and uncensored).

you could have used lists for censor1 as well.


#13

I think that if i stay long enough I can resolve it in a single line of code :joy::joy:… This is why i just fell in love with coding. One problem -> Infinite solutions


#14

solving censor in a single line is certainly possible.

not all solutions are equally good. Some solutions are better then others. But that is always a tricky thing, given a solution can be better performance wise, readability or maintainability.


#15

If i don’t bother I have another question about different line of codes.
How do you know that a code is better than another code, beside the lenght ? It involves things from computer science like memory allocation, repetition of tasks,etc?


#16

length isn’t even a good measurement. I recently saw a piece of code from the Linux kernel, it was really difficult to read and understand, and pretty long. Could the code have been shorter? Yes, absolutely. But speed was critical here, so the longer more complicated was justified given speed was vital. The code was optimized for speed.

in other cases, where speed is not so critical, you might go for more readable code. Speed optimization would cost time, while the gain is minimal.


Functions should only do a single task, so they can be unit tested as well as integrity test. So that individual components as well as the program as whole are tested

you saw this design in taking a vacation for example. (part of the python course you have completed)

To gain this understanding, a lot of practice is required. I haven’t mastered this yet either.


#17

I understand. Well… Thank you for your help here today.


#18

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.