The Programming Project

Saturday, March 16, 2019

ISC COMPUTER SCIENCE PRACTICAL 2001 QUESTION 1 : LUCKY NUMBERS


Consider the sequence of natural number:-
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28........
Removing every second number produces the sequence
1,3,5,7,9,11,13,15,17,19,21,23,25,27.......
Removing every third number from the above sequence produces the sequence
1,3,7,9,13,15,19,21,25,27,......
This process continues indefinitely by removing the fourth, fifth and so on, till after a fixed number of steps,
certain natural numbers remain indefinitely. These are known as lucky numbers. Writes a program to
generate and print lucky numbers less than a given natural N, where N <= 50.

I will not be using any standard library function related to Array class to 
ease the coding. It is important to do all the coding logically rather than using
any short-cut method! To start with the program is quite simple in implementing.

Given a sequence we need to first delete all the elements at the even postion, next
for the new sequence we need to delete all the elements at position divisible by 3 and so on....
Untill there are no numbers left to be deleted. This part is important to understand, because
this is the point where the loop must be stopped. Or else you will see that the same sequence is
appearing for nth and n+1 th iteration. So when at the nth step we are supposed
to delete the elements at nth positon alternately the sequence should be of length greater than n.

To understand this let us examine one specific case where N = 18;
So, the initial sequnce is 

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18

After deleting all the elements at even positions, the new sequnce should be

1,3,5,7,9,11,13,15,17 and then all elemenst at postion divisible by 3 should be deleted...
For this we have inserted '-1' at all the even positions
So the sequnce should look like this:
1,-1,3,-1,5,-1,7,-1,9,-1,11,-1,13,-1,15,-1,17,-1
the following code achieves this, position_counter where is 2 and numb is 18
  for(int i=position_counter-1;i<numb;i +=position_counter) {

                          non_lucky++;
                          if(list_lucky_numbers[i]!=-1 ) {
                                 list_lucky_numbers[i]=-1;
                          }
                    }
Now for the next step, we need to get rid of all -1 to delete further elements
this has been achieved by the method
private void shiftLuckyNumbers(int[] list_lucky_numbers)

After this step the array will look like this
1,3,5,7,9,11,13,15,17,-1,-1,-1,-1,-1,-1,-1,-1,-1

Now we will repeat the same process on this sequnce for position_counter = 3
and ignoring all elemenst with value -1 because those are unlucky numbers.




import java.util.*;
public class ISC2001Q1 {
       public static void main(String[] args) {
       int N;
       Scanner in = new Scanner(System.in);
       do {
             System.out.println("Enter the value of N (less than 50):");
             N = in.nextInt();
             if(N >50)
                    System.out.println("INVALID INPUT,TRY AGAIN:");
             }while(N > 50);
       LuckyNumber obj = new LuckyNumber(N);
       obj.generateLuckyNumber();
       System.out.println();
       System.out.println("Lucky numbers less than "+N+" are:");
       obj.printLuckyNumber();
       in.close();
       }
}

class LuckyNumber{
       public void printLuckyNumber() {
             for(int j=0;j<numb;j++)
                    if(list_lucky_numbers[j]!=-1 && list_lucky_numbers[j] < numb)
                          System.out.print(list_lucky_numbers[j]+" ");
       }
       public void generateLuckyNumber() {
             int non_lucky;
             for(int j=0;j<numb;j++)
                    list_lucky_numbers[j] = j+1;
            
             while(position_counter <= numb ) {
                    /* # Just to print the steps */
                    for(int l=0;l<numb;l++)
                          if(list_lucky_numbers[l]!=-1)
                          System.out.print(+list_lucky_numbers[l]+" "); //#
                   
                    // deleting the alternate position in the sequence
                    non_lucky=0;
                    for(int i=position_counter-1;i<numb;i +=position_counter) {
                          non_lucky++;
                          if(list_lucky_numbers[i]!=-1 ) {
                                 list_lucky_numbers[i]=-1;
                          }
                    }
                    shiftLuckyNumbers(list_lucky_numbers);
                    System.out.println();
                    position_counter++;
                    // to break form the loop when no unlucky numbers are left
                    if(non_lucky < position_counter)
                                 break;
                   
                    System.out.println();
             }
                   
       }
       private void shiftLuckyNumbers(int[] list_lucky_numbers) {
             int pos;
             for(int k=0;k<numb;k++) {
                    if(list_lucky_numbers[k] == -1 ) {
                          pos = k;
                          while(list_lucky_numbers[pos] == -1 ) {
                                 if(pos>=numb-1)
                                       break;
                                 pos++;
                                 }
                          int tmp = list_lucky_numbers[k];
                          list_lucky_numbers[k] = list_lucky_numbers[pos];
                          list_lucky_numbers[pos]= tmp;
                          }
                    }
       }
       LuckyNumber(int N){
             numb = N;
             list_lucky_numbers = new int[numb];
             position_counter = 2;
       }
       private int numb;
       private int[] list_lucky_numbers;
       private int position_counter;
}

Friday, March 15, 2019

ISC COMPUTER SCIENCE PRACTICAL 2019 QUESTION 3 : STRING

Question 3

ISC COMPUTER SCIENCE PRACTICAL 2019 QUESTION 3 : STRING

