aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2025-10-23 12:19:11 +0200
committerLubos Kolouch <lubos@kolouch.net>2025-10-23 12:19:11 +0200
commit2dbaa714ab27481fcae48d7f8fc8f0eb292aef91 (patch)
tree4e5d7d5fa9d6e9a1257e9bf27ecbc857a03bcb72
parent7c0847cc89f19213cb933727c8a775e5409c5d4f (diff)
downloadperlweeklychallenge-club-2dbaa714ab27481fcae48d7f8fc8f0eb292aef91.tar.gz
perlweeklychallenge-club-2dbaa714ab27481fcae48d7f8fc8f0eb292aef91.tar.bz2
perlweeklychallenge-club-2dbaa714ab27481fcae48d7f8fc8f0eb292aef91.zip
Add Perl ch-1 solution for challenge 344
-rw-r--r--challenge-344/lubos-kolouch/perl/ch-1.pl52
1 files changed, 52 insertions, 0 deletions
diff --git a/challenge-344/lubos-kolouch/perl/ch-1.pl b/challenge-344/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..9906490ef1
--- /dev/null
+++ b/challenge-344/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/env perl
+
+# Perl Weekly Challenge: Task 1 - Array Form Compute
+# Add an integer to its array-form representation and return the resulting digits.
+
+use strict;
+use warnings;
+use v5.30;
+
+# Enable signatures for clearer intent.
+use experimental 'signatures';
+
+# Add the integer $x to the array-form integer represented by $digits_ref.
+## no critic (Subroutines::ProhibitSubroutinePrototypes)
+sub array_form_compute ( $digits_ref, $x ) {
+ my @digits = $digits_ref->@*;
+ my @result;
+ my $index = $#digits;
+ my $carry = $x;
+
+ while ( $index >= 0 || $carry > 0 ) {
+ my $sum = $carry;
+ if ( $index >= 0 ) {
+ $sum += $digits[$index];
+ $index--;
+ }
+
+ unshift @result, $sum % 10;
+ $carry = int( $sum / 10 );
+ }
+
+ return @result ? @result : (0);
+}
+
+# Unit tests
+use Test::More;
+
+is_deeply(
+ [ array_form_compute( [ 1, 2, 3, 4 ], 12 ) ],
+ [ 1, 2, 4, 6 ],
+ 'Example 1: (1,2,3,4) + 12 -> (1,2,4,6)'
+);
+is_deeply( [ array_form_compute( [ 2, 7, 4 ], 181 ) ], [ 4, 5, 5 ], 'Example 2: (2,7,4) + 181 -> (4,5,5)' );
+is_deeply( [ array_form_compute( [ 9, 9, 9 ], 1 ) ], [ 1, 0, 0, 0 ], 'Example 3: (9,9,9) + 1 -> (1,0,0,0)' );
+is_deeply(
+ [ array_form_compute( [ 1, 0, 0, 0, 0 ], 9999 ) ],
+ [ 1, 9, 9, 9, 9 ],
+ 'Example 4: (1,0,0,0,0) + 9999 -> (1,9,9,9,9)'
+);
+is_deeply( [ array_form_compute( [0], 1000 ) ], [ 1, 0, 0, 0 ], 'Example 5: (0) + 1000 -> (1,0,0,0)' );
+
+done_testing();