Saturday, 15 August 2015

TCS Codevita : Hotel trip

roblem : Hotel trip
Tom, Dick and Harry go to a restaurant. Each of them has a certain amount of money. When their monies are pooled they discover they have S Rs. Next, they start going through the menu card to choose items they would like to order.

Your task is to help them find out how many different menu item combinations they can order. Since there can be many menu items with the same price, we are interested only in unique price combinations. Refer output specifications and examples to get a better understanding. 
Input Format:

First line contains N positive integers delimited by space, corresponding to prices of N menu items
Second line contains an integer S corresponding to the amount of money pooled between them 
Output Format: 
1.    Print all possible combinations of item prices whose sum is equal to S. Enclose each such combination in square brackets [ ]. Elements should be separated by, followed by a space character.
2.    Item price combinations must be unique (See example 2 to understand this better)
3.    If more than one combinations exists, then the print order should be as follows 
a.     Combinations should be printed in descending order item prices
b.    Two combinations can be differentiated by first comparing their costliest, followed by second costliest, and followed by the next costliest item, so on and so forth. Refer example outputs for better understanding of print order.


Constraints:
Pi > 0 ; where i = 1 to N and Pi denotes the price of the ith item
S > 0
Sample Input and Output
SNo.
Input
Output
1

10 7 6 5 3 2 1
23

[10, 7, 6]
[10, 7, 5, 1]
[10, 7, 3, 2, 1]
[10, 6, 5, 2]
[7, 6, 5, 3, 2]
2

50 100 1 1 1 1 1
51

[50, 1]
3

50 100 4 3 1 1 1 1
54

[50, 4]
[50, 3, 1]
[50, 1, 1, 1, 1]

Explanation:

In example 1 there are 7 items on the menu each having a fixed price. Tom, Dick and Harry have 23 Rs/- amongst them. So they can order the following combinations 
  1. [10, 7, 6]
  2. [10, 7, 5, 1]
  3. [10, 7, 3, 2, 1]
  4. [10, 6, 5, 2]
  5. [7, 6, 5, 3, 2]
Note how the items are sorted in descending order within a combination. Also note how the combinations themselves are too sorted in descending order.

In example 2 there are 7 items. 5 of these 7 items have the same price. Tom, Dick and Harry have 51 Rs/- amongst them. So the only unique price combination they can order is [50, 1]

In example 3 there are 8 items. 4 of these 8 items have the same price. Tom, Dick and Harry have 54 Rs/- amongst them. So they can form the following combinations. 
  1. [50, 4]
  2. [50, 3, 1]
  3. [50, 1, 1, 1, 1]

Again, note how the items are sorted in descending order within a combination. Also note how the combinations themselves are too sorted in descending order. Also combinations 2 and 3 are printed only once. 

Solution in Python :

tt = input().split()
pm = int(input())

comb = list()
for ind in range(0,len(tt)):
tt[ind] = int(tt[ind])

def subset_sum(numbers, target, partial=[]):
    s = sum(partial)
    if s == target:
        comb.append(partial)
    if s >= target:
        return
    for ind in range(len(numbers)):
        n = numbers[ind]
        remaining = numbers[ind+1:]
        subset_sum(remaining, target, partial + [n])

subset_sum(tt,pm)

unique = []
for item in comb:
    if sorted(item) not in unique:unique.append(sorted(item))

for items in unique:
    print(items[::-1])


        

TCS Codevita Sheldon Cooper and his beverage paradigm

Problem : Sheldon Cooper and his beverage paradigm
Sheldon Cooper, Leonard Hofstadter and Penny decide to go for drinks at Cheese cake factory. Sheldon proposes to make a game out of this. Sheldon proposes as follows, 
  • To decide the amount of beverage they plan to consume, say X.
  • Then order for a random number of different drinks, say {A, B, C, D, E, F} of quantities {a, b, c, d, e, f} respectively.
  • If quantity of any three drinks add up to X then we'll have it else we'll return the order.
    E.g. If a + d + f = X then True else False

You are given
1.    Number of bottles N corresponding to different beverages and hence their sizes
2.    Next N lines, contain a positive integer corresponding to the size of the beverage
3.    Last line consists of an integer value, denoted by X above
Your task is to help find out if there can be any combination of three beverage sizes that can sum up to the quantity they intend to consume. If such a combination is possible print True else False 


