Solution given in assignment given in week 5 seems to produce wrong answer
no_of_words = 0
with open(‘sample text.txt’, ‘r’) as file:
for line in file.readlines():
no_of_words += line.len()
produces 467 instead of 73 words
with open(input_file,“r”) as f:
for i in range(length):
for t in s:
new_file = " ".join(words)
Please check this:
For further queries, please write there.
I think answer should be 79 instead of 467. Here is my code:
sample_text = "Quantum entanglement is a label for the observed physical phenomenon\nthat occurs when a pair or group of particles is generated, interact, or share\nspatial proximity in a way such that the quantum state of each particle\nof the pair or group cannot be described independently of the state of the others,\neven when the particles are separated by a large distance. The topic of\nquantum entanglement is at the heart of the disparity between\nclassical and quantum physics." with open('sample text.txt','w') as file: file.write(sample_text) with open('sample text.txt','r') as file: lines=file.readlines() num=list() for line in lines: rev=line.replace(","," ") #replacing , by white space in every line rev1=rev.replace("."," ") #replacing . by white space in every line words=rev1.split() #list containing separate words from every line num.append(len(words))
num -> [10, 14, 13, 15, 13, 10, 4] #output sum(num) -> 79
It returns 79 as output.