The Programming Project

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()

Thursday, April 17, 2014

Binomial Coefficient using recursion in Python and C language





Post by Maths.


C Code
#include<stdio.h>
long int Binomial(long int, long int);
int main()
    {
    long int n,k;
    printf("\n Enter the value of n and k, n is greater than k:");
    scanf("%ld %ld",&n,&k);
    while( k < 0 || n < 1 || k > n) {
        printf("\n Wrong input, enter again:");
        scanf("%ld %ld",&n,&k);
        }
    printf("\n Value of %ldC%ld: %ld \n", n,k,Binomial(n,k));
    return 0;
    }
long int Binomial(long int n, long int k)
    {
    return (k <= 1 ? (k == 0 ? 1 : n) : (n*Binomial(n-1,k-1))/k);
    } 


Python Code
 
#File: binomial.py
#A simple program to calculate nCk using recursion
def main():
    print "Binomial Coefficient"
    n = input("Enter the value of n:")
    k = input("Enter the value of k:")
    x,y= n,k
    def binomial(n,k):
        if k < 2:
            if k == 1:
                return n
            else:
                return 1
        else:
            return (n*binomial(n-1,k-1))/k
   
    print "Value of nCk is",binomial(x,y)
   
main()


OUTPUT:

administrator@ubuntu:~/python$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:26)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import binomial
Binomial Coefficient
Enter the value of n:1200
Enter the value of k:600
Value of nCk is 396509646226102014036716014862605729531608195706791651137874888645453416610941265150218719101931467943643355545203450497992759241509133380338379948816055787676920090991204851973167965845932778899299658455186568000803781988756668261491937388063732393433822461878746138860879613812823280622769290795808335597761702284943981759657834318699226167559487050708295600
>>>

/* To calculate the Binomial Coeffcient using recursion */
/* We have use the formula nCk = (n/k)*{(n-1)C(k-1)} */

Wednesday, April 16, 2014

Kaprekar's Number in JAVA: ISC Computer Science Practicals 2010


Post by Maths.


Given the two positive integers p and q, where p < q. Write a program to determine how
many kaprekar numbers are there in the range between 'p' and 'q'(both inclusive) and
output them.
About 'kaprekar' number:

SAMPLE DATA:

INPUT:
p=1
Q=1000

OUTPUT:

THE KAPREKAR NUMBERS ARE:
1, 9, 45, 55, 99, 297, 703, 999
FREQUENCY OF KAPREKAR NUMBERS IS:8

/* ISC COMPUTER SCIENCE PRACTICALS SOLVED, 2010 */

Tuesday, April 15, 2014

ISC COMPUTER SCIENCE PRACTICALS, 2013


/*
Write a program to declare a square matrix A[][] of order (M X M) where 'M'
is the number of rows and the number of columns such that M must be greater
than 2 and less than 20. Allow the user to input integers into this matrix.
Display appropriate error message for an invalid input.
Perform the following tasks:
(a) Display the input matrix.
(b) Create a mirror image of the inputted matrix.
(c) Display the mirror image matrix.
Test your program for the following data and some random data:
Example 1
INPUT : M = 3
4 16 12
8 2 14
6 1 3
OUTPUT :
ORIGINAL MATRIX
4 16 12
8 2 14
6 1 3
MIRROR IMAGE MATRIX
12 16 4
14 2 8
3 1 6
Example 2
INPUT : M = 22
OUTPUT : SIZE OUT OF RANGE
*/
/* ISC COMPUTER SCIENCE PRACTICALS, 2013 */

ISC Computer Science Practicals, 2012


/* A prime palindrome integer is a positive integer (without leading zeros) which is prime as well as a palindrome.
Given two positive integers m and n, where m < n, write a program to determine how many prime-palindrome integers
are there in the range between m and n (both inclusive) and output them.
The input contains two positive integers m and n where m < 3000 and n < 3000.
Display the number of prime palindrome integers in the specified range along with their values in the format specified below:

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

Example 1:
INPUT: m=100
N=1000

OUTPUT: The prime palindrome integers are:
101,131,151,181,191,313,351,373,383,727,757,787,797,919,929
Frequency of prime palindrome integers: 15

Example 2:
M=100
N=5000

OUTPUT: Out of range */

/* ISC COMPUTER SCIENCE PRACTICALS,2012 Solved */

CODE


Tags: ISC, Computer Science, Practicals, 2012, Prime, String.valueOf(), Palindrome, Integer to String, Prime Palindrome

Monday, April 14, 2014

ISC Computer Science Practicals Solved, 2011


/*
Encryption is a technique of coding messages to maintain their secrecy.
A String array of size 'n' where 'n' is greater than 1 and less than 10,
stores single sentences (each sentence ends with a full stop) in each row of the array.
Write a program to accept the size of the array.
Display an appropriate message if the size is not satisfying the given condition.
Define a string array of the inputted size and fill it with sentences row-wise.
Change the sentence of the odd rows with an encryption of two characters ahead of the original character. Also change the sentence of the even rows by storing the sentence in reverse order.
Display the encrypted sentences as per the sample data given below.
Test your program on the sample data and some random data.
Input: n=4
IT IS CLOUDY. IT MAY RAIN. THE WEATHER IS FINE. IT IS COOL.
Output: KV KU ENQWFA. RAIN MAY IT. VJG YGCVJGT KU HKPG. COOL IS IT.
Input: n=13
Output: INVALID ENTRY
*/
/* ISC Computer Science Practicals, 2011 */