1 December 2014

Quiz 57: Dice game

Problem:

Rohit, Neelabh and Priya are planning a holiday trip together, but all of them wants to go to different destinations.They decided to roll a regular dice and the person having highest score wins and all will go to his/her destination for holiday. First Rohit and Neelabh play their dice. Priya now wants to calculate chances of her win. If Priya got maximum score and it is a tie with neelabh or Rohit then too Priya will win. Print in form m/n where m and n cannot be furthur divided ie 3/6 should be 1/2

Input Format: 

T=number of test cases
A B(score of Rohit and Neelabh)

Output Format: 

N lines containing probability


Constraints: 

None

Sample Input

6
1 1
2 1
2 3
4 2
4 5
6 3

Sample Output:

1/1
5/6
2/3
1/2
1/3
1/6

Explanations:

for case 1. whatever output Priya gets she will win. So ans is 6/6 ie 1/1


Solution:

chomp($t=<STDIN>);
for($i=0;$i<$t;$i++)
{
chomp($in=<STDIN>);
$in=~ / /;
$a=$`;
$b=$';
if($a<$b){$a=$b;}
@arr=('0/0','1/1','5/6','2/3','1/2','1/3','1/6');
push(@out,$arr[$a]);
}
foreach(@out)
{
print "$_\n";
}



Tips:

better to use arrays if number of output are limited.

27 November 2014

Quiz 56:Make the text center align

Problem:

You are give N words. Length of each word<=10. You need to make them center aligned keeping max length as 10 ie a 6 letter word should be changed to 10 letter word by adding 2 spaces to right and 2 to left. If length is odd, then keep the word towards left.

Input Format: 

N=number of words
N lines containing a word

Output Format: 

N lines containing center aligned word


Constraints: 

None

Sample Input

10
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
aaaaaaaaa
aaaaaaaaaa

Sample Output:

    a
    aa
   aaa
   aaaa
  aaaaa
  aaaaaa
 aaaaaaa
 aaaaaaaa
aaaaaaaaa
aaaaaaaaaa

Explanations:

O/P is center aligned


Solution:

chomp($n=<STDIN>);
for($i=0;$i<$n;$i++)
{
chomp($in=<STDIN>);
@arr=split(//,$in);
$len=@arr;
if($len%2==0)
{
$space_r=(10-$len)/2;
$space_l=$space_r;
}
else
{
$space_r=(10-$len+1)/2;
$space_l=$space_r-1;
}
$ans=(' ' x ($space_l).$in.' ' x ($space_r));
push(@out,$ans);
}
foreach(@out)
{
print "$_\n";
}



Tips:

' ' is used to insert space

Quiz 55: Find outgoing traffic of chat server

Problem:

We have a chat application.
Allowed operations are:
1> Adding a person to chat group, format +priyanka
2> Removing a person to chat group, format -sonali
3> Sending a message to chat group, format priyanka:Hi
Now, no traffic is sent to server for adding or removing person. But for every sent message, K bytes(length of message) for each person present in group are sent to server. Like if only Priyanka is there is a group, then Priyanka:Hello will send 5 bytes. If 2 persons are there in group, Priyanka:Hello will send 5x2=10 bytes.
Find total bytes sent to server.
Note: All input data is correct. A person cannot be added if it is already there in group. A person cannot be removed if he/she is not there is group. No two persons have same name.

Input Format: 

Instructions in each line:
+Mohan
+shyam
-shyam
Mohan:Hello how r u


Output Format: 

total bytes sent to server


Constraints: 

None

Sample Input

+priya
+shikha
priya:hey shikha, how r u
shikha:fine, thanks
shikha:lets add sonali
+sonali
shikha:hi sonali
sonali:shikha, personal talks, remove priya
-priya
shikha:tell me now

Sample Output:

249

Explanations:

92(when 2 people in chat) + 135(when 3 people in chat) + 22(when again 2 people). total=249


Solution:

$c=0;
$ans=0;
my $input;
while($input=<>) 
{
@arr=split(//,$input);
if($arr[0] eq '+'){$c++;next;}
if($arr[0] eq '-'){$c--;next;}
$len=@arr;
$k=$len;
for($i=0;$i<$k;$i++)
{
$len--;
if($arr[$i] eq ':')
{
$len--;
$ans=$ans+($len*$c);
last;
}
}
}
print $ans;



Tips:

No use to maintain HASH for different names since we are just concerned about number of people in chat, not the names of person.

Quiz 54: Change duplicate names by appending with number

Problem:
There is a list of N students with their first names. Now if 2 or more students have same name, you need to change 2nd name to "name1", 3rd name to "name2" and so on. If there is only 1 occurrence of particular name, then it will remain as it is.

Input Format: 
N(number of students)
N lines with 1 name each

Output Format: 
n lines with modified names

Constraints: 
None

Sample Input
10
priya
satyam
shikha
sid
amit
shikha
amit
amit
amit
satyam

Sample Output:
priya
satyam
shikha
sid
amit
shikha1
amit1
amit2
amit3
satyam1

Explanations:
As soon as name "shikha" came for 2nd time, change it to "shikha1" and so on.


Solution:

chomp($n=<STDIN>);
%count=();
for($i=0;$i<$n;$i++) 
{
chomp($name=<STDIN>);
if($count{$name}) 
{
$name2=$name . $count{$name};
$count{$name}++;
push(@out,$name2);

else 
{
$count{$name}=1;
push(@out,$name);
}
}
foreach(@out)
{
print "$_\n";
}



Tips:
use of PERL hashes

Quiz 53: Find minimum number of moves by King to reach destination

Problem:
In a chess board, only 1 King is left. Given his current position and his destination position, find number of minimum moves, in which king can reach his destinations.
Valid moves are same as that of King in Chess Game.


Input Format: 
T(number of test cases)
Next T lines having initial position and destination position like a1 d7

Output Format: 
T lines having minimum number of moves

Constraints: 
None

Sample Input
5
a1 h8
e4 e4
c2 d5
e7 d2
h2 b1

Sample Output:
7
0
3
5
6

Explanations:
for c2 d5, moves can be c2-d3, d3-d4, d4-d5. So 3 moves minimum


Solution:

chomp($t=<STDIN>);
for($i=0;$i<$t;$i++)
{
chomp($line=<STDIN>);
@arr=split(" ",$line);
$arr[0]=~ /(\w)(\d*)/;
$c1=ord($1);
$r1=$2;
$arr[1]=~ /(\w)(\d*)/;
$c2=ord($1);
$r2=$2;
if($c1>$c2)
{
$d1=$c1-$c2;
}
else
{
$d1=$c2-$c1;
}
if($r1>$r2)
{
$d2=$r1-$r2;
}
else
{
$d2=$r2-$r1;
}
if($d1>$d2)
{
push(@out,$d1);
}
else
{
push(@out,$d2);
}
}
foreach(@out)
{
print "$_\n";
}


Tips:
Maximum (diff of rows, diff of columns)

26 November 2014

Quiz 52: Find winner of card game

Problem:
Few friends are playing cards. There play N rounds. Winner of each round is in format "name points". Points of all other players for that round is 0. Final Winner is the person with maximum total points of all rounds. It is guaranteed that no 2 players will have same maximum total after N rounds. Find the name of person and his/her total score

Input Format: 
N(number of rounds)
N lines in format "name points"

Output Format: 
Name Total_points

Constraints: 
None

Sample Input
10
priya 10
satyam 7
shikha 6
sid 11
amit 7
shikha 6
amit 1
amit 1
amit 2
satyam 4

Sample Output:
shikha 12

Explanations:
Shikha have 6+6=12 points, which are maximum.


Solution:

chomp($t=<STDIN>);
for($i=0;$i<$t;$i++)
{
chomp($line=<STDIN>);
push(@arr,$line);
}
foreach(@arr){
($a,$b)=split/ /;
$n{$a}+=$b;
}
$max= (sort {$b<=>$a} values %n)[0];
for(@arr){
($a,$b)=split/ /;
$m{$a}+=$b;
if ($max == $n{$a}) {$ans=$a; last }
}
print "$ans $max";


Tips:
use of PERL hashes

Quiz 51: Find titles required to cover the ground

Problem:
Given a rectangular ground of dimension X by Y. And we have square tiles of size A by A. Find minimum number of tiles required to cover the ground.
Note: Tiles cannot be broken, it is OK if titles crosses rectangle boundary.

Input Format: 
X Y A

Output Format: 
Number of tiles

Constraints: 
None

Sample Input
8 10 4

Sample Output:
6

Explanations:
8 x 8 will be covered by 4 titles of 4 x 4. Remaining area 8 x 2 will be covered by another 2 tiles. So total 6 tiles.


Solution:

chomp($line=<STDIN>);
($x,$y,$p)=split(" ",$line);
if($x%$p==0)
{
$tmp1=$x/$p;
}
else
{
$tmp1=int($x/$p)+1;
}
if($y%$p==0)
{
$tmp2=$y/$p;
}
else
{
$tmp2=int($y/$p)+1;
}
$ans=$tmp1*$tmp2;
print $ans;


Tips:
check x, y for divisibility by a and code accordingly