15 August 2014

Quiz 11: Service lane problem

Problem:
There is a Highway 20 km long. Along the highway, there are 20 service lanes numbered 1-20 and each service lane is 1 km long. Width of service lane can be either 1,2 or 3.
Our data of width is in form of array: 1 2 2 2 3 3 3 1 1 2 2 3 3 3 1 1 2 2 3 3
ie service lane 1 have width 1 and service lane 20 have width 3.
Now a vehicle can exit highway from any service lane and enter highway again from any lane, but we have a width constraint:
-- if width = 1, only bike can enter/exit that service lane.
-- if width = 2, bike or car can enter/exit that service lane.
-- if width = 3, bike, car or truck can enter/exit that service lane.

Input: Entering service lane and exiting service lane
Output: which type of vehicle can pass thru it

Sample Input:
4 6

Sample Output:
Only bike or car can enter by service lane 4 and exit by service lane 6

Explanations:- width of service lane 4,5,6 are 2,3,3--> so truck cannot enter by service lane 4

Solution:

@arr2 = qw(1 2 2 2 3 3 3 1 1 2 2 3 3 3 1 1 2 2 3 3);
@arr4 = ();
chomp($str3 =<STDIN>);
@arr3 = split(" ",$str3);
if($arr3[0]<1 or $arr3[1]<=$arr3[0] or 20<$arr3[1])
{
exit;
}
    for($j=$arr3[0]-1;$j<=$arr3[1]-1;$j++)
    {
    unshift(@arr4,$arr2[$j]);
    }
@arr4 = sort(@arr4);
if ($arr4[0] == 1)
{
print "Only Bike can enter from service lane $arr3[0] and exit from service lane $arr3[1]";
}
elsif ($arr4[0] == 2)
{
print "Only Bike and Car can enter from service lane $arr3[0] and exit from service lane $arr3[1]";
}
else
{
print "Bike,Car or Truck can enter from service lane $arr3[0] and exit from service lane $arr3[1]";
}


Tips:
  • if input is 4 6, solution is the minimum value of width of service lane 4 to 6.

2 comments:

  1. use strict;
    use warnings;
    my @a=(1,2,2,2,3,3,3,1,1,2,2,3,3,3,1,1,2,2,3,3);
    print "\nenter the entry lane";
    my $entry=<>;
    print "\nenter the exit lane";
    my $exit=<>;
    my $e=$a[$entry];
    my $ex=$a[$exit];
    if($e==1)
    {print "only bike can enter and exit in this combination of lanes\n";}
    elsif($e==2 && $ex==2)
    {print "bike and car can enter and exit in this combination of lanes\n";}
    elsif($e==2 && $ex==1)
    {print "only bike can enter and exit in this combination of lanes\n";}
    elsif($e==2 && $ex==3)
    {print "bike and car in this combination of lanes\n";}
    elsif($e==3 && $ex==3)
    {print "bike car and truck in this combination of lanes\n";}
    elsif($e==3 && $ex==2)
    {print "bike and car in this combination of lanes\n";}
    else
    {print "only bike in this combination of lanes\n";}

    ReplyDelete
    Replies
    1. for entry 4 and exit 6, your solution also allow truck, which is incorrect

      Delete