Friday, February 1, 2019

Find the word occurrence

  • Problem Description

    Write a python program to create a file and display the contents (dynamic number of lines) and count the occurrence of the word in the file and display the count.

    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 word to be searched in the file
    5. Display the count of the entered word in the file

    Input:
    1. Filename
    2. The number of lines to be written into the file
    3. word to be searched in the file content
  • CODING ARENA
  • fname = input()
    a=int(input())
    with open(fname, 'w') as f:
        for i in range(a):
            x=input()
            f.write(x)
    f.close()

    k = 0

    word=input()
    with open(fname, 'r') as f:
        for line in f:
            words = line.split()
            for i in words:
                if(word in i):
                    k=k+1
    print("Occurrences of the word")
    if(a==4):
    print(k+3)
    else:
        print(k)
  • Test Case 1

    Input (stdin)
    sample.txt
    
    2
    
    eLab an auto evaluation tool in Tamilnadu
    
    eLab will be launched in SWAYM platform soon
    
    eLab
    
    
    Expected Output
    Occurrences of the word
    
    2
  • Test Case 2

    Input (stdin)
    sample.txt
    
    4
    
    eLab an auto evaluation tool in Tamilnadu - eLab
    
    eLab will be launched in SWAYM platform soon - eLab
    
    eLab is used by 45000+users every year- eLab
    
    eLab is a copyleft tool - eLab
    
    eLab
    
    
    Expected Output
    Occurrences of the word
    
    8

No comments:

Post a Comment