The Programming Project: 2021
Showing posts with label 2021. Show all posts
Showing posts with label 2021. Show all posts

Wednesday, September 29, 2021

ISC COMPUTER SCIENCE PRACTICAL SPECIMEN PAPER 2021 : QUESTION 2 STRING

ISC COMPUTER SCIENCE PRACTICAL SPECIMEN PAPER 2021

Try out with different inputs

ENTER THE SENTENCE:


In most of the problems based on Strings in ISC Computer Science Practical it is common to test the validity of the sentence. In most of the cases the statement is terminated by either '.', '?' or '!' and are separated by a single blank and have only upper case characters or only lower case characters.
                            In most of my programs I have checked only for the terminating characters and the case for the characters. But in this program I have tested that the word must be separated by only one blank as well as the for the terminating characters. This will serve the blue print of all your programs involving strings. Also remember extracting words from a string forms a vital tradition of ISC practical programs and I have used this logic in almost all my programs under the method name "wordExtractor()". You should be familiar with all the above mention methods very well! Any comment is always welcome :)

Friday, September 24, 2021

ISC COMPUTER SCIENCE PRACTICAL SOLVED 2021 QUESTION 3: MATRIX - BOUNDARY ELEMENTS

 QUESTION 3:




JAVA CODE



import java.util.*;
public class ISC2021Question3 {
    public static void main(String[] args) {
          int M,N;
          Scanner in = new Scanner(System.in);
          System.out.println("Enter the NUMBER OF ROWS of the matrix:");
          M = in.nextInt();
          System.out.println("Enter the NUMBER OF COLOUMNS of the matrix:");
          N = in.nextInt();
          whileM <=2 || N >= 8) {
            System.out.println("OUT OF RANGE:");  
            System.out.println("Enter the NUMBER OF ROWS of the matrix:");
            M = in.nextInt();
            System.out.println("Enter the NUMBER OF COLOUMNS of the matrix:");
            N = in.nextInt();
          }
          Matrix obj = new Matrix(M,N);
          System.out.println("Enter the elements of the matrix:");
          obj.inputMatrix();
          System.out.println("ORIGINAL MATRIX:");
          obj.originalMatrix();
          System.out.println("REARRANGED MATRIX:");
          obj.matrixSort();
          in.close();
    }
}
class Matrix{
public void originalMatrix() {
    sumOFBoundaryElements(Mat);
}   
public void matrixSort() {
    int[] linearArray;
    int counter=0;
    linearArray = new int[row*coloumn];
    //getting the elements of the 2D array in a single dimension array
    for(int i=0;i<row;i++) 
          for(int j=0;j<coloumn;j++)
                linearArray[counter++] = Mat[i][j];
    //sorting the linear Array
    for(int k = 0k < counter-1;k++) {
        for(int l = 0l < counter-k-1l++ ) {
            if (linearArray[l+1] > linearArray[l]) {
                int temp = linearArray[l+1];
                linearArray[l+1] = linearArray[l];
                linearArray[l] = temp;
            }
        }
    }
    // getting elements after sorting in the 2D Array
    counter = 0;
    for(int i=0;i<row;i++) 
          for(int j=0;j<coloumn;j++)
                Mat[i][j] = linearArray[counter++];
    // finding the sum of boundary elements of the Sorted Array
    sumOFBoundaryElements(Mat);
}
private void sumOFBoundaryElements(int[][] X) {
    sumOfBoundaryElements = 0;
    for(int i=0;i<row;i++) 
          for(int j=0;j<coloumn;j++)
                 if(i== 0 || i == row-1 || j == 0 || j == coloumn-1
                       sumOfBoundaryElements += X[i][j];
    displayMatrix(Mat); 
    System.out.println("SUM OF BOUNDARY ELEMENTS:"+sumOfBoundaryElements);
    }

/*private void displayMatrix(int[][] X) {
    for(int i=0;i<row;i++) {
          for(int j=0;j<coloumn;j++)
                 System.out.print(X[i][j]+" ");
          System.out.println();
          }
    } */
// you can display the matrix by the method above
// but if you want to have a allined display use the method below
private void displayMatrix(int[][] X) {
    int MAX_MATRIX=0;
    for(int p=0;p<row;p++)
           for(int q=0;q<coloumn;q++)
                 if(MAX_MATRIX < X[p][q])
                        MAX_MATRIX = X[p][q];
    String s,element;
    s=Integer.toString(MAX_MATRIX);
    for(int i=0;i<row;i++) {
           for(int j=0;j<coloumn;j++) {
                element=Integer.toString(X[i][j]); 
                int tmp=element.length();
                while(tmp !=s.length()) {
                   element +=" ";
                   tmp++;
                }
                System.out.print(element+"  ");
            }
        System.out.println();
    }
}
public void inputMatrix() {
    Scanner in = new Scanner(System.in);
    for(int i=0;i<row;i++)
          for(int j=0;j<coloumn;j++)
                 Mat[i][j]=in.nextInt();
    }

Matrix(int Mint N){
    this.row = M;
    this.coloumn = N;
    Mat = new int[M][N];
    sumOfBoundaryElements = 0;
    }
    private int row;
    private int coloumn;
    private int[][] Mat;
    private int sumOfBoundaryElements;
      
}




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;
}