diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-20 09:08:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-20 09:08:54 +0100 |
| commit | 26ba89d6d3fa0ab10e9d854f825237166d490377 (patch) | |
| tree | dec3d900df153e1be97ba1856d4581106a99d117 | |
| parent | cbba7d4c380845ac507a4b3a68be4014192d4817 (diff) | |
| parent | aba6b7ef791aa95ffe5fea741a6e49aa970fd633 (diff) | |
| download | perlweeklychallenge-club-26ba89d6d3fa0ab10e9d854f825237166d490377.tar.gz perlweeklychallenge-club-26ba89d6d3fa0ab10e9d854f825237166d490377.tar.bz2 perlweeklychallenge-club-26ba89d6d3fa0ab10e9d854f825237166d490377.zip | |
Merge pull request #12046 from PerlBoy1967/branch-for-challenge-322
w322 - Task 1 & 2
| -rwxr-xr-x | challenge-322/perlboy1967/perl/ch1.pl | 46 | ||||
| -rwxr-xr-x | challenge-322/perlboy1967/perl/ch2.pl | 39 |
2 files changed, 85 insertions, 0 deletions
diff --git a/challenge-322/perlboy1967/perl/ch1.pl b/challenge-322/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..d945105fd2 --- /dev/null +++ b/challenge-322/perlboy1967/perl/ch1.pl @@ -0,0 +1,46 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 322 +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-322#TASK1> + +Author: Niels 'PerlBoy' van Dijke + +Task 1: String Format +Submitted by: Mohammad Sajid Anwar + +You are given a string and a positive integer. + +Write a script to format the string, removing any dashes, in groups of +size given by the integer. The first group can be smaller than the integer +but should have at least one character. Groups should be separated by dashes. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +sub stringFormat ($str,$len) { + my @r; + + # Get first part + $str =~ s/([^-]{1,$len})//; + push(@r,$1); + + # Get the rest + $str =~ s/-//g; + 1 while (push(@r,substr($str,0,$len,'')) && $str); + + return join('-',@r); +} + +is(stringFormat('ABC-D-E-F',3),'ABC-DEF','Example 1'); +is(stringFormat('A-BC-D-E',2),'A-BC-DE','Example 2'); +is(stringFormat('-A-B-CD-E',4),'A-BCDE','Example 3'); +is(stringFormat('ABC-DE-F',2),'AB-CD-EF','Own example'); + +done_testing; diff --git a/challenge-322/perlboy1967/perl/ch2.pl b/challenge-322/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..5e46d4cf12 --- /dev/null +++ b/challenge-322/perlboy1967/perl/ch2.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 322 +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-322#TASK2> + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Rank Array +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers. + +Write a script to return an array of the ranks of each element: the lowest +value has rank 1, next lowest rank 2, etc. If two elements are the same +then they share the same rank. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +use List::MoreUtils qw(uniq); + +sub rankArray (@ints) { + my ($i,%idx) = (1); + $idx{$_} = $i++ for uniq sort { $a <=> $b } @ints; + return map { $idx{$_} } @ints; +} + +is([rankArray(55,22,44,33)],[4,1,3,2],'Example 1'); +is([rankArray(10,10,10)],[1,1,1],'Example 2'); +is([rankArray(5,1,1,4,3)],[4,1,1,3,2],'Example 3'); + +done_testing; |
