diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-02-12 18:24:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-12 18:24:54 +0000 |
| commit | e47787e7d624decb8eea3640ace38b8773ef0613 (patch) | |
| tree | 00bd33ec9872c1220adb3b5f12a3c02ada1e8f72 | |
| parent | 578e20d72e394a9bdd4efc93fde8cee2e3528eb5 (diff) | |
| parent | 75d7e84e1952607b6f91a4cfa70d0be1ddfc2a61 (diff) | |
| download | perlweeklychallenge-club-e47787e7d624decb8eea3640ace38b8773ef0613.tar.gz perlweeklychallenge-club-e47787e7d624decb8eea3640ace38b8773ef0613.tar.bz2 perlweeklychallenge-club-e47787e7d624decb8eea3640ace38b8773ef0613.zip | |
Merge pull request #11568 from pjcs00/wk308
Week 308 - AND and XOR
| -rw-r--r-- | challenge-308/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-308/peter-campbell-smith/perl/ch-1.pl | 37 | ||||
| -rwxr-xr-x | challenge-308/peter-campbell-smith/perl/ch-2.pl | 32 |
3 files changed, 70 insertions, 0 deletions
diff --git a/challenge-308/peter-campbell-smith/blog.txt b/challenge-308/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..9231e18076 --- /dev/null +++ b/challenge-308/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/308 diff --git a/challenge-308/peter-campbell-smith/perl/ch-1.pl b/challenge-308/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..067390d1de --- /dev/null +++ b/challenge-308/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-02-10 +use utf8; # Week 308 - task 1 - Count common +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +count_common(['fred', 'jim', 'max', 'john'], ['john', 'bill', 'fred', 'alex']); +count_common(['fred', 'fred', 'fred'], ['joe', 'joe']); +count_common(['fred', 'fred', 'fred'], ['fred', 'fred', 'alice']); +count_common(['fred', 'fred', 'fred'], []); +count_common([], ['fred', 'fred', 'fred']); +count_common([], []); +count_common(['axe', 'bean', 'cabbage', 'dog', 'egg'], ['egg', 'axe', 'cabbage', 'bean', 'dog']); + +sub count_common { + + my (@str1, @str2, $one, $count, $s); + + @str1 = @{$_[0]}; + @str2 = @{$_[1]}; + $count = 0; + + # join @str1 into a string separated by ~ + $one = '~' . join('~', @str1) . '~'; + + # count times we can delete a member of @str2 from the string + for $s (@str2) { + $count ++ if $one =~ s|~$s~|~|; + } + + say qq[\nInput: \@str1 = ('] . join(q[', '], @str1) . qw[')]; + say qq[ \@str2 = ('] . join(q[', '], @str2) . qw[')]; + say qq[Output: $count]; +} diff --git a/challenge-308/peter-campbell-smith/perl/ch-2.pl b/challenge-308/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..7708ce32e1 --- /dev/null +++ b/challenge-308/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-02-10 +use utf8; # Week 308 - task 2 - Decode xor +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +decode_xor([1, 2, 3], 1); +decode_xor([6, 2, 7, 3], 4); +decode_xor([47, 21, 33, 8], 4); + +sub decode_xor { + + my (@e, $a, $b, $e, @result); + + # inputs + @e = @{$_[0]}; + $a = $_[1]; + $result[0] = $a; + + # loop over values of @encoded + for $e (@e) { + $b = $e ^ $a; + push @result, $b; + $a = $b; + } + + say qq[\nInput: \@encoded = (] . join(', ', @e) . qq[), \$integer = $_[1]]; + say qq[Output: (] . join(', ', @result) . ')'; +} |
