diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-18 19:28:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-18 19:28:43 +0100 |
| commit | 2d40bc36496f52bf421e5c2aed87a7af8cf3b744 (patch) | |
| tree | 417809d4da0a44da845da5937816fe09c7cd18c7 | |
| parent | 6c2c0faf92ee6bf60ed85efae701c112ef9b5971 (diff) | |
| parent | 7a6f61b8cb38ea32ab526a93798f7b515f9233b4 (diff) | |
| download | perlweeklychallenge-club-2d40bc36496f52bf421e5c2aed87a7af8cf3b744.tar.gz perlweeklychallenge-club-2d40bc36496f52bf421e5c2aed87a7af8cf3b744.tar.bz2 perlweeklychallenge-club-2d40bc36496f52bf421e5c2aed87a7af8cf3b744.zip | |
Merge pull request #10639 from pauloscustodio/master
Add Perl solution to challenge 225
| -rw-r--r-- | challenge-225/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-225/paulo-custodio/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-225/paulo-custodio/perl/ch-2.pl | 76 | ||||
| -rw-r--r-- | challenge-225/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-225/paulo-custodio/t/test-2.yaml | 15 | ||||
| -rw-r--r-- | challenge-226/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-226/paulo-custodio/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-226/paulo-custodio/perl/ch-2.pl | 50 | ||||
| -rw-r--r-- | challenge-226/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-226/paulo-custodio/t/test-2.yaml | 15 |
10 files changed, 233 insertions, 0 deletions
diff --git a/challenge-225/paulo-custodio/Makefile b/challenge-225/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-225/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-225/paulo-custodio/perl/ch-1.pl b/challenge-225/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..c2ea1a9e1b --- /dev/null +++ b/challenge-225/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +# Challenge 225 +# +# Task 1: Max Words +# Submitted by: Mohammad S Anwar +# You are given a list of sentences, @list. +# +# +# A sentence is a list of words that are separated by a single space with no +# leading or trailing spaces. +# +# +# Write a script to find out the maximum number of words that appear in a +# single sentence. +# +# Example 1 +# Input: @list = ("Perl and Raku belong to the same family.", +# "I love Perl.", +# "The Perl and Raku Conference.") +# Output: 8 +# Example 2 +# Input: @list = ("The Weekly Challenge.", +# "Python is the most popular guest language.", +# "Team PWC has over 300 members.") +# Output: 7 + +use Modern::Perl; +use List::Util 'max'; + +say max(map {scalar(split ' ', $_)} @ARGV); diff --git a/challenge-225/paulo-custodio/perl/ch-2.pl b/challenge-225/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..2da519c1fb --- /dev/null +++ b/challenge-225/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,76 @@ +#!/usr/bin/env perl + +# Challenge 225 +# +# Task 2: Left Right Sum Diff +# Submitted by: Mohammad S Anwar +# You are given an array of integers, @ints. +# +# Write a script to return left right sum diff array as shown below: +# +# +# @ints = (a, b, c, d, e) +# +# @left = (0, a, (a+b), (a+b+c)) +# @right = ((c+d+e), (d+e), e, 0) +# @left_right_sum_diff = ( | 0 - (c+d+e) |, +# | a - (d+e) |, +# | (a+b) - e |, +# | (a+b+c) - 0 | ) +# +# Example 1: +# Input: @ints = (10, 4, 8, 3) +# Output: (15, 1, 11, 22) +# +# @left = (0, 10, 14, 22) +# @right = (15, 11, 3, 0) +# +# @left_right_sum_diff = ( |0-15|, |10-11|, |14-3|, |22-0|) +# = (15, 1, 11, 22) +# Example 2: +# Input: @ints = (1) +# Output: (0) +# +# @left = (0) +# @right = (0) +# +# @left_right_sum_diff = ( |0-0| ) = (0) +# Example 3: +# Input: @ints = (1, 2, 3, 4, 5) +# Output: (14, 11, 6, 1, 10) +# +# @left = (0, 1, 3, 6, 10) +# @right = (14, 12, 9, 5, 0) +# +# @left_right_sum_diff = ( |0-14|, |1-12|, |3-9|, |6-5|, |10-0|) +# = (14, 11, 6, 1, 10) + +use Modern::Perl; + +my @ints = @ARGV; +my @sum = right_sum_diff(@ints); +say "@sum"; + +sub right_sum_diff { + my(@ints) = @_; + + my $sum = 0; + my @left = ($sum); + for (@ints[0..$#ints-1]) { + $sum += $_; + push @left, $sum; + } + + $sum = 0; + my @right = ($sum); + for (reverse @ints[1..$#ints]) { + $sum += $_; + unshift @right, $sum; + } + + my @sum; + for (0..$#ints) { + push @sum, abs($left[$_]-$right[$_]); + } + return @sum; +} diff --git a/challenge-225/paulo-custodio/t/test-1.yaml b/challenge-225/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..7e9cfd03b2 --- /dev/null +++ b/challenge-225/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: '"Perl and Raku belong to the same family." "I love Perl." "The Perl and Raku Conference."' + input: + output: 8 +- setup: + cleanup: + args: '"The Weekly Challenge." "Python is the most popular guest language." "Team PWC has over 300 members."' + input: + output: 7 diff --git a/challenge-225/paulo-custodio/t/test-2.yaml b/challenge-225/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..970b197902 --- /dev/null +++ b/challenge-225/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 10 4 8 3 + input: + output: 15 1 11 22 +- setup: + cleanup: + args: 1 + input: + output: 0 +- setup: + cleanup: + args: 1 2 3 4 5 + input: + output: 14 11 6 1 10 diff --git a/challenge-226/paulo-custodio/Makefile b/challenge-226/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-226/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-226/paulo-custodio/perl/ch-1.pl b/challenge-226/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..9e3887ae3b --- /dev/null +++ b/challenge-226/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl + +# Challenge 226 +# +# Task 1: Shuffle String +# Submitted by: Mohammad S Anwar +# You are given a string and an array of indices of same length as string. +# +# Write a script to return the string after re-arranging the indices in the correct order. +# +# Example 1 +# Input: $string = 'lacelengh', @indices = (3,2,0,5,4,8,6,7,1) +# Output: 'challenge' +# Example 2 +# Input: $string = 'rulepark', @indices = (4,7,3,1,0,5,2,6) +# Output: 'perlraku' + +use Modern::Perl; + +my($str, @idx) = @ARGV; +my @chars = split //, $str; +say join('', map {$_->[1]} sort {$a->[0] <=> $b->[0]} map {[$idx[$_], $chars[$_]]} 0..$#chars); diff --git a/challenge-226/paulo-custodio/perl/ch-2.pl b/challenge-226/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..1effcf0822 --- /dev/null +++ b/challenge-226/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +# Challenge 226 +# +# Task 2: Zero Array +# Submitted by: Mohammad S Anwar +# You are given an array of non-negative integers, @ints. +# +# Write a script to return the minimum number of operations to make every +# element equal zero. +# +# In each operation, you are required to pick a positive number less than or +# equal to the smallest element in the array, then subtract that from each +# positive element in the array. +# +# +# Example 1: +# Input: @ints = (1, 5, 0, 3, 5) +# Output: 3 +# +# operation 1: pick 1 => (0, 4, 0, 2, 4) +# operation 2: pick 2 => (0, 2, 0, 0, 2) +# operation 3: pick 2 => (0, 0, 0, 0, 0) +# Example 2: +# Input: @ints = (0) +# Output: 0 +# Example 3: +# Input: @ints = (2, 1, 4, 0, 3) +# Output: 4 +# +# operation 1: pick 1 => (1, 0, 3, 0, 2) +# operation 2: pick 1 => (0, 0, 2, 0, 1) +# operation 3: pick 1 => (0, 0, 1, 0, 0) +# operation 4: pick 1 => (0, 0, 0, 0, 0) + +use Modern::Perl; +use List::Util 'min'; + +say num_ops(@ARGV); + +sub num_ops { + my(@n) = @_; + my $ops = 0; + while (grep {$_ > 0} @n) { + my $min = min(grep {$_ > 0} @n); + @n = map {$_ > 0 ? $_-$min : 0} @n; + $ops++; + } + return $ops; +} diff --git a/challenge-226/paulo-custodio/t/test-1.yaml b/challenge-226/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..94a37a6263 --- /dev/null +++ b/challenge-226/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: lacelengh 3 2 0 5 4 8 6 7 1 + input: + output: challenge +- setup: + cleanup: + args: rulepark 4 7 3 1 0 5 2 6 + input: + output: perlraku diff --git a/challenge-226/paulo-custodio/t/test-2.yaml b/challenge-226/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..5a8ebeb4fe --- /dev/null +++ b/challenge-226/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 5 0 3 5 + input: + output: 3 +- setup: + cleanup: + args: 0 + input: + output: 0 +- setup: + cleanup: + args: 2 1 4 0 3 + input: + output: 4 |
