Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Your code assumes there is at least one newline in each 64K chunk, and assumes there are no words past the final newline.

It will fail on "abc" and give the wrong answer for "abc\ndef".

I prefer rpartition over rsplit to handle first case, and the loop-and-a-half construct instead of the while+walrus operator to handle the second, as in this modified version of your code:

    remaining = ""
    c=Counter( )
    while True:
        chunk = sys.stdin.read(64 * 1024)
        if not chunk:
            if not remaining:
                break
            pre = post = ""
        else:
            pre, mid, post = chunk.lower().rpartition("\n")
        c.update((remaining + pre).split())
        remaining = post


Indeed, rpartition is better suited for this task.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: