## 28 October 2014

### Quiz 43: Find TAG and attributes of HTML code

Problem:
Given a  HTML line, find the tag and its attributes.
ex Name:<input type="text">, here tag is 'input' and attribute is 'type'

Input Format:
included in code

Output Format:
Attributes for tag a are href & name & id

Constraints:
none

Sample Input

Sample Output:
Attributes for tag a are href & name & id

Explanations:
tag is a and others are attributes

Solution:

(\$a)=\$n=~/<(.*?)\s/;
(@arr)=\$n=~/\s(.*?)=/g;
print "attributes of tag \$a are ";
\$c=0;
foreach(@arr)
{
if(\$c>0){print "& "};
print "\$_ ";
\$c++;
}

Tips:
Tag is anything between < and space. attribute is anything between space and =

### Quiz 42: Valid PAN card number verification

Problem:
Given a PAN card number, find whether number is valid or not.
where UC is upper case alphabet and D is digit

Input Format:
n = no. of test cases
followed by n pancard numbers in different lines

Output Format:
print "YES" for valid and "NO" for invalid

Constraints:
none

Sample Input
3
ABCDE1234F
ABCDe1234F
ABCDE12345

Sample Output:
YES
NO
NO

Explanations:
for case 2, it contains lower case alphabet, so number is invalid

Solution:

chomp(\$n=<STDIN>);
for(\$i=0;\$i<\$n;\$i++)
{
chomp(\$s=<STDIN>);
if(\$s =~ /[A-Z][A-Z][A-Z][A-Z][A-Z]\d\d\d\d[A-Z]/)
{
push(@out,"YES");
}
else
{
push(@out,"NO");
}
}
foreach(@out)
{
print "\$_\n";
}

Tips:
[A-Z] means any upper case alphabet

### Quiz 41: Find whether a phone number is valid or not

Problem:
Given a phone number, find whether number is valid or not.
Rule1: number should have 10 digits
Rule2: only numbers allowed

Input Format:
n = no. of test cases
followed by n numbers in different lines

Output Format:
print corresponding error for each line. print "Valid number" for valid number

Constraints:
none

Sample Input
3
9876543210
999999999
9876543w21

Sample Output:
valid phone number
Phone number should have length 10
Phone number should have numbers only

Explanations:
for case 2, length is 9, so number is invalid

Solution:

chomp(\$n=<STDIN>);
for(\$i=0;\$i<\$n;\$i++)
{
chomp(\$s=<STDIN>);
@arr=split(//,\$s);
\$l=@arr;
\$c=0;
if(\$l != 10)
{
push(@out,"Phone number should have length 10");
\$c++;
}
foreach(@arr){
if(\$_ !~ /\d/)
{
push(@out,"Phone number should have numbers only");
\$c++;
last;
}}
if(\$c == 0)
{
push(@out,"valid phone number");
}
}
foreach(@out)
{
print "\$_\n";
}

Tips:
\d is used to match digits only. [0-9] can also be used.

### Quiz 40: Find matching regex at 1st and last psoition

Problem:
Given a 1 line sentence, find the term "india" and display result as following
print 1 if sentence starts with "india"
print 2 if sentence ends with "india"
print 0 if sentence starts and ends with "india"
else print -1

Input Format:
n = no. of test cases
followed by n sentences in different lines

Output Format:
print corresponding number for each line

Constraints:
none

Sample Input
4
i live in india
india is the best
india is india
in india people are good

Sample Output:
2
1
0
-1

Explanations:
for 1st case, sentence ends with 'india', so print 2

Solution:

chomp(\$n=<STDIN>);
for(\$i=0;\$i<\$n;\$i++)
{
chomp(\$s=<STDIN>);
@arr=split(/ /,\$s);
\$l=@arr;
if(\$arr[\$l-1] =~ /india/ and \$arr =~ /india/)
{
push(@out,0);
}
elsif(\$arr =~ /india/)
{
push(@out,1);
}
elsif(\$arr[\$l-1] =~ /india/)
{
push(@out,2);
}
else
{
push(@out,-1)
}
}
foreach(@out)
{
print "\$_\n";
}

Tips:
=~  /expression/ is used to match regex

## 16 October 2014

### Quiz 39: Find index of flowers

Problem:
Mohit wants to buy 2 flowers for pooja. He have m rupees and flower shop have n different flowers. Given price of each flower, find the indexes of 2 flowers such that all the money Mohit have is used.

Input Format:
m = money
n = number of different flowers
cost of n flowers separated by space.

Output Format:
index1 index2

Constraints:
none

Sample Input
200
10
1 5 15 25 198 14 200 2 100 150

Sample Output:
5 8

Explanations:
198+2=200, index of 198 is 5 and index of 2 is 8

Solution:

chomp(\$m=<STDIN>);
chomp(\$n=<STDIN>);
chomp(\$l=<STDIN>);
@list=split(/ /,\$l);
for(\$j=0;\$j<\$n;\$j++)
{
for(\$k=\$j+1;\$k<\$n;\$k++)
{
if(\$m == \$list[\$j]+\$list[\$k])
{
print \$j+1;
print " ";
print \$k+1;
last;
}
}
}

Tips:
Use last to end the loop as soon as your job is done

### Quiz 38: Find the minimum price of a product

Problem:
Given price offered for a product by N different companies. Find the minimum price and name of company

Input Format:
n = no of offers
followed by n lines in format "price company name" ie separated by space.

Output Format:
Minimum price is __ by ___

Constraints:
none

Sample Input
5
17000 snapdeal
21000 amazon
16322 ebay
16321 flipkart
32000 homeshop18

Sample Output:
Minimum cost is 16321 by flipkart

Explanations:
Claerly minimum cost is 16321

Solution:

chomp(\$n=<STDIN>);
@data=();
for(\$i=0;\$i<\$n;\$i++)
{
chomp(\$inp=<STDIN>);
@arr=split(" ",\$inp);
push(@data,\$arr);
push(@data,\$arr);
}
%hash=@data;
@names = keys %hash;
@names = sort{\$a<=>\$b}@names;
\$min = \$names;
print "Minimum cost is \$min by ";
print "\$hash{\$min}";

Tips:
Using HASH is best approach for such problems

## 3 October 2014

### Quiz 37:Find LCM of multiple numbers

Problem:
Take N numbers as input and print their LCM

Input Format:
num1 num2 num3......ie numbers seperated by space

Output Format:
LCM

Constraints:
none

Sample Input
20 30 40 50

Sample Output:
LCM is 600

Explanations:
600 is LCM of 20.30.40.50

Solution:

sub gcf {
my (\$x, \$y) = @_;
(\$x, \$y) = (\$y, \$x % \$y) while \$y;
return \$x;
}

sub lcm {
return(\$_ * \$_ / gcf(\$_, \$_));
}

sub multilcm {
my \$x = shift;
\$x = lcm(\$x, shift) while @_;
return \$x;
}

chomp(\$num=<STDIN>);
@arr=split(" ",\$num);
\$ans=multilcm(@arr);
print "LCM is \$ans";

Tips:
Find LCM of 2 numbers at a time, say lcm1. In next cycle, find lcm of lcm1 and next number and so on.

### Quiz 36: Find GCD of multiple numbers

Problem:
Take N numbers as input and print their GCD

Input Format:
num1 num2 num3......ie numbers seperated by space

Output Format:
GCD

Constraints:
none

Sample Input
6 15 9 30

Sample Output:
GCD is 3

Explanations:
3 is GCD of 6,15,9,30

Solution:

sub gcf {
my (\$x, \$y) = @_;
(\$x, \$y) = (\$y, \$x % \$y) while \$y;
return \$x;
}

sub multigcf {
my \$x = shift;
\$x = gcf(\$x, shift) while @_;
return \$x;
}

chomp(\$num=<STDIN>);
@arr=split(" ",\$num);
\$ans=multigcf(@arr);
print "GCD is \$ans";

Tips:
Find GCD of 2 numbers at a time, say gcd1. In next cycle, find gcd of gcd1 and next number and so on.

### Quiz 35: Find LCM of 2 numbers

Problem:
Take 2 numbers as input and print their LCM

Input Format:
number 1
number 2

Output Format:
LCM

Constraints:
none

Sample Input
15
20

Sample Output:
LCM is 60

Explanations:
60 is LCM of 15 and 20

Solution:

chomp(\$x=<STDIN>);
chomp(\$y=<STDIN>);
(\$a,\$b) =(\$x,\$y);
while(\$b)
{
(\$a, \$b) = (\$b, \$a % \$b)
}
\$gcd=\$a;
\$lcm=(\$x*\$y)/\$gcd;
print "LCM is \$lcm";

Tips:
LCM= product of numbers/GCD

### Quiz 34: Find GCD of 2 numbers

Problem:
Take 2 numbers as input and print their GCD

Input Format:
number 1
number 2

Output Format:
GCD

Constraints:
none

Sample Input
6
9

Sample Output:
GCD is 3

Explanations:
3 is GCD of 6 and 9

Solution:

chomp(\$a=<STDIN>);
chomp(\$b=<STDIN>);
while(\$b)
{
(\$a, \$b) = (\$b, \$a % \$b)
}
print "GCD is \$a";

Tips:
Learn the logic of GCD