fork download
  1. #!/usr/bin/perl
  2. use 5.016;
  3. use warnings;
  4. use List::Util qw(reduce);
  5.  
  6. sub f {
  7. my $r = reduce {
  8. defined $a->[2] or return $a;
  9. my $d = ($a->[2] <=> $b);
  10. ($a->[1] == $d or $d == 0 or $a->[1] == 0) ?
  11. [$a->[0] + 1, $a->[1] || $d, $b] : [$a->[0], $a->[1], undef];
  12. } [0, 0, $_[0]], @_;
  13.  
  14. return ($r->[0] == @_ ? ["AS", "EQ", "DES"]->[$r->[1] + 1] : $r->[0]);
  15. }
  16.  
  17. say f(6,6,3,2,6,4,7,4,7,4);
  18. say f(2,3,4,4,4,6,6,6,7,7);
  19. say f(7,7,6,6,6,4,4,4,3,2);
  20. say f(1,1,1,1,1,1,1,1,1,1);
  21. say f();
  22. say f(1);
  23.  
  24. sub g {
  25. (@_ < 1) and return [];
  26. return reduce {
  27. $a->[-1]->[-1] <= $b ?
  28. [ @{$a}[0 .. $#{$a} - 1], [@{$a->[-1]}, $b]] :
  29. [ @{$a}, [$b] ]
  30. } [[$_[0]]], @_[1 .. $#_];
  31. }
  32.  
  33. use Data::Dumper;
  34. local $Data::Dumper::Terse = 1;
  35. local $Data::Dumper::Indent = 0;
  36.  
  37. say Dumper(g(8,3,4,9,9,10,6,1,4,3));
  38. say Dumper(g(1,3,3,4,4,6,8,9,9,10));
  39. say Dumper(g(10,9,9,8,6,4,4,3,3,1));
  40. say Dumper(g(1,1,1,1,1,1,1,1,1,1));
  41. say Dumper(g());
  42. say Dumper(g(1));
  43.  
Success #stdin #stdout 0.02s 5256KB
stdin
Standard input is empty
stdout
4
AS
DES
EQ
EQ
EQ
[[8],[3,4,9,9,10],[6],[1,4],[3]]
[[1,3,3,4,4,6,8,9,9,10]]
[[10],[9,9],[8],[6],[4,4],[3,3],[1]]
[[1,1,1,1,1,1,1,1,1,1]]
[]
[[1]]