diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-23 12:06:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-23 12:06:16 +0000 |
| commit | 2ae505b68377dfe9ab5f5d51600c26321d933f36 (patch) | |
| tree | 9e5a2b1e4941db8eb59bae0bc68b741d03889016 | |
| parent | 028f9b5da0d931d66b91fde72b46abdb5f3e703a (diff) | |
| parent | 786acd8ebaff190b023730a9d6bc34bc0f83db39 (diff) | |
| download | perlweeklychallenge-club-2ae505b68377dfe9ab5f5d51600c26321d933f36.tar.gz perlweeklychallenge-club-2ae505b68377dfe9ab5f5d51600c26321d933f36.tar.bz2 perlweeklychallenge-club-2ae505b68377dfe9ab5f5d51600c26321d933f36.zip | |
Merge pull request #9453 from spadacciniweb/PWC-253
PWC 253 in Perl
| -rw-r--r-- | challenge-253/spadacciniweb/perl/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-253/spadacciniweb/perl/ch-2.pl | 85 |
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-253/spadacciniweb/perl/ch-1.pl b/challenge-253/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..b6ffebb733 --- /dev/null +++ b/challenge-253/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +# Task 1: Split Strings +# Submitted by: Mohammad S Anwar +# +# You are given an array of strings and a character separator. +# Write a script to return all words separated by the given character excluding empty string. +# +# Example 1 +# Input: @words = ("one.two.three","four.five","six") +# $separator = "." +# Output: "one","two","three","four","five","six" +# +# Example 2 +# Input: @words = ("$perl$$", "$$raku$") +# $separator = "$" +# Output: "perl","raku" + + +use strict; +use warnings; + +my @words = ("one.two.three","four.five","six"); +my $separator = "."; +split_strings(\@words, $separator); + +@words = ('$perl$$', '$$raku$'); +$separator = '$'; +split_strings(\@words, $separator); + +exit 0; + +sub split_strings { + my $words = shift; + my $separator = shift; + + printf "Output: \"%s\"\n", join '","', map { join '","', grep {!/^$/} split /\Q$separator\E/, $_ } + @$words; + + return 0; +} diff --git a/challenge-253/spadacciniweb/perl/ch-2.pl b/challenge-253/spadacciniweb/perl/ch-2.pl new file mode 100644 index 0000000000..e9de1b2539 --- /dev/null +++ b/challenge-253/spadacciniweb/perl/ch-2.pl @@ -0,0 +1,85 @@ +#!/usr/bin/env perl + +# Task 2: Weakest Row +# Submitted by: Mohammad S Anwar +# +# You are given an m x n binary matrix i.e. only 0 and 1 where 1 always appear before 0. +# A row i is weaker than a row j if one of the following is true: +# +# a) The number of 1s in row i is less than the number of 1s in row j. +# b) Both rows have the same number of 1 and i < j. +# +# Write a script to return the order of rows from weakest to strongest. +# Example 1 +# +# Input: $matrix = [ +# [1, 1, 0, 0, 0], +# [1, 1, 1, 1, 0], +# [1, 0, 0, 0, 0], +# [1, 1, 0, 0, 0], +# [1, 1, 1, 1, 1] +# ] +# Output: (2, 0, 3, 1, 4) +# +# The number of 1s in each row is: +# - Row 0: 2 +# - Row 1: 4 +# - Row 2: 1 +# - Row 3: 2 +# - Row 4: 5 +# +# Example 2 +# Input: $matrix = [ +# [1, 0, 0, 0], +# [1, 1, 1, 1], +# [1, 0, 0, 0], +# [1, 0, 0, 0] +# ] +# Output: (0, 2, 3, 1) +# +# The number of 1s in each row is: +# - Row 0: 1 +# - Row 1: 4 +# - Row 2: 1 +# - Row 3: 1 + +use strict; +use warnings; +use List::Util qw/sum/; + + +my $matrix = [ + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] + ]; +order_rows($matrix); + +$matrix = [ + [1, 0, 0, 0], + [1, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 0] + ]; +order_rows($matrix); + +exit 0; + +sub order_rows { + my $matrix = shift; + + my %sum_rows; + foreach my $i (0..(scalar(@$matrix) -1)) { + $sum_rows{$i} = sum(@{ $matrix->[$i] }); + } + + printf "Output: (%s)\n", join ', ' , map { $_ } + sort { $sum_rows{$a} <=> $sum_rows{$b} + || + $a <=> $b + } keys %sum_rows; + + return 0; +} |
