[Tutor] Python - help with something most essential

Peter Otten __peter__ at web.de
Tue Jun 6 02:31:03 EDT 2017


Schtvveer Schvrveve wrote:

> I need someone's help. I am not proficient in Python and I wish to
> understand something. I was in a job pre-screening process where I was
> asked to solve a simple problem.
> 
> The problem was supposed to be solved in Python and it was supposed to
> take two arguments: filename and word. The program reads the file which is
> a .txt file containing a bunch of words and counts how many of those words
> in the file are anagrams of the argument.
> 
> First I concocted this solution:
> 
> import sys
> from collections import Counter
> 
> def main(args):
>     filename = args[1]
>     word = args[2]
>     print countAnagrams(word, filename)
> 
> def countAnagrams(word, filename):
> 
>     fileContent = readFile(filename)
> 
>     counter = Counter(word)
>     num_of_anagrams = 0
> 
>     for i in range(0, len(fileContent)):
>         if counter == Counter(fileContent[i]):
>             num_of_anagrams += 1
> 
>     return num_of_anagrams
> 
> def readFile(filename):
> 
>     with open(filename) as f:
>         content = f.readlines()
> 
>     content = [x.strip() for x in content]
> 
>     return content
> 
> if __name__ == '__main__':
>     main(sys.argv)
> 
> Very quickly I received this comment:
> 
> "Can you adjust your solution a bit so you less loops (as little as
> possible) and also reduce the memory usage footprint of you program?"
> 
> I tried to rework the methods into this:
> 
> def countAnagrams(word, filename):
> 
>     fileContent = readFile(filename)
> 
>     return sum(1 for _ in filter(lambda x: Counter(word) ==
> Counter(x.strip()), fileContent))
> 
> def readFile(filename):
> 
>     with open(filename) as f:
>         content = f.readlines()
> 
>     return content
> 
> And I was rejected. I just wish to understand what I could have done for
> this to be better?
> 
> I am a Python beginner, so I'm sure there are things I don't know, but I
> was a bit surprised at the abruptness of the rejection and I'm worried I'm
> doing something profoundly wrong.

for i in range(0, len(stuff)):
    ...

instead of 

for item in stuff:
    ...

and

content = file.readlines() # read the whole file into memory
process(content)

are pretty much the most obvious indicators that you are a total newbie in 
Python. Looks like they weren't willing to give you the time to iron that 
out on the job even though you knew about lambda, Counter, list 
comprehensions and generator expressions which are not newbie stuff. 

When upon their hint you did not address the root cause of the unbounded 
memory consumption they might have come to the conclusion that you were 
reproducing snippets you picked up somewhere and thus were cheating.



More information about the Tutor mailing list