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
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
followed by n sentences in different lines
Output Format:
print corresponding number for each line
print corresponding number for each line
Constraints:
none
none
Sample Input
4
i live in india
india is the best
india is india
in india people are good
i live in india
india is the best
india is india
in india people are good
Sample Output:
2
1
0
-1
2
1
0
-1
Explanations:
for 1st case, sentence ends with 'india', so print 2
for 1st case, sentence ends with 'india', so print 2
Solution:
for($i=0;$i<$n;$i++)
{
chomp($s=<STDIN>);
@arr=split(/ /,$s);
$l=@arr;
if($arr[$l-1] =~ /india/ and $arr[0] =~ /india/)
{
push(@out,0);
}
elsif($arr[0] =~ /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
No comments:
Post a Comment