aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-322/perlboy1967/perl/ch1.pl46
-rwxr-xr-xchallenge-322/perlboy1967/perl/ch2.pl39
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;