aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2022-11-28 14:54:10 +0000
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2022-11-28 14:54:10 +0000
commit3b53c2fae5cc8a397cf144f7e44f184430cdf81c (patch)
treed7db43c6503ed70eead7758a9641cfcb88c0bfc5
parentc080faf96278bc39241afc30f80454b56345bd8c (diff)
downloadperlweeklychallenge-club-3b53c2fae5cc8a397cf144f7e44f184430cdf81c.tar.gz
perlweeklychallenge-club-3b53c2fae5cc8a397cf144f7e44f184430cdf81c.tar.bz2
perlweeklychallenge-club-3b53c2fae5cc8a397cf144f7e44f184430cdf81c.zip
Week 193 submission
-rw-r--r--challenge-193/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-193/peter-campbell-smith/perl/ch-1.pl27
-rwxr-xr-xchallenge-193/peter-campbell-smith/perl/ch-2.pl45
3 files changed, 73 insertions, 0 deletions
diff --git a/challenge-193/peter-campbell-smith/blog.txt b/challenge-193/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..ea345f5ca2
--- /dev/null
+++ b/challenge-193/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+https://pjcs-pwc.blogspot.com/2022/11/all-binaries-and-find-odd-man-out.html
diff --git a/challenge-193/peter-campbell-smith/perl/ch-1.pl b/challenge-193/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..5ea9f0cee5
--- /dev/null
+++ b/challenge-193/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-11-28
+# PWC 193 task 1
+
+use v5.28;
+use utf8;
+use warnings;
+
+# You are given an integer, $n > 0. Write a script to find all possible binary numbers of length $n.
+
+# Blog: https://pjcs-pwc.blogspot.com/2022/11/all-binaries-and-find-odd-man-out.html
+
+my (@tests, $n, $j, $string);
+
+@tests = (2, 3, 4, 10);
+
+# loop over tests
+for $n (@tests) {
+
+ # format as binary
+ $string = '';
+ $string .= sprintf("%0${n}b", $_) . ', ' for (0 .. 2 ** $n - 1);
+
+ # show output
+ say qq[\nInput: $n\nOutput: ] . substr($string, 0, -2);
+}
diff --git a/challenge-193/peter-campbell-smith/perl/ch-2.pl b/challenge-193/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..ac892671d6
--- /dev/null
+++ b/challenge-193/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-11-28
+# PWC 193 task 2
+
+use v5.28;
+use utf8;
+use warnings;
+
+# You are given a list of strings of same length, @s. Write a script to find the odd string in the given list.
+# Use positional value of alphabet starting with 0, i.e. a = 0, b = 1, ... z = 25.
+# Find the difference array for each string as shown in the examples. Then pick the odd one out.
+
+# Blog: https://pjcs-pwc.blogspot.com/2022/11/all-binaries-and-find-odd-man-out.html
+
+my (@tests, $test, @s, $i, $j, $diff, %seen, $s, $input);
+
+@tests = (["adc", "wzy", "abc"], ["aaa", "bob", "ccc", "ddd"], ['abcd', 'defg', 'hijk', 'fred'],
+ ['abcdefghij', 'bcdefghijk', 'bcdefghijj', 'nopqrstuvw']);
+
+for $test (@tests) {
+
+ # initialise
+ @s = @$test;
+ %seen = ();
+ $input = '';
+ $input .= qq["$_", ] for @s;
+ say qq[\nInput: \@s = (] . substr($input, 0, -2) . ')';
+
+ # loop over strings in list
+ for ($i = 0; $i < scalar @s; $i ++) {
+
+ # calculate difference
+ $diff = '';
+ for $j (0 .. length($s[0]) - 2) {
+ $diff .= ord(substr($s[$i], $j + 1, 1)) - ord(substr($s[$i], $j, 1)) . ', ';
+ }
+ $seen{$diff} .= qq[$s[$i]=];
+ }
+
+ # find the one which has been seen only once
+ for $s (keys %seen) {
+ say qq[Output: "$1"] if $seen{$s} =~ m|^([^=]*)=$|;
+ }
+}