Comment

Comment by lqdc13

Basically this whole thing is crazy.

First, this can be done with one line in Python.

Second, it's almost a page of C++.

Third: if done in a 10 lines in python it is around 30% faster than the C++ version and 2x faster than the perl version.

Here is my reference Python version that also counts the words in the same non-code golf kind of way:

    import sys
    from collections import defaultdict

    def wc(path):
        d = defaultdict(int)
        with open(path) as f:
            for line in f:
                for w in line.split():
                    d[w] += 1
        return sum(d.values())

    if __name__=='__main__':
        path = sys.argv[1]
        print(path, ':', wc(path))

EDIT: They are counting new lines correctly.
parent lqdc13 11 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton