It's really awkward the way you're using Counter here... you're making new
instances in every lambda (which is not great for memory usage), and then
not actually using the Counter functionality:
return sum(1 for _ in filter(lambda x: Counter(word) == Counter(x.strip()),
fileContent))
(the whole point of the Counter() is to get sums, you don't need to do any
of this !!)
I'm not sure that they cared about how you used file.readlines(), I think
the memory comment was a hint about instantiating Counter()s
anyhow, all of this can be much much simpler:
"""
sortedword = sorted(inputWord) # an array of letters in the word, in
alphabetical order
count = 0
with open(filename) as f:
for word in f.read().split(' '): # iterate over every word in the file
if sorted(word) == sortedword:
count +=1
print count
"""
which could in turn probably be written as a one liner.
So even though you cleaned the code up a bit, it's still quite a bit more
complicated then it needs to be, which makes it seem like your fundamentals
are not great either!
On Tue, Jun 6, 2017 at 2:31 AM, Peter Otten <[email protected]> wrote:
> 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.
>
> _______________________________________________
> Tutor maillist - [email protected]
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor