/
ch-1.pl
executable file
·37 lines (35 loc) · 1.09 KB
/
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
37
#!/usr/bin/env perl
# Perl weekly challenge 235
# Task 1: Remove One
#
# See https://wlmb.github.io/2023/09/18/PWC235/#task-1-remove-one
use v5.36;
sub test(@in) {
# Removing one element from a one or two element array yields an
# empty or a one element trivially sorted array
return 1 if @in<=2;
my $count=0;
my @sorted;
push @sorted, shift @in; # initialize
while(@in){
if($in[0]>$sorted[-1]){ # can move from @in to @sorted
# without disordering it?
push @sorted, shift @in;
}else{ # if not
$sorted[-1]=shift(@in); # replace last element of @sorted
++$count;
return 0
if $count>=2 # fail if too many replacements or
# if @sorted is not actually ordered
|| (@sorted>=2 && $sorted[-2] >= $sorted[-1]);
}
}return 1; # success
}
die <<~"FIN" unless @ARGV;
Usage: $0 N1 {N2...]
to find if the array N1, N2... becomes sorted by removing
only one element.
FIN
my @in=@ARGV;
my $out=test(@in)?"True":"False";
say "@ARGV -> $out";