by eesmith · 4y ago · 0 comments
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