Censor - Cannot get spaces between words that I am replacing


#1

I am close to this solution. The line works correctly except for the fact that I cannot get the spaces in between words. The error message is:

Your function fails on censor("hey hey hey","hey"). It returns "******" when it should return " *** ***".

If I add a space to the "".join(s) statement, like " ".join(s) it will ad a space in between letters, not words. Adding a space in the concatenation will add an additional space to the end of the string which is also undesired and throws an error.

The print statements, as written print the following:
hey hey hey
hey
['hey', 'hey', 'hey']


None

I am getting the correct number of asterisks. I just need to find a way to add a space in between words. Can anyone help?


def censor(text, word):
    str1 = text.split()
    str2 = ""
    print text
    print word
    print str1
    for s in str1:
        if s == word:
            s = s.replace(s, "*" * len(s))
        str2 = str2 + "".join(s)
    print str2
    return str2
censor("hey hey hey", "hey")


#2

Should you be losing them in the first place? You can't go from having no separation between the words to knowing where to put the spaces. You lost that information. Try storing each word separately instead, and then join them on space.


#3

I understand what you mean. I would like to do that, however, I am not certain how to set a variable name that increments. Having one would easily allow me to set up what you are talking about. What I want, since the value of the word is already looped for s, change the value to s(1), s(2), ... s(n). Any thoughts on how best to achieve this? Can I use index since this is not a dictionary?


#4

Varible names don't incerement, they don't change, they are names, right?
If you mean some form of counter, then you could use an integer for that

The first thing your function does is put each word in a list.
You could simply overwrite the words that are supposed to be changed into something else. You could also add each word to a new list where you either changed it first, or not.

Also note that str.replace is a more general form of the function that you are implementing. It's silly to use it at all as it does all the things that your function should do (and if you're using it one would wonder what the rest of the code could possibly be doing)


#5

I like the idea of making a new list. Of your suggestions, that makes the most sense to me. I started down that path with str2. However, I had other errors I ran into that made me change my mind. I can change it back with some additional work.

Prior to adding the word to the new list (str2), I think that I still need to test to see if the value is equal to "word". If it is, change it, if not then just add it, as you further outline.

I use str.replace to loop through the letters in the word so that I get the exact number of asterisks I need, regardless of the word and its word length, which is a requirement of this exercise. I would not know what to replace that code with.

So I will:
1) take the old list and loop through each of the words in the list or lists.
2) If any of the words equal the word of interest, I will replace the word with the asterisks' needed.
3) I will then add the words to the new string as I loop.
4) return the new string.

Am I on the right track? Can you offer any other suggestions that would help me think through this problem?


#6

The length of the word is how many asterisks you need.

Carry out your algorithm manually to find out if it's on the right track. Consider whether the operations you carry out are supported by the data types that you use (if not, choose a different data type or do something else)


#7

I got it to work. Thank you for all your help.

I am a master tutor in mathematics and I appreciate that you did not do my coding for me. You allowed me to really learn. Thank you again.

def censor(text, word):
str1 = text.split()
str2 = []
print text
print word
print str1
print str2
for s in str1:
if s == word:
s = s.replace(s, "*" * len(s))
str2.append(s)
print " ".join(str2)
return " ".join(str2)
censor("hey hey hey", "hey")


#9