Skip to content

Instantly share code, notes, and snippets.

@keskarnitish
Last active August 29, 2015 13:56
Show Gist options
  • Save keskarnitish/9105195 to your computer and use it in GitHub Desktop.
Save keskarnitish/9105195 to your computer and use it in GitHub Desktop.
A simple script to count number of words and lines in a document using only map and reduce functions (No Loops). Usage: cat foobar.txt | python LineWordCounter.py
# A simple script to count number of words and lines in a document using only map and reduce functions (No Loops)
# Usage: cat foobar.txt | python LineWordCounter.py
import sys
DataToRead = sys.stdin.read()
print "Document has " + str(sum(map(lambda x: x=='\n',DataToRead))) + " lines"
print "Document has " + str(len(map(len, DataToRead.split()))) + " words"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment