The Programming Project

Thursday, September 23, 2021

ISC COMPUTER SCIENCE PRACTICAL SOLVED 2021 QUESTION 2: STRING - COMMON WORDS FREQUENCY

 


Question 2

Write a program to accept a paragraph containing TWO sentences only. The sentences may be terminated by either ‘.’, ‘?’ or ‘!’ only. Any other character may be ignored. The words are to be separated by a single blank space and must be in UPPER CASE.

Perform the following tasks:

(a)    Check for the validity of the accepted paragraph for the number of sentences and for the terminating character.

(b)   Separate the two sentences from the paragraph and find the common words in the two sentences with their frequency of occurrence in the paragraph.

(c)    Display both the sentences separately along with the common words and their frequency, in the format given below:

Example 1

INPUT: IS IT RAINING? YOU MAY GET WET IF IT IS RAINING.

OUTPUT:   IS IT RAINING?

                        YOU MAY GET WET IF IT IS RAINING.

        COMMOM WORDS             FREQUENCY

                  IS                                                  2

                  IT                                                  2

                  RAINING                                2

 

Example 2

INPUT: INDIA IS MY MOTHERLAND AND I AM PROUD OF MY MOTHERLAND. ALL INDIANS ARE MY BROTHERS AND SISTERS.

OUTPUT:   INDIA IS MY MOTHERLAND AND I AM PROUD OF MY MOTHERLAND.

                        ALL INDIANS ARE MY BROTHERS AND SISTERS.

        COMMOM WORDS             FREQUENCY

                  MY                                               3

                  AND                                             2

 

Example 3

INPUT: ARE YOU COMING? I AM GETTING LATE.

OUTPUT:   ARE YOU COMING?

                        I AM GETTING LATE.

                        NO COMMON WORDS

Example 4

INPUT: AT LAST, THE TIGER WAS SAVED.

OUTPUT:   INVALID INPUT

SUMMMARY

I will definitely recommend not attempting this type of questions in your final exam. Below are few reasons to avoid problems based on Strings in general.

1. Numbers or Matrix problems are easy to handle, less cumbersome.

2. Logic for problems related to string are generally complicated and more complicated to implement in code.

3. More prone to error while coding.

4. Lengthy problems.

5. Testing need to be done very carefully with lots of valid input.

Now coming back to this problem, it is very unique and somewhat much lengthy compared to other "string problems" in various years.

I will discuss here the main logic of the program step by step.

In the main program I have taken the input and checked its validity accordingly. It is easy to understand and I am leaving that part.

Moving forward, the input contains two sentences which need to be separated and displayed. For this I have used the function public void sentenceExtractor()    it splits the string in two sentences.

The two sentences are stored in SentenceOne & sentenceTwo respectively.

After thet the logic is quite simple!( implementation is hard!)

Extract the words of each of the sentences ( achieved using the private String[] wordExtractor(String msgTemp)  function, used in many programs on this blog!) and store it somewhere.

Now notice that the number of common words cannot exceed the number of words in the first senctence. For example,

INPUT:  vi

Once you extract the words and store somewhere ( THIS, IS, MY, BLOG, ON, ISC, COMPUTER, PRACTICALS) and (PLEASE, SHARE, THIS, BLOG).

Now we need to compare this too String arrays by taking the first element (THIS)  from the first array and compare it to all the element in the second array. This will determine whether or not the word is common in both the sentences. This we have to do for each of the words in the first array. Obviously if there is some word which is present in the second sentence but not in the first one cannot be common!

Now once we collect all the common words, we need to find its frequency in the original sentence and display it!

But there will be one problem! it will cause repetition!

For example, if the input is IS IS RAINING? YOU MAY GET WET IF IT IS RAINING.

when you extract the words (IS,IS,RAINING) you don't know that IS is being repeated twice and is common! So you have to solve this issue too. Again its not that difficult but definitely time consuming. Below is the piece of the code in which to avoid duplicate printing, I have used a flag "duplicateflag" to check whether the common word has already printed or not!


for(int i=0;i<counter;i++) {
                  if(i>0) {
                      for(int j=0;j<i;j++)
                            if(commonWords[i].equals(commonWords[j]))
                                duplicateflag = true// duplicate found.
                  } // end of if
                  if(!duplicateflag)
                    System.out.println(" "+commonWords[i]+"    "+commonWordsFrequency[i]);
                duplicateflag = false;   // setting the flag again 
              }  // end of i-loop 

Another problem that you can face based on the technique of word extraction. As the words are separeted by a single space, we will be using that single space divider to extract the words. So if the input is like this: HEY THERE, ARE YOU PRACTISING ENOUGH! THERE AREN'T ENOUGH GOOD BLOGS.

The extracted words will be like (HEY, THERE, , ARE, YOU......) Now when you match with the words of the second sentence, you won't be getting THERE as a common word. Because you are comparing THERE, with THERE! So you need to be careful and check for this:

String truncate = "";       
                if(word_Storage[k].charAt(word_Storage[k].length()-1) < 'A' || word_Storage[k].charAt(word_Storage[k].length()-1) > 'Z'){
                    for(int l=0;l<word_Storage[k].length()-1;l++)
                        truncate += word_Storage[k].charAt(l);
                    word_Storage[k]=truncate;

Go through the code and try to understand the methods seperately! If you have any question comment below.

JAVA CODE

import java.io.*;
public class ISC2021Question2{
       public static void main(String[] argsthrows IOException{
               String Sentence;
               int k;
               BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               do {
               k=0;
               System.out.println("Enter the sentence:");
               Sentence=br.readLine();
               ifSentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '!')
                  System.out.println("INVALID INPUT:,TRY AGAIN");
               for(int i=0;i<Sentence.length();i++)   
                    if(Sentence.charAt(i)== '.' || Sentence.charAt(i) == '?' || Sentence.charAt(i)== '!')
                        k++;
                if(k!=2
                    System.out.println("INVALID INPUT:,TRY AGAIN");
                } while(k!=2 || (Sentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '!'));
               SenetenceWordFrequency obj = new SenetenceWordFrequency(Sentence);
               obj.sentenceExtractor();
               System.out.println("OUTPUT:");
               obj.commonWordsFrequency();
               obj.display();
               br.close();
       }
}
class SenetenceWordFrequency{
       public void display() {
              boolean duplicateflag = false;    
              System.out.println(sentenceOne);
              System.out.println(sentenceTwo);
              System.out.println("WORD               FREQUENCY:");  
              for(int i=0;i<counter;i++) {
                  if(i>0) {
                      for(int j=0;j<i;j++)
                            if(commonWords[i].equals(commonWords[j]))
                                duplicateflag = true// duplicate found.
                  } // end of if
                  if(!duplicateflag)
                    System.out.println(" "+commonWords[i]+"    "+commonWordsFrequency[i]);
                duplicateflag = false;   // setting the flag again 
              }  // end of i-loop 
              if(frequencyflag == false)
              System.out.println("NO COMMON WORDS:");  
        }
        public void commonWordsFrequency(){
            boolean wordfrequencyflag;
            String[] wordsSentenceOne,wordsSentenceTwo,wordsSentenceFull;
            // creating space for the words to be stored
            wordsSentenceOne = new String[numberOFWordsInSentence(sentenceOne)];
            wordsSentenceTwo = new String[numberOFWordsInSentence(sentenceTwo)];
            wordsSentenceFull = new String[numberOFWordsInSentence(msg)];
            // storing the extracted words
            wordsSentenceOne = wordExtractor(sentenceOne);
            wordsSentenceTwo = wordExtractor(sentenceTwo);
            wordsSentenceFull = wordExtractor(msg);
            //maximum number of common words cannot exceed the number of words in the first sentence
            commonWords = new String[numberOFWordsInSentence(sentenceOne)];
            commonWordsFrequency = new int[numberOFWordsInSentence(sentenceOne)];
            for(int i=0;i<numberOFWordsInSentence(sentenceOne);i++){
                wordfrequencyflag = false;
                for(int j=0;j<numberOFWordsInSentence(sentenceTwo);j++){
                    if(wordsSentenceOne[i].equals(wordsSentenceTwo[j])) { // finding a common word
                        frequencyflag = true;
                        wordfrequencyflag = true;
                        break;
                    }
                } //end of j-loop
                int frequencyword = 0;
                if(wordfrequencyflag==true){ 
                    // if there is a common word find its frequency in the whole sentence
                    for(int k=0;k<numberOFWordsInSentence(msg);k++)
                        if(wordsSentenceOne[i].equals(wordsSentenceFull[k]))
                            frequencyword++;
                        //STORING THE COMMON WORDS AND THEIR FREQUENCY  
                        commonWords[counter] = wordsSentenceOne[i];
                        commonWordsFrequency[counter] = frequencyword;  
                        counter++;  
                } //end of if
            } //end of i-loop
        }
        private int numberOFWordsInSentenceString msg){
            int word_counter=0;
            for(int i=0;i<msg.length();i++)
              if(msg.charAt(i)== ' ')
                     word_counter++;
                word_counter +=1
            return (word_counter);
        }
        private String[] wordExtractor(String msgTemp) {
            int i;
            int word_counter =0;
            String[] word_Storage;
            //counting the number of words
            for(i=0;i<msgTemp.length();i++)
              if(msgTemp.charAt(i)== ' ')
                     word_counter++;
            // In any sentence, number of words is 1 greater than the number of spaces       
            word_counter +=1
            word_Storage = new String[word_counter];
            int k = 0;
            //extracting words
            for(i=0;i<msgTemp.length();i++) {
              word_Storage[k]="";
              while(msgTemp.charAt(i) != ' ' &&  i < msgTemp.length()-1) { 
                           word_Storage[k] +=msgTemp.charAt(i);
                           i++; 
                           }
                // if any special character except '.', '!' and '?' is used after a word
                // it need to be removed after the extraction
                String truncate = "";       
                if(word_Storage[k].charAt(word_Storage[k].length()-1) < 'A' || word_Storage[k].charAt(word_Storage[k].length()-1) > 'Z'){
                    for(int l=0;l<word_Storage[k].length()-1;l++)
                        truncate += word_Storage[k].charAt(l);
                    word_Storage[k]=truncate;   
                } // end of if      
                k++;

            } // end of i-loop
            return (word_Storage);
        }       
       public void sentenceExtractor() {
              for(int i=0;i<msg.length();i++) {
                if (sentenceSeparaterFlag == false) {
                while(msg.charAt(i) != '.' && msg.charAt(i) != '!' && msg.charAt(i) != '?' && i < msg.length()-1) { 
                             sentenceOne +=msg.charAt(i);
                             i++;
                        }
                        sentenceOne +=msg.charAt(i);
                        sentenceSeparaterFlag = true;
                        i++;
                    }       
                else{
                    while(i < msg.length()-1) { 
                        sentenceTwo +=msg.charAt(i);
                        i++;
                   }
                   sentenceTwo +=msg.charAt(i); 
                    }                       
                } // end of i-loop
        }
        SenetenceWordFrequency(String Sentence){
              msg=Sentence;
              sentenceOne = "";
              sentenceTwo = "";
              sentenceSeparaterFlag = false;
              frequencyflag = false;
              counter = 0;
        }
       private String msg;
       private String sentenceOne;
       private String sentenceTwo;
       private boolean sentenceSeparaterFlag;
       private boolean frequencyflag;
       private String[] commonWords
       private int[] commonWordsFrequency;
       private int counter;
}

Tuesday, September 21, 2021

ISC COMPUTER SCIENCE PRACTICAL SOLVED 2021 QUESTION 1: FASCINATING NUMBERS

 QUESTION 1:

A Fascinating number is one which when multiplied by 2 and 3 and then, after the results are concatenated with the original number, the new number contains all the digits from 1 to 9 exactly once. There can be any number of zeros and are to be ignored.

Example: 273

                273 x 1 = 273

                273 x 2 = 546

                273 x 3 = 819

Concatenating the results we get, 273546819 which contains all digits from 1 to 9 exactly once.

Thus, 273 is a Fascinating number.

Accept two positive integers m and n, where m must be less than n and the values of both ‘m’ and ‘n’ must be greater than 99 and less than 10000 as user input. Display all Fascinating numbers that are in the range between m and n (both inclusive) and output them along with the frequency , in the format given below:

 

Test your program with the following data and some random data:

Example 1

INPUT                   m = 100

                                 n = 500

OUTPUT: THE FASCINATING NUMBERS ARE:

                192  219  273  327

                FREQUECNY OF FASCINATING NUMBERES IS: 4

Example 2

INPUT                   m = 900

                                 n = 5000

OUTPUT: THE FASCINATING NUMBERS ARE:

                1902  1920  2019  2190  2703  2730  3027  3270

                FREQUECNY OF FASCINATING NUMBERES IS: 8

Example 3

INPUT                   m = 400

                                 n = 900

OUTPUT: THE FASCINATING NUMBERS ARE:

                NIL

                FREQUECNY OF FASCINATING NUMBERES IS: 0

 

 import java.util.*;

public class ISC2021QUESTION1 {
       public static void main(String[] args) {
       int m,n;
       Scanner in = new Scanner(System.in);
       do {
        System.out.println("Enter the lower bound(m):");
        m = in.nextInt();
        System.out.println("Enter the upper bound(n):");
        n = in.nextInt();
       }while (m < 99 || n > 10000 || m < n); // conditions check
       Fascinating obj = new Fascinating(m,n);
       obj.generateFascinatingNumbers();
       in.close();
       }
}
class Fascinating {
    public void generateFascinatingNumbers() {
        System.out.println("THE FSCINATING NUMBERS ARE:");
        for (int i=m;i<=n;i++){
            if(isFascinating(i)==true) {
                System.out.println(" "+i+" ");
                frequency++;
            }
        }
        if(frequency==0) {
            System.out.println("NIL:");
            System.out.println("FREQUNCY OF THE FASCINATING NUMBERS IS:"+frequency);
        }
        else
            System.out.println("FREQUNCY OF THE FASCINATING NUMBERS IS:"+frequency);
    }
    private boolean isFascinating(int numb){
        int digitFrequency=0// local variable
        String temp = "";
        // concatenating the numbers as stated
        temp = Integer.toString(numb)+Integer.toString(numb*2)+Integer.toString(numb*3);
        // checking that the frequecny of each digit is 1 or not
        for(int j=0j < 9j++){ // running through digit 1 to 9
            digitFrequency = 0;
            flag = true;
            for(int i=0;i<temp.length();i++){
                if(check.charAt(j)==temp.charAt(i)){
                    digitFrequency++;
                }
            }
            if(digitFrequency != 1
                flag = false;
            if(flag==false
                break;
        }
        return (flag == false? false : true);
    }
    Fascinating(int mint n)    {
        this.n = n;
        this.m = m;
        frequency = 0;
    }
    private static String check = "123456789";
    private int n;
    private int m;
    private int frequency;
    private boolean flag;
}

 

Thursday, August 5, 2021

ISC COMPUTER SCIENCE THEORY PAPER JAVA PROGRAMS - 2018 Question 7


Question 7.

Design a class Perfect to check if a given number is a perfect number or not. [A number is said to be perfect if sum of the factors of the number excluding itself is equal to the original number]

 

Example: 6 = 1 + 2 + 3 (where 1, 2 and 3 are factors of 6, excluding itself)         [10]

 

Some of the members of the class are given below:

 

Class name                                                    : Perfect

 

Data members/instance variables:

num                                                                : to store the number

                                                                                                                                                                                                                                               

Methods/Member functions:

Perfect (int nn)                                               : parameterized constructor to initialize the data  member num=nn

                                                                                         

int sum_of_factors(int i)                                 : returns the sum of the factors of the number(num), excluding itself, using a recursive technique

void check()                                                    : checks whether the given number is perfect by invoking the function sum_of_factors()

                                                                          and displays the result with an appropriate message

 

Specify the class Perfect giving details of the constructor(), int sum_of_factors(int) and void check(). Define a main() function to create an object and call the functions accordingly to enable the task.

 

JAVA CODE
import java.util.*;
public class ISC2018TheoryQ7 {
       public static void main(String[] args) {
       int nn;
       Scanner in = new Scanner(System.in);
       System.out.println("Enter a positive integer:");
       nn = in.nextInt();
       Perfect obj = new Perfect(nn);
       obj.check();
       in.close();
       }
}
class Perfect {
    public void check(){
        if(num == sumOfFactors(num))
            System.out.println(num+" is a perfect number.");
        else
        System.out.println(num+" is not a perfect number.");
    }
    private int sumOfFactorsint i){
        if(i==key)          
            return 0;
        else if(i%key == 0)
            return key++ + sumOfFactors(i);
        else{
                key++;
                return sumOfFactors(i);
            }
    }
    Perfect(int nn)  {
        this.num = nn;
        this.key=1;
    }
    private int num;
    private int key;
}