diff options
| -rw-r--r-- | challenge-235/solathian/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-235/solathian/perl/ch-2.pl | 23 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-235/solathian/perl/ch-1.pl b/challenge-235/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..db47138ab5 --- /dev/null +++ b/challenge-235/solathian/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!usr/bin/perl +use v5.38; + +use builtin 'indexed'; +use List::MoreUtils 'uniq'; +no warnings 'experimental'; + +# Challenge 235 - 1 - Remove One +# You are given an array of integers. +# Write a script to find out if removing ONLY one integer makes it strictly increasing order. + + +say rem1(0, 2, 9, 4, 6); # Output: true +say rem1(5, 1, 3, 2); # Output: false +say rem1(2, 2, 3); # Output: true +say rem1(2, 2, 3, 4); # Output: true +say rem1(2, 2, 3, 0); # Output: false since it is not strictly increasing + + +sub rem1(@input) +{ + + foreach my($index, $elem) ( indexed @input) + { + my @temp = @input; + + splice(@temp, $index, 1); + + my @sorted = uniq sort @temp; # somehow sort uniq is not working hmm (´。_。`) + + return "true" if( "@sorted" eq "@temp"); + + } + + return "false"; +}
\ No newline at end of file diff --git a/challenge-235/solathian/perl/ch-2.pl b/challenge-235/solathian/perl/ch-2.pl new file mode 100644 index 0000000000..b816581db4 --- /dev/null +++ b/challenge-235/solathian/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!usr/bin/perl +use v5.38; + +# Challenge 235 - 2 - Duplicate Zeros + +dup0(1, 0, 2, 3, 0, 4, 5, 0); # (1, 0, 0, 2, 3, 0, 0, 4) +dup0(1, 2, 3); # (1, 2, 3) +dup0(0, 3, 0, 4, 5); # (0, 0, 3, 0, 0) + + +sub dup0(@old) +{ + + my @new; + + foreach my $elem (@old) + { + push(@new, $elem); + push(@new, $elem) if($elem == 0); + } + splice(@new, @old); # remove elements after the size + say join ',', @new; +}
\ No newline at end of file |
