Ittiam Systems Interview Question
Software TraineesTeam: dsfgfds
Country: India
Interview Type: Written Test
For the question-Input ip address. Check if it is in the range 172.125.1 and 172.125.25
#!/usr/bin/perl
print "Content-type:text/html\n\n";
my $ipaddr= "172.125.24.255";
chomp($ipaddr);
my $pattern='^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$';
if($ipaddr=~m/$pattern/)
{
if (($1<172 || $2<125 || $3<1 || $4<0) || ($1>172 || $2>125 || $3>25 || $4>0))
{
print "$ipaddr is not in the range between IP Address 172.125.1.0 and 172.125.25.0";
}
else
{
print "$ipaddr is an valid IP Address";
}
}
else
{
print "$ipaddr is an Invalid IP Address";
}
I am getting the output as valid when input ip address is 172.125.25.0. But output is invalid when the input is 172.125.24.255. How to solve this? Please help
#!/usr/bin/perl -w
use strict;
#Task 1: Input IP Address. Check if it lies between 172.125.1.0 and 172.125.25.0?
my $ip_add = $ARGV[0];
if($ip_add =~ m/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ && ($1>171 && $1<173)
&& ($2>124 && $2<126) && ($3>0 && $3<25) && ($4>=0 && $4<256) || $ip_add =~ m/172.125.25.0/)
{
print "Valid IP Address\n";
}
else
{
print "Non-Valid IP Address\n";
}
#!/usr/bin/perl -w
use strict;
#Task 1: Input IP Address. Check if it lies between 172.125.1.0 and 172.125.25.0?
my $ip_add = $ARGV[0];
if($ip_add =~ m/^172\.125\.(\d{1,3})\.(\d{1,3})$/ && ($1>0 && $1<25) && ($2>=0 && $2<256) || $ip_add =~ m/172.125.25.0/)
{
print "Valid IP Address\n";
}
else
{
print "Non-Valid IP Address\n";
}
this is python. but re should be the same. wow the re is long... any better solution?
- gnahzy August 29, 2012regex = re.compile("^172\.125\.((25\.0)|((([1-9])|(1[0-9])|(2[0-4]))\.(([0-
])|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))))$")