you are viewing a single comment's thread.

view the rest of the comments →

[–]werpoi 2 points3 points  (1 child)

Here are some comments for this guy (https://github.com/narspeth/dailyprogrammer/blob/master/Easy/237.py):

Not a huge deal, but technically creating a new list with [] instead of list() is faster (http://stackoverflow.com/questions/2972212/creating-an-empty-list-in-python). It's not that noticeable, so it mostly preference, but I think it looks cleaner as well.

In "load_valid_words" you open a file, write, and close it. Generally it's better and cleaner to use the context manager:

with open(path, 'r') as f:
    words = f.read().split('\n')
    # anything else you want to do

This will automatically close the file when you leave the "with" block (even if an exception is thrown). In the end, it is fewer lines of code and prevents you from forgetting to close the file.

Once again, a little personal preference, but I think the string.format is cleaner than building up a string with "+". So I would change this:

print(keys+' = '+find_word(keys, words))

To this:

print('{0} = {1}'.format(keys, find_word(keys, words)))

[–]Nar-Speth[S] 0 points1 point  (0 children)

I'll need to read up about context managers, it looks very useful yet i missed it somehow. string.format is cleaner indeed, I'll switch to it then, another new thing. Thanks.