-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-1.pl
executable file
·36 lines (29 loc) · 936 Bytes
/
ch-1.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
sub main {
my @numbers = @_;
# Check all values are numbers and we have at least three
die "You must specify at least three values\n" unless scalar(@numbers) >= 3;
foreach my $v (@numbers) {
die "Value '$v' is not an integer\n" unless $v =~ /^-?[0-9]+$/;
}
# The last number is the target
my $target = pop @numbers;
foreach my $first_pos ( 0 .. $#numbers ) {
my $first_num = $numbers[$first_pos];
foreach my $second_pos ( 0 .. $#numbers ) {
my $second_num = $numbers[$second_pos];
if ( $first_pos != $second_pos
&& $first_num - $second_num == $target )
{
say "Output: 1 as $first_num - $second_num = $target";
return;
}
}
}
# There is no possible solution
say "Output: 0";
}
main(@ARGV);