diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-21 18:01:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-21 18:01:21 +0100 |
| commit | 47f9cdb9fbba49081a0424e02d07f20bff758bb3 (patch) | |
| tree | b1bc205137117554d428ef8d1ac5e956b186e645 | |
| parent | b8ab88c5e8d52fa1d06682edecdef62009b042ae (diff) | |
| parent | 2b31fc43b5dd0bf16296c3b2ea815901e5f5e261 (diff) | |
| download | perlweeklychallenge-club-47f9cdb9fbba49081a0424e02d07f20bff758bb3.tar.gz perlweeklychallenge-club-47f9cdb9fbba49081a0424e02d07f20bff758bb3.tar.bz2 perlweeklychallenge-club-47f9cdb9fbba49081a0424e02d07f20bff758bb3.zip | |
Merge pull request #10680 from pauloscustodio/master
Add Perl solution to challenge 238
| -rw-r--r-- | challenge-235/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-238/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-238/paulo-custodio/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-238/paulo-custodio/perl/ch-2.pl | 52 | ||||
| -rw-r--r-- | challenge-238/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-238/paulo-custodio/t/test-2.yaml | 10 |
6 files changed, 115 insertions, 0 deletions
diff --git a/challenge-235/paulo-custodio/t/test-1.yaml b/challenge-235/paulo-custodio/t/test-1.yaml index e33466a56d..2ea3c77996 100644 --- a/challenge-235/paulo-custodio/t/test-1.yaml +++ b/challenge-235/paulo-custodio/t/test-1.yaml @@ -13,3 +13,8 @@ args: 2 2 3 input: output: true +- setup: + cleanup: + args: 1 2 3 1 2 3 + input: + output: false diff --git a/challenge-238/paulo-custodio/Makefile b/challenge-238/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-238/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-238/paulo-custodio/perl/ch-1.pl b/challenge-238/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..197805173d --- /dev/null +++ b/challenge-238/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +# Challenge 238 +# +# Task 1: Running Sum +# Submitted by: Mohammad S Anwar +# +# 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) +# +# Example 2 +# +# Input: @int = (1, 1, 1, 1, 1) +# Output: (1, 2, 3, 4, 5) +# +# Example 3 +# +# Input: @int = (0, -1, 1, 2) +# Output: (0, -1, 0, 2) + +use Modern::Perl; + +my @n = @ARGV; +my $sum = 0; +say join " ", map {$sum += $_} @n; diff --git a/challenge-238/paulo-custodio/perl/ch-2.pl b/challenge-238/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..54a5589a8c --- /dev/null +++ b/challenge-238/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl + +# Challenge 238 +# +# Task 2: Persistence Sort +# Submitted by: Mohammad S Anwar +# +# 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) +# +# 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) + +use Modern::Perl; + +my @ints = @ARGV; +say join " ", + map {$_->[0]} + sort {$a->[1] == $b->[1] ? $a->[0] <=> $b->[0] : $a->[1] <=> $b->[1]} + map {[$_, count_steps($_)]} @ints; + +sub count_steps { + my($n) = @_; + my $steps = 0; + while ($n > 9) { + my @digits = split //, $n; + my $expr = join "*", @digits; + $n = eval($expr); die $@ if $@; + $steps++; + } + return $steps; +} diff --git a/challenge-238/paulo-custodio/t/test-1.yaml b/challenge-238/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..8d423ac596 --- /dev/null +++ b/challenge-238/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 3 4 5 + input: + output: 1 3 6 10 15 +- setup: + cleanup: + args: 1 1 1 1 1 + input: + output: 1 2 3 4 5 +- setup: + cleanup: + args: 0 -1 1 2 + input: + output: 0 -1 0 2 diff --git a/challenge-238/paulo-custodio/t/test-2.yaml b/challenge-238/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..2f19edb1ec --- /dev/null +++ b/challenge-238/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 15 99 1 34 + input: + output: 1 15 34 99 +- setup: + cleanup: + args: 50 25 33 22 + input: + output: 22 33 50 25 |
