aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-10-06 20:30:25 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-10-06 20:30:25 +0100
commit1a03e9b07b5d63dc498822d2811224c4cc077321 (patch)
tree719842f21e48eeb6b5aeffdcf55f784086b08063
parent19079cd8a18e2ddf3696b1fcb9eb14f27d785e15 (diff)
downloadperlweeklychallenge-club-1a03e9b07b5d63dc498822d2811224c4cc077321.tar.gz
perlweeklychallenge-club-1a03e9b07b5d63dc498822d2811224c4cc077321.tar.bz2
perlweeklychallenge-club-1a03e9b07b5d63dc498822d2811224c4cc077321.zip
- Tidied up a bit.
-rw-r--r--challenge-342/mohammad-anwar/perl/ch-1.pl63
1 files changed, 41 insertions, 22 deletions
diff --git a/challenge-342/mohammad-anwar/perl/ch-1.pl b/challenge-342/mohammad-anwar/perl/ch-1.pl
index 965d08d827..7066fddf5c 100644
--- a/challenge-342/mohammad-anwar/perl/ch-1.pl
+++ b/challenge-342/mohammad-anwar/perl/ch-1.pl
@@ -18,28 +18,47 @@ for (@examples) {
done_testing;
sub balance_string {
- my $s = shift; # Get the input string from arguments
- my @d = sort grep /\d/, split //, $s; # Extract all digits and sort them
- my @l = sort grep /\D/, split //, $s; # Extract all letters and sort them
+ my $s = shift;
+ # Extract all digits and sort them
+ my @d = sort grep /\d/, split //, $s;
+ # Extract all letters and sort them
+ my @l = sort grep /\D/, split //, $s;
- return "" if abs(@d - @l) > 1; # Return empty string if impossible
+ # Return empty string if impossible
+ return "" if abs(@d - @l) > 1;
- @d > @l ? # If more digits than letters:
- join "", # Join all elements into a string
- map $d[$_] . ($l[$_] || ""), # For each index: digit + letter (or empty if no letter)
- 0..$#d # Iterate through all digit indices
- : # Else:
- @l > @d ? # If more letters than digits:
- join "", # Join all elements into a string
- map $l[$_] . ($d[$_] || ""), # For each index: letter + digit (or empty if no digit)
- 0..$#l # Iterate through all letter indices
- : # Else (equal counts):
- $d[0] lt $l[0] ? # If first digit < first letter lexicographically:
- join "", # Join all elements into a string
- map $d[$_] . $l[$_], # For each index: digit + letter
- 0..$#d # Iterate through all indices (both arrays same size)
- : # Else:
- join "", # Join all elements into a string
- map $l[$_] . $d[$_], # For each index: letter + digit
- 0..$#d # Iterate through all indices (both arrays same size)
+ # If more digits than letters:
+ @d > @l ?
+ # Join all elements into a string
+ join "",
+ # For each index: digit + letter (or empty if no letter)
+ map $d[$_] . ($l[$_] || ""),
+ # Iterate through all digit indices
+ 0..$#d
+ :
+ # If more letters than digits:
+ @l > @d ?
+ # Join all elements into a string
+ join "",
+ # For each index: letter + digit (or empty if no digit)
+ map $l[$_] . ($d[$_] || ""),
+ # Iterate through all letter indices
+ 0..$#l
+ # Else (equal counts):
+ :
+ # If first digit < first letter lexicographically:
+ $d[0] lt $l[0] ?
+ # Join all elements into a string
+ join "",
+ # For each index: digit + letter
+ map $d[$_] . $l[$_],
+ # Iterate through all indices (both arrays same size)
+ 0..$#d
+ : # Else:
+ # Join all elements into a string
+ join "",
+ # For each index: letter + digit
+ map $l[$_] . $d[$_],
+ # Iterate through all indices (both arrays same size)
+ 0..$#d
}