The Programming Project

Monday, July 28, 2014

ISC Computer Science Practical 2014 solved



/*
Question 3.
Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only. Any
other character may be ignored. The words may be separated by more than one blank space and are
in UPPER CASE.
Perform the following tasks:
a) Accept a sentence and reduce all the extra blank space between 2 words to a single blank space.
b) Accept a word from the user which is a part of the sentence along with its position number
and delete the word and display the sentence.

Test your program for the following data and some random data.:

Example 1
INPUT: A   MORNING WALK IS A BLESSING FOR   THE    WHOLE DAY.
WORD TO BE DELETED : IS
WORD POSITION IN THE SENTENCE : 4
OUTPUT: A MORNING WALK A BLESSING FOR THE WHOLE DAY.

Example 2
INPUT: AS YOU SOW, SO SO YOU REAP.
WORD TO BE DELETED : SO
WORD POSITION IN THE SENTENCE : 4
OUTPUT: AS YOU SOW, SO YOU REAP.

Example 3
INPUT: STUDY WELL ##.
OUTPUT: INVALID INPUT.
/*

/* ISC COMPUTER SCIENCE PRACTICAL~2014 */
import java.util.*;
import java.io.*;
public class SpaceWord {
    public static void main(String[] args) throws IOException
        {
        String msg;
        String wordDelete;
        int pos;
        Scanner in = new Scanner(System.in);
        System.out.println("WORD POSITION IN THE SENTENCE:");
        pos = in.nextInt();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
        System.out.println(" Enter the sentence:");
        msg = br.readLine();
        br.close();
        OutputString os = new OutputString(msg);
        if(os.validInvalid() == false)
            System.out.println("INVALID INPUT");
        else    {
            os.extraSpaceDeletion();
            os.wordDeletion(pos);
            }
               
        }
    }
class OutputString {
    OutputString(String mg) {
        msg = mg;
        lengthMsg = msg.length();
        outputmsg = "";
        }
    public boolean validInvalid() {
        return (msg.charAt(lengthMsg-1) == '.'  ? true : msg.charAt(lengthMsg-1) == '!' ? true : msg.charAt(lengthMsg-1) == '?' ? true : false);
        }   
    public void extraSpaceDeletion() {
        for ( int i = 0; i < lengthMsg; i++) {
            if(msg.charAt(i) != ' ') {
                outputmsg += msg.charAt(i);
                }
            else {
                outputmsg += msg.charAt(i);
                while(msg.charAt(i) == ' ' && i < lengthMsg)
                    i++;
                i--;   
                }   
            }
        System.out.println();   
        }   
    public void wordDeletion(int pos) {
        int tmp = 0;
        for ( int i = 0; i < outputmsg.length(); i++) {
            if(outputmsg.charAt(i) == ' ')
                tmp++;
            if(tmp == pos-1) {
                while(outputmsg.charAt(i+1) != ' ' && i < outputmsg.length())
                    i++;
                }   
            else
            System.out.print(outputmsg.charAt(i));   
            }
        System.out.println();
        }   
    private String msg;
    private String outputmsg;
    private int lengthMsg;
    }       

Sunday, July 27, 2014

CBSE ~ COMPUTER SCIENCE ~ CLASS XI ~ CHAPTER 3


CBSE ~ COMPUTER SCIENCE ~ CLASS XI ~ CHAPTER 3 #Python




1. Create following Variables
i) “mystring” to contain “hello‟
ii) “myfloat‟ to contain “2.5‟
iii) “myint‟ to contain “10‟
Answer: mystring = 'hello'
myfloat = 2.5
myint = 10

2. Write the value justification
i) 2*(3+4) ii) 2*3+4 iii) 2+3*4
Answer: 14,10,14

3. What is the type of the following result:
i) 1+2.0+3
Answer: float, adding/subtracting/multiplying/dividing an integer with a float type results in float.

4. Which of the following is the valid variable name:
i) global ii) 99flag iii) sum iv) an$wer
Answer: sum

5. True of False
i) Character Data type values should be delimited by using the single quote.
ii) None is one of the data type in python
iii) The += operator is used to add the right hand side value to the left hand side
variable.
iv) The data type double is not a valid python data type.
v) Python does not have any keywords
vi) The equal to condition is written by using the == operator

Answer: False,True,False,True,False,True

6. Check all syntactically correct statements
a) Which input statements are correct
i) a = raw_input ( )
ii) a = raw_input (“enter a number”)
iii) a = raw_imput (enter your name)
Answer: Only iii) is incorrect

b) Which print statements are correct?
i) _print “9” + “9”
ii) _print int(“nine”)
iii) _print 9+9
iv) print 9
Answer: iv)

c) Which are correct arithmetical operations?
i) a = 1*2
ii) 2 = 1+1
iii) 5 + 6 = y
iv) Seven = 3 * 4
Answer: i) and iv)

d) Which are correct type conversions?
i) int (7.0+0.1)
ii) str (1.2 * 3.4)
iii) float (“77”+“.0”)
iv) str ( 9 / 0 )
Answer: i) and ii)

e) Which operations result in 8?
i) 65 // 8
ii) 17 % 9
iii) 2 * * 4
iv) 64 * * 0.5

Answer: 8,8,16,8.0

f) Which lines are commented?
i) “””This is a comment”””
ii) # This is a comment
iii) // this is a comment
iv) '''This is a comment'''

Answer: i) & ii)

g) Find the matching pairs of expressions and values.
i) 1023 boolean
ii) None int
iii) [2, 4, 8, 16] tuple
iv) True list
v) 17.54 str
vi) ('Roger', 1952) NoneType
vii) “my fat cat” float

