diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-30 22:29:59 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-30 22:29:59 +0100 |
| commit | fc14ab2b2f3a2358614aefea69a229ced42f90a7 (patch) | |
| tree | c5a64a2828be65f51da1819d8a9decae43b02fb3 | |
| parent | 72bea83e2380f0a794c3c9c515238eeadeba27f7 (diff) | |
| parent | b21d7670b9285094362e3451f4e92a98f8d5f0c3 (diff) | |
| download | perlweeklychallenge-club-fc14ab2b2f3a2358614aefea69a229ced42f90a7.tar.gz perlweeklychallenge-club-fc14ab2b2f3a2358614aefea69a229ced42f90a7.tar.bz2 perlweeklychallenge-club-fc14ab2b2f3a2358614aefea69a229ced42f90a7.zip | |
Merge pull request #10522 from pjcs00/wk280
Week 280 - Counting the stars
| -rw-r--r-- | challenge-280/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-280/peter-campbell-smith/perl/ch-1.pl | 32 | ||||
| -rwxr-xr-x | challenge-280/peter-campbell-smith/perl/ch-2.pl | 29 |
3 files changed, 62 insertions, 0 deletions
diff --git a/challenge-280/peter-campbell-smith/blog.txt b/challenge-280/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..a2a7a7403b --- /dev/null +++ b/challenge-280/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/280 diff --git a/challenge-280/peter-campbell-smith/perl/ch-1.pl b/challenge-280/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..97ac70bf73 --- /dev/null +++ b/challenge-280/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-07-29 +use utf8; # Week 280 - task 1 - Twice appearance +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +twice_appearance('acbddbca'); +twice_appearance('abccd'); +twice_appearance('abcdabbb'); +twice_appearance('aabcdef'); +twice_appearance('abcdeff'); +twice_appearance('soporific'); + +sub twice_appearance { + + my ($str, $j, $a); + $str = shift; + + # iterate along string + for $j (0 .. length($str) - 1) { + $a = substr($str, $j, 1); + + # check if this letter appears twice + last if $str =~ m|$a.*$a|; + $a = ''; + } + printf(qq[\nInput: \@str = '%s'\n], $str); + printf(qq[Output: '%s'\n], $a); +} diff --git a/challenge-280/peter-campbell-smith/perl/ch-2.pl b/challenge-280/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..d7ec13c6b3 --- /dev/null +++ b/challenge-280/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-07-29 +use utf8; # Week 280 - task 2 - Count asterisks +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +count_asterisks('p|*e*rl|w**e|*ekly|'); +count_asterisks('perl'); +count_asterisks('th|ewe|e**|k|l***ych|alleng|e'); +count_asterisks('*****|*****|*****|*****|'); + +sub count_asterisks { + + my ($str); + + $str = shift @_; + printf(qq[\nInput: \@str = '%s'\n], $str); + + # remove |.*?| from $str + $str =~ s/\|.*?\|//g; + + # remove non * from $str + $str =~ s|[^\*]||g; + + printf(qq[Output: %s\n], length($str)); +} |
