#!/usr/bin/perl
use 5.016;
use warnings;
use List
::Util qw(reduce
);
sub f {
my $r = reduce {
my $d = ($a->[2] <=> $b);
($a->[1] == $d or $d == 0 or $a->[1] == 0) ?
[$a->[0] + 1, $a->[1] || $d, $b] : [$a->[0], $a->[1], undef]; } [0, 0, $_[0]], @_;
return ($r->[0] == @_ ? ["AS", "EQ", "DES"]->[$r->[1] + 1] : $r->[0]); }
say f(6,6,3,2,6,4,7,4,7,4);
say f(2,3,4,4,4,6,6,6,7,7);
say f(7,7,6,6,6,4,4,4,3,2);
say f(1,1,1,1,1,1,1,1,1,1);
say f();
say f(1);
sub g {
$a->[-1]->[-1] <= $b ?
[ @{$a}[0 .. $#{$a} - 1], [@{$a->[-1]}, $b]] :
[ @{$a}, [$b] ]
} [[$_[0]]], @_[1 .. $#_];
}
use Data::Dumper;
local $Data::Dumper::Terse = 1; local $Data::Dumper::Indent = 0;
say Dumper(g(8,3,4,9,9,10,6,1,4,3));
say Dumper(g(1,3,3,4,4,6,8,9,9,10));
say Dumper(g(10,9,9,8,6,4,4,3,3,1));
say Dumper(g(1,1,1,1,1,1,1,1,1,1));
say Dumper(g());
say Dumper(g(1));
IyEvdXNyL2Jpbi9wZXJsCnVzZSA1LjAxNjsKdXNlIHdhcm5pbmdzOwp1c2UgTGlzdDo6VXRpbCBxdyhyZWR1Y2UpOwoKc3ViIGYgewoJbXkgJHIgPSByZWR1Y2UgewoJCWRlZmluZWQgJGEtPlsyXSBvciByZXR1cm4gJGE7CgkJbXkgJGQgPSAoJGEtPlsyXSA8PT4gJGIpOwoJCSgkYS0+WzFdID09ICRkIG9yICRkID09IDAgb3IgJGEtPlsxXSA9PSAwKSA/CgkJCVskYS0+WzBdICsgMSwgJGEtPlsxXSB8fCAkZCwgJGJdIDogWyRhLT5bMF0sICRhLT5bMV0sIHVuZGVmXTsKCX0gWzAsIDAsICRfWzBdXSwgQF87CgoJcmV0dXJuICgkci0+WzBdID09IEBfID8gWyJBUyIsICJFUSIsICJERVMi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