from string import lowercase
from itertools import groupby
lines = [line.strip() for line in open('diamond.txt')]
count=0
sumofwords=0
letters=[]
def numberoflines(lines):
return len(lines)
def numberofletters(letters):
count=0
for l in letters:
for ll in l:
count=count+len(ll)
return count
for s in lines:
count=count+1
counts = [(len(list(cpart))) for c,cpart in groupby(s) if c == ' ']
sumofwords=sumofwords+len(counts)+1
letters.append(s.split())
print "Number of lines in document ",numberoflines(lines),"\n"
print "Number of words in document ",sumofwords,"\n"
print "Number of letters in document ",numberofletters(letters)
This is the code, I need to print lines, characters and words in the text from diamond.txt.
The text contains this.
Diamond has remarkable optical characteristics. Because of its extremely rigid lattice, it can be contaminated by very few types of impurities, such as boron and nitrogen. Combined with wide transparency, this results in the clear, colorless appearance of most natural diamonds.
The lines and words are giving correct output, but number of letters in document coming as 238.
The correct output should be 279.
i think you're mixing things up the number of letters without whitespaces is in fact 238, while the answer you expect is the length of the whole text including whitespace which is 279 and by the way i'll prefer you use str.replace() to remove the whitespace in the text