Censor/Code bug


#1
def censor(string1,string2):
    k=""
    count=0
    word1=[]
    word2=[]
    last_word1=string1[-1]
    last_word2=string2[-1]
    #for first string
    for i in string1:
        count=count+1
        if i==" " and count!=len(string1):
            word1.append(k)
            k=""
        elif count==len(string1):
            k=k+last_word1
            word1.append(k)
        else:
            k=k+i

    k=""
    count=0
    #for second string
    for i in string2:
        count=count+1
        if i==" " and count!=len(string2):
            word2.append(k)
            k=""
        elif count==len(string2):
            k=k+last_word2
            word2.append(k)
        else:
            k=k+i


    #print "Original string number one"
    #print word1
    #print "String which will be used to replace"
    #print word2

    z=""
    str_to_replace=""
    for i in range(len(string2)):
        str_to_replace=str_to_replace+"*"

    #print str_to_replace

    for x in range(len(word1)):
        for y in range(len(word2)):
            if word1[x]==word2[y]:
                z=word2[y].replace(word1[x],str_to_replace)
                word1[x]=z


    #print "After replacing string one with number two --> "
    #print word1

    k=""
    j=0
    for i in range(len(word1)):
        k=k+word1[i]
        for j in range(1):
            k=k+" "
    return k
print censor("hey hey hey","hey")

my code is different to the one provided by codeacademy. Still, output of my result is same with the one thrown by codeacademy’s inbuilt code for censor lesson.

Can anyone explain? I am more than happy to be proven wrong.


#2

How did you manage such a lengthy code? An additional challenge would be to refactor.

well, your function produces the following output:

*** *** *** 

if we then replace the spaces with underscores to see the spaces:

***_***_***_

we can see the problem, you have a trailing space at the end of your sentence, which shouldn’t be


#4

Thanks stetim94 for your diagnosis. The solution provided by codeacademy solution provider is efficient. I was more concerned about getting the job done using loops instead of trying go simpler.
Thanks again


#5

I agree, you should try to implement whatever idea you do have, to convert your thoughts into code - doesn’t matter if it gets long.

I suspect you don’t need help getting rid of the last space, because if there’s something that your code does show, it is that you’re willing to look at what happens and solve problems

You should probably avoid the string replace method though, because that can do the whole thing all by itself! It might also not do exactly what you want either. To replace single characters I suggest putting the characters in a list! (because you cannot change strings, they are frozen)

Converting to and from list of chars:

>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> ''.join(['h', 'e', 'l', 'l', 'o'])
'hello'

Or perhaps you’d just replace the whole string by assigning to a new one (again, can’t change it, so maybe this is what you’re doing anyway)


#6

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