diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-17 17:15:39 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-17 17:15:39 +0100 |
| commit | b4edfa8a44b3ea8d5ab0423afa5613a344af2297 (patch) | |
| tree | 9a4d3b5ee940e21584a86f3fe31d03feb26944f0 | |
| parent | bdd4bb818d8dde2e3210e3aec0f5e81df7b052b6 (diff) | |
| download | perlweeklychallenge-club-b4edfa8a44b3ea8d5ab0423afa5613a344af2297.tar.gz perlweeklychallenge-club-b4edfa8a44b3ea8d5ab0423afa5613a344af2297.tar.bz2 perlweeklychallenge-club-b4edfa8a44b3ea8d5ab0423afa5613a344af2297.zip | |
Add Perl solution to challenge 221 task 2
| -rw-r--r-- | challenge-221/paulo-custodio/perl/ch-2.pl | 69 | ||||
| -rw-r--r-- | challenge-221/paulo-custodio/t/test-2.yaml | 15 |
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-221/paulo-custodio/perl/ch-2.pl b/challenge-221/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..c2f51c86a1 --- /dev/null +++ b/challenge-221/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl + +# Challenge 221 +# +# Task 2: Arithmetic Subsequence +# Submitted by: Mohammad S Anwar +# You are given an array of integers, @ints. +# +# Write a script to find the length of the longest Arithmetic Subsequence in +# the given array. +# +# +# A subsequence is an array that can be derived from another array by deleting +# some or none elements without changing the order of the remaining elements. +# +# A subsquence is arithmetic if ints[i + 1] - ints[i] are all the same value +# (for 0 <= i < ints.length - 1). +# +# +# Example 1: +# Input: @ints = (9, 4, 7, 2, 10) +# Output: 3 +# +# The longest Arithmetic Subsequence (4, 7, 10) can be derived by deleting +# 9 and 2. +# Example 2: +# Input: @ints = (3, 6, 9, 12) +# Output: 4 +# +# No need to remove any elements, it is already an Arithmetic Subsequence. +# Example 3: +# Input: @ints = (20, 1, 15, 3, 10, 5, 8) +# Output: 4 +# +# The longest Arithmetic Subsequence (20, 15, 10, 5) can be derived by deleting +# 1, 3 and 8. + +use Modern::Perl; +use List::Util 'max'; + +my @ints = @ARGV; +my $max_sequence = 0; +for my $i (0 .. $#ints-1) { + for my $j ($i+1 .. $#ints) { + my $sequence = find_sequence($ints[$i], @ints[$j .. $#ints]); + $max_sequence = max($max_sequence, $sequence); + } +} +say $max_sequence; + +sub find_sequence { + my(@ints) = @_; + @ints >= 2 or die; + my $cur = shift @ints; + my $delta = $ints[0] - $cur; + my $sequence = 1; + while (@ints) { + my $next = $cur+$delta; + while (@ints && $ints[0] != $next) { + shift @ints; + } + if (@ints) { + $cur += $delta; + shift @ints; + $sequence++; + } + } + return $sequence; +} diff --git a/challenge-221/paulo-custodio/t/test-2.yaml b/challenge-221/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..acff6becac --- /dev/null +++ b/challenge-221/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 9 4 7 2 10 + input: + output: 3 +- setup: + cleanup: + args: 3 6 9 12 + input: + output: 4 +- setup: + cleanup: + args: 20 1 15 3 10 5 8 + input: + output: 4 |