Input Format: 
1.    First line contains number of bottles ordered denoted by N
2.    Next N lines, contains a positive integer Ai, the size of the ith bottle
3.    Last line contains the quantity they intend to consume denoted by X in text above



Output Format:
True, if combination is possible
False, if combination is not possible 
Constraints:
N >= 3
Ai > 0
1 <= i <= N
X > 0 



Sample Input and Output

SNo.
Input
Output
1

6
1
4
45
6
10
8
22

True
2

4
1
3
12
4
14

False





Solution in Java:


import java.util.Scanner;
import java.util.Stack;
public class Shell
{
private Stack<Integer> stack = new Stack<Integer>();
   private int sumin = 0,sizeStack=0;
   int c=0;
   public void populateSubset(int[] data, int fromIndex, int last,int t)
   {
   
    int TARGET_SUM=t;
       if (sumin == TARGET_SUM) 
       {
        if(sizeStack==3)
            {
         count(stack);
            }
       }

       for (int now = fromIndex; now < last; now++) {

           if (sumin + data[now] <= TARGET_SUM) {
               stack.push(data[now]);
               sumin += data[now];
               sizeStack=stack.size();
               populateSubset(data, now + 1, last,TARGET_SUM);
               sumin -= (Integer) stack.pop();
           }
       }
   }
   private void count(Stack<Integer> stack)
   {
    c++;
   }
   void print()
   {
    if(c>0)
        System.out.println("True");
    else
    System.out.println("False");
   }
   public static void main(String [] args)
   {
       Shell get = new Shell();
       int t1,n1,i;
       Scanner sc=new Scanner(System.in);
       n1=sc.nextInt();
       int []ip=new int[n1];
       for(i=0;i<n1;i++)
       {
           ip[i]=sc.nextInt();
       }
       t1=sc.nextInt();
       get.populateSubset(ip, 0, ip.length,t1);
      get.print();
   }

}



TCS CodeVita Milk Man Problem

Problem : Milk Man and His Bottles
A Milkman serves milk in packaged bottles of varied sizes. The possible size of the bottles are {1, 5, 7 and 10} litres. He wants to supply desired quantity using as less bottles as possible irrespective of the size. Your objective is to help him find the minimum number of bottles required to supply the given demand of milk. 
Input Format:

First line contains number of test cases N
Next N lines, each contain a positive integer Li which corresponds to the demand of milk. 
Output Format:

For each input Li, print the minimum number of bottles required to fulfill the demand 
Constraints:
1 <= N <= 1000
Li > 0
1 <= i <= N
Sample Input and Output
SNo.
Input
Output
1

2
17
65

2
7

Explanation:

Number of test cases is 2 
  1. In first test case, demand of milk is 17 litres which can be supplied using minimum of 2 bottles as follows
    • 1 x 10 litres and
    • 1 x 7 litres
  2. In second test case, demand of milk is 65 litres which can be supplied using minimum of 7 bottles as follows
    • 6 x 10 litres and
    • 1 x 5 litres

 Solution in C :

#include<stdio.h>
int main()
{
int c,i,cnt,array[1000],j;

scanf("%d",&c);
if(c>=1&&c<=1000)
{

for(j=0;j<c;j++)
{
    scanf("%d",&i);
    cnt=0;
if(i>0)
{
while(i!=0)
{
    if((i==12||i==13||i==19||i==14))
    {
        cnt--;
        if(i==14)
        {
            cnt=cnt-2;
        }

    }
        if(i>=10)
{
    cnt++;
    i=i-10;
}
else if(i>=7)
{
    cnt++;
    i=i-7;
}
else if(i>=5)
{
    cnt++;
    i=i-5;
}
else if(i>=1)
{
    cnt++;
    i=i-1;
}
}

printf("%d\n",cnt);
}
}
}
return 0;
}

Solution in Java :


