diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-17 19:46:14 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-17 19:46:14 +0100 |
| commit | 99b3c63fe1fbc533a8421e76e6f8452f14535d0d (patch) | |
| tree | 0f5e916a00a02fd23123afa44e67becf5b781ec0 | |
| parent | b4edfa8a44b3ea8d5ab0423afa5613a344af2297 (diff) | |
| download | perlweeklychallenge-club-99b3c63fe1fbc533a8421e76e6f8452f14535d0d.tar.gz perlweeklychallenge-club-99b3c63fe1fbc533a8421e76e6f8452f14535d0d.tar.bz2 perlweeklychallenge-club-99b3c63fe1fbc533a8421e76e6f8452f14535d0d.zip | |
Add Perl solution to challenge 222
| -rw-r--r-- | challenge-222/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-222/paulo-custodio/perl/ch-1.pl | 49 | ||||
| -rw-r--r-- | challenge-222/paulo-custodio/perl/ch-2.pl | 52 | ||||
| -rw-r--r-- | challenge-222/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-222/paulo-custodio/t/test-2.yaml | 15 |
5 files changed, 133 insertions, 0 deletions
diff --git a/challenge-222/paulo-custodio/Makefile b/challenge-222/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-222/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-222/paulo-custodio/perl/ch-1.pl b/challenge-222/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..9ca882d4f7 --- /dev/null +++ b/challenge-222/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +# Challenge 222 +# +# Task 1: Matching Members +# Submitted by: Mohammad S Anwar +# You are given a list of positive integers, @ints. +# +# Write a script to find the total matching members after sorting the list +# increasing order. +# +# +# Example 1 +# Input: @ints = (1, 1, 4, 2, 1, 3) +# Output: 3 +# +# Original list: (1, 1, 4, 2, 1, 2) +# Sorted list : (1, 1, 1, 2, 3, 4) +# +# Compare the two lists, we found 3 matching members (1, 1, 2). +# Example 2 +# Input: @ints = (5, 1, 2, 3, 4) +# Output: 0 +# +# Original list: (5, 1, 2, 3, 4) +# Sorted list : (1, 2, 3, 4, 5) +# +# Compare the two lists, we found 0 matching members. +# Example 3 +# Input: @ints = (1, 2, 3, 4, 5) +# Output: 5 +# +# Original list: (1, 2, 3, 4, 5) +# Sorted list : (1, 2, 3, 4, 5) +# +# Compare the two lists, we found 5 matching members. + +use Modern::Perl; + +my @ints = @ARGV; +say matching_members(@ints); + +sub matching_members { + my(@ints) = @_; + + my @sorted = sort {$a <=> $b} @ints; + my $matching = scalar grep {$ints[$_]==$sorted[$_]} 0..$#ints; + return $matching; +} diff --git a/challenge-222/paulo-custodio/perl/ch-2.pl b/challenge-222/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..f94c87a806 --- /dev/null +++ b/challenge-222/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl + +# Challenge 222 +# +# Task 2: Last Member +# Submitted by: Mohammad S Anwar +# You are given an array of positive integers, @ints. +# +# Write a script to find the last member if found otherwise return 0. Each turn +# pick 2 biggest members (x, y) then decide based on the following conditions, +# continue this until you are left with 1 member or none. +# +# a) if x == y then remove both members +# +# b) if x != y then remove both members and add new member (y-x) +# +# +# Example 1: +# Input: @ints = (2, 7, 4, 1, 8, 1) +# Output: 1 +# +# Step 1: pick 7 and 8, we remove both and add new member 1 => (2, 4, 1, 1, 1). +# Step 2: pick 2 and 4, we remove both and add new member 2 => (2, 1, 1, 1). +# Step 3: pick 2 and 1, we remove both and add new member 1 => (1, 1, 1). +# Step 4: pick 1 and 1, we remove both => (1). +# Example 2: +# Input: @ints = (1) +# Output: 1 +# Example 3: +# Input: @ints = (1, 1) +# Output: 0 +# +# Step 1: pick 1 and 1, we remove both and we left with none. + +use Modern::Perl; + +my @ints = @ARGV; +say last_member(@ints); + +sub last_member { + my(@ints) = @_; + for (;;) { + return 0 if @ints==0; + return $ints[0] if @ints==1; + @ints = sort {$a <=> $b} @ints; + my $y = pop @ints; + my $x = pop @ints; + if ($x != $y) { + push @ints, $y-$x; + } + } +} diff --git a/challenge-222/paulo-custodio/t/test-1.yaml b/challenge-222/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..8fe2db43d0 --- /dev/null +++ b/challenge-222/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 1 4 2 1 3 + input: + output: 3 +- setup: + cleanup: + args: 5 1 2 3 4 + input: + output: 0 +- setup: + cleanup: + args: 1 2 3 4 5 + input: + output: 5 diff --git a/challenge-222/paulo-custodio/t/test-2.yaml b/challenge-222/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..b8163a34c7 --- /dev/null +++ b/challenge-222/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 2 7 4 1 8 1 + input: + output: 1 +- setup: + cleanup: + args: 1 + input: + output: 1 +- setup: + cleanup: + args: 1 1 + input: + output: 0 |
