Censor + general tips for better coding



I was doing the Censor exercise. I had forgotten quite a lot of important commands (like list-making, join text, etc), and thus being unaware of them, and being too proud (or foolish?) to click on the hint, I decided to work it out the long way.

There is no problem with the code, in terms of results. But is it really something that should be satisfying? Do you have any tips on how I should study built-in commands, functions, etc, and how to optimize the coding? I'm a total beginner, with a completely different background, so... layman terms please? :smiley:

def censor(text,word):
    dummy_text = ""
    dummy_list = []
    new_word_start = 0
    number_of_words = 0
    for i in range(len(text)):
        dummy_dummy = ""
        dummy_end = text+"#"
        if new_word_start < len(text):
            for j in range(new_word_start,len(text)):
                if text[j] != " ":
                    dummy_dummy += text[j]
                    new_word_start = j+1
                    number_of_words += 1
                    print new_word_start
    for i in range(len(dummy_list)):
        if dummy_list[i] == word:
            dummy_list[i] = "*" * len(word)
    dummy_list = dummy_list[0:number_of_words+1]
    final = ""
    for i in range(len(dummy_list)):
        final = final+" "+dummy_list[i]
    return final[1:]

print censor(raw_input("gimme text: "), raw_input("now gimme censor: "))


Is this how it would look in "real" code? No, it would be one line.

The exercise isn't about writing flawless code though, it's about coming up with and implementing an idea that has the desired effect.

I suggest focusing on the concepts, remember what is possible to do. If you need to do that something, google for the specifics.


Thanks for the reply :slight_smile: !

Would you happen to have more tips about studying, or perhaps a good article for the purpose?


You can check out sites like codewars.com and check.io for programming problems to solve. Make sure to compare your code to what other people did and pick up things you like. The main quality to judge by is how is something is to read and understand, and then of course how many operations are required to perform the task, that gets very important for large input data.

If you plan to get in touch with more tools than just Python, you might want to very briefly give linux a go. If you visit the command-line track here, you can try typing in:

echo "print('hello world')" > hello.py
python hello.py

Two things you can do with your code:

Run your code through a PEP8 checker, fix the things you don't disagree with. Consistent code style helps readability and reduces mistakes.

Your function is doing several unrelated steps, you could split it up into several functions, giving you something like:

def censor(text, word):
    words = split(text, ' ')
    censored_words = replace(words, word)
    return join(censored_words)

def split

def replace

def join

Which enables you to look at a function and understand it as a whole without much effort, making it much easier to reason about whether they are correct.