diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-23 01:10:31 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-23 01:10:31 +0000 |
| commit | 5813fba591952ba6ad0c5bec7c4d454286e0b4aa (patch) | |
| tree | b2474a5a317cf030aa47426ae2a109197637fc94 /challenge-200 | |
| parent | 3a387476866d4bbdd5b514e4a99c04f0145296e4 (diff) | |
| parent | 7484c35eafb305da031e35a221cd55582baa2d27 (diff) | |
| download | perlweeklychallenge-club-5813fba591952ba6ad0c5bec7c4d454286e0b4aa.tar.gz perlweeklychallenge-club-5813fba591952ba6ad0c5bec7c4d454286e0b4aa.tar.bz2 perlweeklychallenge-club-5813fba591952ba6ad0c5bec7c4d454286e0b4aa.zip | |
Merge pull request #7454 from adamcrussell/challenge-200
initial commit
Diffstat (limited to 'challenge-200')
| -rw-r--r-- | challenge-200/adam-russell/perl/ch-1.pl | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-200/adam-russell/perl/ch-1.pl b/challenge-200/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..19784d8447 --- /dev/null +++ b/challenge-200/adam-russell/perl/ch-1.pl @@ -0,0 +1,40 @@ +use v5.36; +## +# You are given an array of integers. +# Write a script to find out all Arithmetic Slices for the given array of integers. +## +use boolean; + +sub is_arithmetic{ + my($slice) = @_; + do{ + return false if $slice->[$_] != $slice->[$_ + 1] - 1; + } for 0 .. @{$slice} - 2; + return true; +} + +sub arithmetic_slices{ + my @slices; + my @a = @_; + return ([]) if @a < 3; + do{ + my $i = $_; + do{ + my $slice = [@a[$i .. $i + $_]] if $a[$i] && $a[$i + $_]; + push @slices, $slice if $slice && is_arithmetic($slice); + } for 2 .. @a - 1; + } for 0 .. @a - 3; + return @slices; +} + +MAIN:{ + say q/1, 2, 3, 4/; + for my $slice (arithmetic_slices(1, 2, 3, 4)){ + say q/[/ . join(q/, /, @{$slice}) . q/]/; + } + say q//; + say q/2/; + for my $slice (arithmetic_slices(2)){ + say q/[/ . join(q/, /, @{$slice}) . q/]/; + } +}
\ No newline at end of file |