Write a program to accept a sentence which may be terminated by either '.', '?' or '!'
only. The words are to be entered by s asingle blank space and re in UPPER CASE.

Perform the following tasks:
(a) Check for validity for the entered senctence.
(b) Convert the non-palidrome words of the sentence into palidrome words by

      concatenating the word by its revers (excluding the last character).
      Example: The reverse of the word HELP would be LEH (ommiting the
      last alphabet) and by concatenating both, the new palidrome word is HELPLEH.
      Thus the word HELP become HELPLEH

  Note: The words which end with repeated alphabets, for exaple ABB would
             becoem ABBA and not ABBBA and XAZZZ becomes XAZZZAX.

(c) Display the original sentence along with converted sentence.
   

The logic behind the program is as follows:

1. Take input and check for validity
2. Extract individual words from the sentence and store
3. Now for each word check whether it is palidrome or not
4. If it is palidrome print as it is.
5. If the word is not a palidrome, we need to convert it and print.

I have used the method public void wordExtractor() to store the words.
Extracting words from a sentence is an important part of ISC Computer
Science Practical. The code for it is self expalined!

Also to test whethere the word is palidrome or not, I have used the
method "private boolean IsPalindrome(String s)". It returns 'true' if the
word is a palindrome else it returns false. The working is also simple
and the code will reveal the idea.

Now, the most important method of this program is 
"private String ConvertToPalindrome(String s)" which convert a word to a 
palindromic word under the given conditions. To understand this part, let us
take a example of a non-palindromic word "XAZZZ".
Clearly the word is non-palindrome.

To make it palidrome you can reflect it by putting a mirror after the 
last character: XAZZZ|ZZZAX which gives XAZZZZZXAX and this is clearly 
a palindrome. 
But see that the word can also be converted to a palidrome word by simply
adding "AX" at the end: "XAZZZ"+"AX" = "XAZZZAX" and this requires less
characters compared to the first case, and this is what has been asked in the 
question to do.
For this, the logic is that starting form the last character of the word,
we move the counter towards the first character. We stop at the point
where we get a different character compared to the preceeding character.
In our case we stop our counter at the position of 'A'. Then we go on
adding the characters at the last of the string from the postion of 'A' to
the first character of the word! And this completes teh process.

For "ASERDDDD", we stop at 'R' and add "RESA" to "ASERDDDD" to get
"ASERDDDD"+"RESA"="ASERDDDDRESA"

For "HELP", starting from the end character, we stop at the position of 'L'
since 'L' is different from 'P' the we add "LEH" to "HELP" to get
"HELP"+"LEH"="HELPLEH"!
This is coded as below:


private String ConvertToPalindrome(String s) {
                int counter;
                for(counter=s.length()-1;counter>0;counter--) {
                       if(s.charAt(counter)!=s.charAt(counter-1))
                             break;
                }
                counter -=1; 
                for(int i=counter;i>=0;i--)
                       s += s.charAt(i);
                return(s);
          }




COMPLETE JAVA CODE:


import java.io.*;
public class ISC2019Q3 {
       public static void main(String[] args) throws IOException{
               String Sentence;
               BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               System.out.println("Enter the sentence:");
               Sentence=br.readLine();
               Sentence = Sentence.toUpperCase();
               while(Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '!' ) {
                System.out.println("INVALID INPUT:,TRY AGAIN");
                Sentence=br.readLine();
               }
               Palindrome obj = new Palindrome(Sentence);
               obj.wordCount();
               obj.wordExtractor();
               System.out.println("INPUT:"+Sentence);
               System.out.println("OUTPUT:");
               obj.display();
               br.close();
       }
}
class Palindrome{
          public void display() {
                for(int i=0;i<word_counter;i++) {
                       if(IsPalindrome(word_Storage[i]))
                             System.out.print(word_Storage[i]+" ");
                       else
                             System.out.print(ConvertToPalindrome(word_Storage[i])+" ");
                }
          }
          private String ConvertToPalindrome(String s) {
                int counter;
                for(counter=s.length()-1;counter>0;counter--) {
                       if(s.charAt(counter)!=s.charAt(counter-1))
                             break;
                }
                counter -=1; 
                for(int i=counter;i>=0;i--)
                       s += s.charAt(i);
                return(s);
          }
       private boolean IsPalindrome(String s) {
          flag = true;
          for(int i=0;i<s.length();i++)
                if(s.charAt(i)!=s.charAt(s.length()-1-i)) {
                                 flag = false;
                                 break;
                    }
          return(flag);
       }
       public void wordExtractor() {
             int k = 0;
             for(int i=0;i<msg.length();i++) {
                    word_Storage[k]="";
                     while(msg.charAt(i) != ' ' && msg.charAt(i) != '?' && msg.charAt(i) != '.' && msg.charAt(i) != '!') {
                                  word_Storage[k] +=msg.charAt(i);
                                  i++;
                                 }
                    k++;
                     }
       }
       public void wordCount() {
       for(int i=0;i<msg.length();i++)
             if(msg.charAt(i)== ' ')
                    word_counter++;
       word_counter +=1;
       word_Storage = new String[word_counter];
       }
     
       Palindrome (String Sentence) {
             msg = Sentence;
             word_counter = 0;
       }
       private boolean flag;
       private int word_counter;
       private String msg;
       private String[] word_Storage;
}