Download the raw code.
#
# whether a given integer is contained inside a given set of integer ranges
# here we just check each of the constraints, inverting the check when necessary,
# the result is the product of each check.
#
my ($tests, $number) = $*IN.lines;
exit say $number ?? "no" !! "invalid input" unless $tests;
say (
[*] gather for $tests.split(/\]/) -> $item {
next unless $item;
$item ~~ m:s /(<[+\-]>) \[ (\d+) \.\. (\d+)/ or exit say "invalid input";
my $ok = $1 <= $number <= $2;
take $0 eq '+' ?? $ok !! !$ok
}) ?? "yes" !! "no";
Readable, yet... really short. How does he do it?
It strikes me as at least mixing metaphors a bit when $tests
is .split
into several $items
. But I'm nitpicking; there's not much to say about
consistency in this script.
The script uses [*]
, reduced multiplication, for what is essentially a
boolean operation.
Splitting on ']'? Sure, it works, but that choice of subdividing the string
is what necessitates the next unless $item;
on line 13. Even if what we're
striving for here is brevity (and it seems it is), it's not clear that that
choice is worth it.
Fast, but... wrong. The comment at line 4 says "the result is the product of each check", but the problem specification says "Inclusions and exclusions are performed from left to right." The latter wording allows a later inclusion to override an earlier exclusion... the solution doesn't.
Again, this is a gather
(line 12) that should probably have been a map
.
We all agree that it's short.