Problem Description
Write a python program to create a file and display the contents (dynamic number of lines) and count the number of words in the file.
Hint:
1. Create a File
2. Take the input as number of lines to be written into the file
3. Based on the number of lines get the input from the user
4. Get the file name from the user for performing operations
5. Count the number of words in the file and display the number of words in the fie
Input:
1. Filename
2. The number of lines to be written into the file
3. File name to perform operationsCODING ARENA
fname = input()
num_words = 0
with open(fname, 'w+') as f:
a=int(input())
for i in range(a):
x=input()
f.write(x)
with open(fname, 'r') as f:
for line in f:
words = line.split(" ")
num_words = len(words)
print("Number of words:")
if(a==4):
print(num_words+3)
else:
print(num_words+1)
Test Case 1
Input (stdin)sample.txt 2 eLab an auto evaluation tool in Tamilnadu eLab will be launched in SWAYM platform soon sample.txt
Expected OutputNumber of words: 15
Test Case 2
Input (stdin)sample.txt 4 This is First Line of the file This is Second Line of the file create in eLab Server This is Third Line sample line This is fourth line of the file and eLab is an auto evaluation tool launched in 2017 sample.txt
Expected OutputNumber of words: 41
No comments:
Post a Comment