diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-19 14:39:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-19 14:39:10 +0100 |
| commit | 9017feae40ad64e20094b8eb0b67c115c85f778e (patch) | |
| tree | eab1262983c29c4440daba74549b5ad19c576440 | |
| parent | 4627610b81d312e5ff3ea2145d10c5dcd537fc93 (diff) | |
| parent | b10ba796259add666f09f0b9ad886a495b162c0e (diff) | |
| download | perlweeklychallenge-club-9017feae40ad64e20094b8eb0b67c115c85f778e.tar.gz perlweeklychallenge-club-9017feae40ad64e20094b8eb0b67c115c85f778e.tar.bz2 perlweeklychallenge-club-9017feae40ad64e20094b8eb0b67c115c85f778e.zip | |
Merge pull request #10658 from pauloscustodio/master
Add Perl solution to challenge 228
| -rw-r--r-- | challenge-228/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-228/paulo-custodio/perl/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-228/paulo-custodio/perl/ch-2.pl | 54 | ||||
| -rw-r--r-- | challenge-228/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-228/paulo-custodio/t/test-2.yaml | 10 |
5 files changed, 122 insertions, 0 deletions
diff --git a/challenge-228/paulo-custodio/Makefile b/challenge-228/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-228/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-228/paulo-custodio/perl/ch-1.pl b/challenge-228/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..d2dc85babf --- /dev/null +++ b/challenge-228/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +# Challenge 228 +# +# Task 1: Unique Sum +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers. +# +# Write a script to find out the sum of unique elements in the given array. +# Example 1 +# +# Input: @int = (2, 1, 3, 2) +# Output: 4 +# +# In the given array we have 2 unique elements (1, 3). +# +# Example 2 +# +# Input: @int = (1, 1, 1, 1) +# Output: 0 +# +# In the given array no unique element found. +# +# Example 3 +# +# Input: @int = (2, 1, 3, 4) +# Output: 10 +# +# In the given array every element is unique. + +use Modern::Perl; +use List::Util 'sum'; + +my @ints = @ARGV; +my %count; +for (@ints) { + $count{$_}++; +} +my $sum = sum(map {$_->[0]} grep {$count{$_->[0]}==1} map {[$_, $count{$_}]} @ints)//0; +say $sum; diff --git a/challenge-228/paulo-custodio/perl/ch-2.pl b/challenge-228/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..1714784c04 --- /dev/null +++ b/challenge-228/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +# Challenge 228 +# +# Task 2: Empty Array +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers in which all elements are unique. +# +# Write a script to perform the following operations until the array is empty and return the total count of operations. +# +# If the first element is the smallest then remove it otherwise move it to the end. +# +# +# Example 1 +# +# Input: @int = (3, 4, 2) +# Ouput: 5 +# +# Operation 1: move 3 to the end: (4, 2, 3) +# Operation 2: move 4 to the end: (2, 3, 4) +# Operation 3: remove element 2: (3, 4) +# Operation 4: remove element 3: (4) +# Operation 5: remove element 4: () +# +# Example 2 +# +# Input: @int = (1, 2, 3) +# Ouput: 3 +# +# Operation 1: remove element 1: (2, 3) +# Operation 2: remove element 2: (3) +# Operation 3: remove element 3: () + +use Modern::Perl; +use List::Util 'min'; + +my @ints = @ARGV; +say count_ops(@ints); + +sub count_ops { + my(@ints) = @_; + my $ops = 0; + while (@ints) { + if (min(@ints)==$ints[0]) { + shift @ints; + } + else { + push @ints, shift @ints; + } + $ops++; + } + return $ops; +} diff --git a/challenge-228/paulo-custodio/t/test-1.yaml b/challenge-228/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..1d83e26870 --- /dev/null +++ b/challenge-228/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 2 1 3 2 + input: + output: 4 +- setup: + cleanup: + args: 1 1 1 1 + input: + output: 0 +- setup: + cleanup: + args: 2 1 3 4 + input: + output: 10 diff --git a/challenge-228/paulo-custodio/t/test-2.yaml b/challenge-228/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..79872d02e8 --- /dev/null +++ b/challenge-228/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 3 4 2 + input: + output: 5 +- setup: + cleanup: + args: 1 2 3 + input: + output: 3 |
