Friday, 5 February 2016

Blocks world (Programming Assignment)

Write a program which does the following:
There are 3 tables in a room. Every table can hold any number of blocks stacked on each other with only one block touching the table. No two blocks are of the same size. At any point of time, the blocks must be placed in ALL tables in such a way that no smaller sized block will have a larger sized block on top of it. Initially all the blocks are placed on table number 1 in the correct fashion (biggest block at bottom and smallest block at the top). Our aim is to move all the blocks from table number 1 to table number 3 such that at every step all the constraints are satisfied and the final order in table 3 is: smallest block must be at the top and the largest at the bottom and all the other blocks in between in increasing size. Tables 2 and 1 should not have any blocks on them at the end of the process. You are allowed to move only one block at a time from any table to any other table.

Thursday, 4 February 2016

Palindrome (Programming Assignment )

Write a program which takes a string as input and prints the longest prefix of the string whose reverse is a valid suffix of the string. For example, if "notation" is given as input, the prefix "no" is the longest prefix that has a matching valid suffix (namely "on") at the end.

If a string is a palindrome (i.e. if the string is the same as its reverse), then the prefix is the whole string itself. For example, if input is "civic", then the longest prefix is "civic" itself. If the string has no valid prefix, print 0. For example, if the input string is "bird", there is a mismatch at the first character itself and there is no valid prefix.

The symbols in the string will only be from the set {A-Z,a-z}. The symbols are case sensitive. i.e. ‘A’ and ‘a’ are considered to be different.

You are provided with a function called printChars ( ) that prints a character array from the starting position pointed to by p to the ending position pointed to by q. If p is NULL, it prints 0.

The prototype of the function is:

void printChars(char *p, char *q);

You do not have to write the program for printChars ( ) function. This function is automatically added at the end of the code segment that you write.

You can use string functions in the library if you desire to.

INPUT:
Input is a string of length N composed of symbols only from the set {A-Za,z}.

OUTPUT:
The longest prefix that has a corresponding matching suffix and 0 if such a prefix does not exist.

CONSTRAINTS:
The inputs will satisfy the following properties. It is not necessary to validate the inputs.

1<=N<=99


Evaluating a Polynomial (Programming Assignment )

You are given a polynomial of degree n. The polynomial is of the form P(x) = anxn + an-1xn-1 + … + a0, where the ai‘s are the coefficients.  Given an integer x, write a program that will evaluate P(x).


You are provided with a function named power( ) that takes two positive integers x & y and returns xy. If y is 0, the function returns 1.


The prototype of this function is


int power(int x, int y);
You do not have to write the program for power ( ) function. This function is automatically added at the end of the code segment that you write.
INPUT: Line 1 contains the integers n and x separated by whitespace.

Line 2 contains the coefficients an, an-1…, a0 separated by whitespace.


OUTPUT: A single integer which is P(x).
CONSTRAINTS: The inputs will satisfy the following properties. It is not necessary to validate the inputs.
1 <= n <= 10 1 <= x <= 10
0 <= ai <=10


Solution-:

Bronze medallist (Programming Assignment )

Your college is organising a sporting event. The events are all done and now the task is to prepare the list of medal winners. The sports secretary for some reason assumed that the events will only have Gold and Silver medals but no Bronze medals. The contestants, especially the 3rd place winners were furious (understandably so!). To get things in control the secretary gives you the list of participants in an event and their standing in the event. Your task is to output the list of Bronze medalists in each of the events.


Each event has 10 participants.
Input:  The number of test cases T followed by T lines. Each line has 10 numbers that indicate the time taken by each contestant to complete the task. Each task takes less than or equal to 30 seconds. If there are two bronze winners, print the one with the lower id.
Output: The id of the contestant who secured the Bronze medal.


Example:
Input
1
16 12 34 11 10 5 3 1 15 21
Output
6

Permutations and Combinations (Programming Assignment)

Write functions to calculate factorial, nCr and nPr and using those functions perform a custom computation.

nPr is the n
umber of all permutations of n things, taken r at a time. nCr is the n
umber of all combinations of n things, taken r at a time.


Your task is to write three functions fact( ), ncr( ) and npr( ). The function definitions are given for your reference.

int fact(int n): This function should return the factorial of the parameter n. This function can be used to calculate nCr and nPr.
int npr(int n, int r): The function should return the value nPr where n and r as input parameters.
int ncr(int n, int r): The function should return the value nCr where n and r as input parameters.

You should not write the main function as the main function is prefixed. The functions you write should match the prototype in prefixed code. The output is printed in the main function. So NO output should be printed by any of the functions you write.