[Tutor] Python - help with something most essential
Schtvveer Schvrveve
sch4444 at gmail.com
Mon Jun 5 10:36:48 EDT 2017
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.
Thank you in advance
More information about the Tutor
mailing list