From 0af1650632b5c41634fddec6d29bf87731adc85e Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 19 May 2025 07:46:45 +0000 Subject: w322 - Task 1 & 2 --- challenge-322/perlboy1967/perl/ch1.pl | 46 +++++++++++++++++++++++++++++++++++ challenge-322/perlboy1967/perl/ch2.pl | 39 +++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100755 challenge-322/perlboy1967/perl/ch1.pl create mode 100755 challenge-322/perlboy1967/perl/ch2.pl 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 + +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..5c24353d26 --- /dev/null +++ b/challenge-322/perlboy1967/perl/ch2.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 322 +L + +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 @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; -- cgit From aba6b7ef791aa95ffe5fea741a6e49aa970fd633 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 19 May 2025 07:50:07 +0000 Subject: Task 2 - add "<=>" sorting --- challenge-322/perlboy1967/perl/ch2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-322/perlboy1967/perl/ch2.pl b/challenge-322/perlboy1967/perl/ch2.pl index 5c24353d26..5e46d4cf12 100755 --- a/challenge-322/perlboy1967/perl/ch2.pl +++ b/challenge-322/perlboy1967/perl/ch2.pl @@ -28,7 +28,7 @@ use List::MoreUtils qw(uniq); sub rankArray (@ints) { my ($i,%idx) = (1); - $idx{$_} = $i++ for uniq sort @ints; + $idx{$_} = $i++ for uniq sort { $a <=> $b } @ints; return map { $idx{$_} } @ints; } -- cgit