diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-18 14:38:50 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-18 14:38:50 +0100 |
| commit | b3d6c27e1de42ddfb8ab2df69083a4cea12f8ca8 (patch) | |
| tree | c76b3f23d677d32441eab54db768522f7fe12773 | |
| parent | 4d7e6b0ab91d0ee7edf3f6656c80729a7e6c9156 (diff) | |
| download | perlweeklychallenge-club-b3d6c27e1de42ddfb8ab2df69083a4cea12f8ca8.tar.gz perlweeklychallenge-club-b3d6c27e1de42ddfb8ab2df69083a4cea12f8ca8.tar.bz2 perlweeklychallenge-club-b3d6c27e1de42ddfb8ab2df69083a4cea12f8ca8.zip | |
Add Perl solution to challenge 224
| -rw-r--r-- | challenge-223/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-224/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-224/paulo-custodio/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-224/paulo-custodio/perl/ch-2.pl | 65 | ||||
| -rw-r--r-- | challenge-224/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-224/paulo-custodio/t/test-2.yaml | 15 |
6 files changed, 135 insertions, 2 deletions
diff --git a/challenge-223/paulo-custodio/perl/ch-2.pl b/challenge-223/paulo-custodio/perl/ch-2.pl index 433eb4daf1..fadbbeb521 100644 --- a/challenge-223/paulo-custodio/perl/ch-2.pl +++ b/challenge-223/paulo-custodio/perl/ch-2.pl @@ -60,5 +60,3 @@ sub collect { $collect *= $box[$i+1] if $i+1 < @box; return $collect; } - - diff --git a/challenge-224/paulo-custodio/Makefile b/challenge-224/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-224/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-224/paulo-custodio/perl/ch-1.pl b/challenge-224/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..09c5d5d636 --- /dev/null +++ b/challenge-224/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +# Challenge 224 +# +# Task 1: Special Notes +# Submitted by: Mohammad S Anwar +# You are given two strings, $source and $target. +# +# Write a script to find out if using the characters (only once) from source, +# a target string can be created. +# +# Example 1 +# Input: $source = "abc" +# $target = "xyz" +# Output: false +# Example 2 +# Input: $source = "scriptinglanguage" +# $target = "perl" +# Output: true +# Example 3 +# Input: $source = "aabbcc" +# $target = "abc" +# Output: true + +use Modern::Perl; + +@ARGV==2 or die "Usage: $0 source target\n"; +my($source, $target) = @ARGV; + +say can_make_string($target, $source) ? "true" : "false"; + +sub can_make_string { + my($str, $chars) = @_; + for my $ch (split //, $chars) { + $str =~ s/$ch//i; + } + return $str eq ''; +} diff --git a/challenge-224/paulo-custodio/perl/ch-2.pl b/challenge-224/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..fa079770d8 --- /dev/null +++ b/challenge-224/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl + +# Challenge 224 +# +# Task 2: Additive Number +# Submitted by: Mohammad S Anwar +# You are given a string containing digits 0-9 only. +# +# Write a script to find out if the given string is additive number. An additive +# number is a string whose digits can form an additive sequence. +# +# A valid additive sequence should contain at least 3 numbers. Except the first +# 2 numbers, each subsequent number in the sequence must be the sum of the +# preceding two. +# +# +# Example 1: +# Input: $string = "112358" +# Output: true +# +# The additive sequence can be created using the given string digits: 1,1,2,3,5,8 +# 1 + 1 => 2 +# 1 + 2 => 3 +# 2 + 3 => 5 +# 3 + 5 => 8 +# Example 2: +# Input: $string = "12345" +# Output: false +# +# No additive sequence can be created using the given string digits. +# Example 3: +# Input: $string = "199100199" +# Output: true +# +# The additive sequence can be created using the given string digits: 1,99,100,199 +# 1 + 99 => 100 +# 99 + 100 => 199 + +use Modern::Perl; + +@ARGV==1 or die "Usage: $0 num\n"; +my $num = shift; +my $result = 0; +is_addictive_seq(\$result, [], $num); +say $result ? "true" : "false"; + +sub is_addictive_seq { + my($result, $seq, $num) = @_; + + if (@$seq >= 3 && $seq->[-3]+$seq->[-2]!=$seq->[-1]) { + return; # not a sequence + } + elsif (@$seq >= 3 && $num eq '') { + $$result = 1; # found solution + return; + } + elsif ($num eq '') { + return; # no solution + } + else { # add one more number to sequence + for my $i (1..length($num)) { + is_addictive_seq($result, [@$seq, 0+substr($num,0,$i)], substr($num,$i)); + } + } +} diff --git a/challenge-224/paulo-custodio/t/test-1.yaml b/challenge-224/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..568f80d8d0 --- /dev/null +++ b/challenge-224/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: abc xyz + input: + output: false +- setup: + cleanup: + args: scriptinglanguage perl + input: + output: true +- setup: + cleanup: + args: aabbcc abc + input: + output: true diff --git a/challenge-224/paulo-custodio/t/test-2.yaml b/challenge-224/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..c6ec8b6f23 --- /dev/null +++ b/challenge-224/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 112358 + input: + output: true +- setup: + cleanup: + args: 12345 + input: + output: false +- setup: + cleanup: + args: 199100199 + input: + output: true |