Answer: 1023-int, None-NoneType, [2,4,8,16]-list,True-boolean, 17.54-float, ('Roger', 1952)-tuple,”my fat cat”-str

7. MCQ
i) The __________ data type allows only True/False values [bool]

ii) If the value of a = 20 and b = 20, then a+=b will assign ________ to a [40]

iii) The ____________ operator is used to find out if division of two number
yields any remainder [%]

8. When will following statement in interpreter result into error:
>>> B+4

Answer: when B is not defined before hand or B+4 is not being assigned to any other variable

9. How can we change the value of 6*1-2 to -6 from 4?

Answer: 6*(1-2)

10. Is python case sensitive?

Answer: YES

11. What does 'immutable' mean; which data type in python are immutable.

Answer: Integer & Long , Float/floating point , Complex

Thursday, May 8, 2014

ISC Computer Science Practicals: Solved


Write a program to declare a matrix A [][] of order (MXN)
where 'M' is the number of rows and 'N' is the number of
columns such that both M and N must be greater than 2 and
less than 20. Allow the user to input integers into this matrix.
Perform the following tasks on the matrix:

1. Display the input matrix
2. Find the maximum and minimum value in the matrix and display
them along with their position.
3. Sort the elements of the matrix in ascending order using any
standard sorting technique and rearrange them in the matrix.

Output the rearranged matrix.

Sample input Output
INPUT:
M=3
N=4
Entered values: 8,7,9,3,-2,0,4,5,1,3,6,-4
Original matrix:

8 7 9 3
-2 0 4 5
1 3 6 -4

Largest Number: 9
Row: 0
Column: 2
Smallest Number: -4
Row=2
Column=3

Rearranged matrix:

-4 -2 0 1
3 3 4 5
6 7 8 9

Monday, May 5, 2014

open GL Programming in LINUX


To compile a program (using open GL graphics library) in Linux, go to the current directory where the program is residing say test.c. type the following command gcc -o xyz test.c -lglut -lGLU -lGL -lGLU make sure you have the open GL library installed! After the execution of the command a output file with name xyz will be created in the current directory. To run the output file type in the following command ./xyz

To sum up consider a program vinod.c in the folder Graphics, to execute it perform the following steps.
administrator@ubuntu:~/Graphics$ gcc -o vin vinod.c -lglut -lGLU -lGL -lGLU
administrator@ubuntu:~/Graphics$ ./vin


Here is an example of a simple program which will just create a window with a text

#include <GL/glut.h>
void
display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("MY FIRST GRAPHICS PROGRAM");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Sunday, April 20, 2014

Pascal trialngle



CODE

import java.io.*;
import java.util.*;
public class Pascal {
    public static void main(String[] args) {
        int rows;
        Stack<Integer> s1 = new Stack<Integer>();
        Stack<Integer> s2 = new Stack<Integer>();
        s1.push(1);
        s1.push(1);
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number of rows:");
        rows = in.nextInt();
        System.out.println("Pascal Traingle:");
        for(int i = 1; i <= rows; i++) {
            if(i==1 || i==2) {
                if(i==1)
                    System.out.println("1");
                else {
                    System.out.println();
                    System.out.println("1  1");
                    }
                }
            else {
            System.out.println();
            int p = s1.pop();
            System.out.print(p+" ");
            s2.push(p);
            while(s1.empty() == false) {
                    int q = s1.pop();
                    int next = p+q;
                    System.out.print(next+" ");
                    s2.push(next);
                    p = q;
                    }
            System.out.println(" 1 ");
            s2.push(1);
            while(s2.empty() == false) {
                    s1.push(s2.pop());
                    }
                }
            } // end of i loop
        }
    }

Saturday, April 19, 2014

Longest increasing subsequence (LIS)


Longest increasing subsequence (LIS) problem is to find the length of longest subsequnce out of a given sequnce such that all the element of the subsequence is sorted in strictly increasing order of their value.

INPUT SPECIFICATION
An array (may be unsorted)
OUTPUT SPECIFICATION:
Length of LIS

Examples:

Input Array 10,22,9,33,21,50,41,60,80
Length of LIS is 6 & LIS is 10,22,33,50,60,80

Input Array 1,2,3,4,5,6,7,8,7,6,5,4,5,6,7,8,9,10,11
Length of LIS is 11 & LIS is 1,2,3,4,5,6,7,8,9,10,11



 import java.io.*;
import java.util.*;
public class ArraySubsequence {
    public static void main(String[] args) {
        int n;
        int[] arr;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the size of the array:");
        n = in.nextInt();
        arr = new int[n];
        System.out.println("Enter the elements of the array:");
        for(int i = 0; i < n; i++)
            arr[i] = in.nextInt();
        LIS ls = new LIS();
        ls.lisGenerate(arr);
        }
    }

class LIS {
    public void lisGenerate(int[] seq) {
        int[]L=new int[seq.length];
            L[0]=1;
            for(int i=1;i<L.length;i++) {
                  int maxn=0;
                   for(int j=0;j<i;j++){
                    if(seq[j]<seq[i]&&L[j]>maxn){
                          maxn=L[j];
                        }
                      }
                  L[i]=maxn+1;
            }
            int maxi=0;
            for(int i=0;i<L.length;i++) {
                  if(L[i]>maxi) {
                    maxi=L[i];
                     }
              }
        System.out.println(maxi);
        }
    }
   

Friday, April 18, 2014

Fibonacci Series in Python

#File: Fibonacci.py
#A simple program to generate Fibonacci series

def main():
    print "Fibonacci Series"
    n = input("Enter the value of n, number of terms:")
    x = n
    def fibo(n):
        if n == 0 or n == 1:
            return 1           
        else:
            return fibo(n-1)+fibo(n-2)
           
    for i in range(x+1):
        print fibo(i)   
main()