diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-12 21:06:47 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-12 21:06:47 +0100 |
| commit | 159d0591c3773f7830728a0505789984ea279ed6 (patch) | |
| tree | 851380c1736c180a06aa847bb18b8b0fc0b044d6 | |
| parent | 7cf48fb57571e15ee47cd112fcde92872327dbd2 (diff) | |
| parent | 5723109d4ddc0dc38266079c3a33e1d77dd2301f (diff) | |
| download | perlweeklychallenge-club-159d0591c3773f7830728a0505789984ea279ed6.tar.gz perlweeklychallenge-club-159d0591c3773f7830728a0505789984ea279ed6.tar.bz2 perlweeklychallenge-club-159d0591c3773f7830728a0505789984ea279ed6.zip | |
Merge pull request #8857 from spazm/week-238-perl
Week 238 perl
| -rwxr-xr-x | challenge-238/spazm/perl/task_1_running_sum.pl | 55 | ||||
| -rwxr-xr-x | challenge-238/spazm/perl/task_2_persistence_sort.pl | 95 |
2 files changed, 150 insertions, 0 deletions
diff --git a/challenge-238/spazm/perl/task_1_running_sum.pl b/challenge-238/spazm/perl/task_1_running_sum.pl new file mode 100755 index 0000000000..e640ec52ef --- /dev/null +++ b/challenge-238/spazm/perl/task_1_running_sum.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl + +use 5.36.0; +use Test::More; + +=pod + You are given an array of integers. + + Write a script to return the running sum of the given array. + The running sum can be calculated as sum[i] = num[0] + num[1] + …. + num[i] + + Example 1 + Input: @int = (1, 2, 3, 4, 5) + Output: (1, 3, 6, 10, 15) + >>> assert (1, 3, 6, 10, 15) == running_sum([1, 2, 3, 4, 5]) + Example 2 + Input: @int = (1, 1, 1, 1, 1) + Output: (1, 2, 3, 4, 5) + >>> assert (1, 2, 3, 4, 5) == running_sum([1, 1, 1, 1, 1]) + Example 3 + Input: @int = (0, -1, 1, 2) + Output: (0, -1, 0, 2) + >>> assert ( 0, -1, 0, 2) == running_sum([0, -1, 1, 2]) +=cut + +sub running_sum (@nums) +{ + my $current_sum = 0; + my @running_sum = (); + for my $i (@nums) + { + $current_sum += $i; + push @running_sum, $current_sum; + } + return @running_sum; +} + +sub test() +{ + my $tests = [ + [ [ 1, 2, 3, 4, 5 ], [ 1, 3, 6, 10, 15 ] ], + [ [ 1, 1, 1, 1, 1 ], [ 1, 2, 3, 4, 5 ] ], + [ [ 0, -1, 1, 2 ], [ 0, -1, 0, 2 ] ], + ]; + for my $test (@$tests) + { + my ( $input, $expected ) = @$test; + is_deeply [ running_sum(@$input) ], $expected, + "input: @$input, expected: @$expected"; + } + done_testing(); +} + +test() if not caller(); +1; diff --git a/challenge-238/spazm/perl/task_2_persistence_sort.pl b/challenge-238/spazm/perl/task_2_persistence_sort.pl new file mode 100755 index 0000000000..989509871a --- /dev/null +++ b/challenge-238/spazm/perl/task_2_persistence_sort.pl @@ -0,0 +1,95 @@ +#!/usr/bin/env perl + +use 5.36.0; +use Test::More; +use Data::Dumper; + +=pod + You are given an array of positive integers. + + Write a script to sort the given array in increasing order with respect to + the count of steps required to obtain a single-digit number by multiplying + its digits recursively for each array element. If any two numbers have the + same count of steps, then print the smaller number first. + + Example 1 + Input: @int = (15, 99, 1, 34) + Output: (1, 15, 34, 99) + + >>> persistence_sort([15, 99, 1, 34]) == [1, 15, 34, 99] + + 15 => 1 x 5 => 5 (1 step) + 99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps) + 1 => 0 step + 34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps) + Example 2 + Input: @int = (50, 25, 33, 22) + Output: (22, 33, 50, 25) + + 50 => 5 x 0 => 0 (1 step) + 25 => 2 x 5 => 10 => 1 x 0 => 0 (2 steps) + 33 => 3 x 3 => 9 (1 step) + 22 => 2 x 2 => 4 (1 step) +=cut + +sub persistence_sort (@nums) +{ + return map { $_->[1] } + sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } + map { [ single_digit_multiplication_steps($_), $_ ] } @nums; +} + +=pod + Given a multiple-digit number, multiply its digits recursively and return + the number of steps required to obtain a single-digit number. + + >>> single_digit_multiplication_steps(50) == 1 + >>> single_digit_multiplication_steps(25) == 2 + >>> single_digit_multiplication_steps(33) == 1 + >>> single_digit_multiplication_steps(22) == 1 + >>> single_digit_multiplication_steps(99) == 2 + >>> single_digit_multiplication_steps(81) == 1 + >>> single_digit_multiplication_steps(14) == 1 + >>> single_digit_multiplication_steps(98) == 3 + >>> single_digit_multiplication_steps(72) == 2 + >>> single_digit_multiplication_steps(14) == 1 +=cut + +sub single_digit_multiplication_steps ($num) +{ + my $product = 1; + for my $digit ( split( //, "$num" ) ) + { + $product *= int($digit); + } + + return $product < 10 + ? 1 + : 1 + single_digit_multiplication_steps($product); +} + +sub test() +{ + my $multiplication_steps_tests = [ + [ 50, 1 ], [ 25, 2 ], [ 33, 1 ], [ 22, 1 ], [ 99, 2 ], [ 81, 1 ], + [ 98, 3 ], [ 72, 2 ], [ 14, 1 ], + ]; + for my $test (@$multiplication_steps_tests) + { + my ( $input, $expected ) = @$test; + is_deeply $expected, single_digit_multiplication_steps($input), + "input: $input, expected: $expected"; + } + my $persistence_sort_tests = [ [ [ 15, 99, 1, 34 ], [ 1, 15, 34, 99 ] ] ]; + for my $test (@$persistence_sort_tests) + { + my ( $input, $expected ) = @$test; + my $output = [ persistence_sort(@$input) ]; + is_deeply $expected, $output, + "input: @$input, expected: @$expected, output: @$output"; + } + done_testing; +} + +test() unless caller(); +1; |