import java.util.*;
public class bottle{

private static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
int n,v,u,m,m1,s,s1,i,v1,w1;
int l[]=new int[9999];
try
{
n=in.nextInt();
if((n>=1)&&(n<=1000))
{
for(i=1;i<=n;i++)
{
if((i>=1)&&(i<=n))
{
l[i]=in.nextInt();
}
}
for(i=1;i<=n;i++)
{
if(l[i]>0)
{

if(l[i]<=9)
{
if((l[i]==1)||(l[i]==5)||(l[i]==7))
{
int a1=1;
System.out.println(a1);
}
if((l[i]==2)||(l[i]==6)||(l[i]==8))
{
int a2=2;
System.out.println(a2);
}
if((l[i]==3)||(l[i]==9))
{
int a3=3;
System.out.println(a3);

}
if(l[i]==4)
{
int a4=4;
System.out.println(a4);
}
}
int z=l[i]/10;
v=z-1;
u=l[i]%10;
if(l[i]>=10)
{
if(u==0)
{
System.out.println(z);
}
if((u==1)||(u==5)||(u==7))
{
v1=z+1;
System.out.println(v1);
}
if((u==2)||(u==4))
{
m=2;
m1=v+m;
System.out.println(m1);
}
if((u==3)||(u==9))
{
s=3;
s1=v+s;
System.out.println(s1);
}
if((u==6)||(u==8))
{
int k1=z+2;
System.out.println(k1);
}
}
}
}
}

}
catch(Exception e)
{

}

}

}


TCS Codevita Catch-22

Problem : Catch-22
A robot is programmed to move forward F meters and backwards again, say B meters, in a straight line. The Robot covers 1 meter in T units of time. On Robot's path there is a ditch at a distance FD from initial position in forward direction as well as a ditch at a distance BD from initial position in backward direction. This forward and backward movement is performed repeatedly by the Robot.

Your task is to calculate amount of time taken, before the Robot falls in either ditch, if at all it falls in a ditch. 
Input Format:

First line contains total number of test cases, denoted by N
Next N lines, contain a tuple containing 5 values delimited by space
F B T FD BD, where
1.    F denotes forward displacement in meters
2.    B denotes backward displacement in meters
3.    T denotes time taken to cover 1 meter
4.    FD denotes distance from Robot's starting position and the ditch in forward direction
5.    BD denotes distance from Robot's starting position and the ditch in backward direction


Output Format:

For each test case print time taken by the Robot to fall in the ditch and also state which ditch he falls into. Print F for forward and B for backward. Both the outputs must be delimited by whitespace 
OR

Print No Ditch if the Robot does not fall in either ditch 
Constraints:
First move will always be in forward direction
1 <= N <= 100
forward displacement > 0
backward displacement > 0
time > 0 
distance of ditch in forward direction (FD) > 0
distance of ditch in backward direction (BD) > 0
All input values must be positive integers only

Sample Input and Output
SNo.
Input
Output
1

3
9 4 3 13 10
9 7 1 11 13
4 4 3 8 12

63 F
25 F
No Ditch
2

5
8 4 7 11 22
4 5 4 25 6
4 9 3 6 29
7 10 6 24 12
10 10 1 9 7

133 F
216 B
231 B
408 B
9 F

Solution in C++:

#include<iostream>
#define cout std::cout
#define cin std::cin
//using namespace std;

int main()
{
int  var1;
cin >> var1;
while (var1--)
{
long long int front, back, tunit, fd, bd;
long long int tcount=0,count=1,dish=0;
cin >> front >> back >> tunit >> fd >> bd;

if (front == back)
{
if (fd > front)
cout << "No Ditch\n";
else
cout << fd *tunit << " F\n";
}
else if (front>back || (back>front && fd<=front))
{
while (count)
{
if (!(dish++ % 2))
{
if (front < fd)
{
fd = fd - front;
tcount = tcount + front;
}
else
{
tcount = tcount + fd;
flag = 0;
}
}
else
{
fd = fd + back;
tcount = tcount + back;
}
}
cout << tcount*tunit<<" F\n";
}
else if (back>front)
{
while (count)
{
if ((dish++ % 2))
{
if (back < bd)
{
bd = bd - back;
tcount = tcount + back;
}
else
{
tcount = tcount + bd;
flag = 0;
}
}
else
{
bd = bd + front;
tcount = tcount + front;
}
}
cout << tcount*tunit << " B\n";
}
}
return 0;
}