aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-01-23 00:18:53 +0000
committerGitHub <noreply@github.com>2023-01-23 00:18:53 +0000
commit4a5cb6c83f7a527d4efecd7c932ca4bca153b178 (patch)
treeda328d0070cd62e093a0aa2bf75c9c78fda1d55c
parent5b43e2a86f5b7ab476fca4a77f69e2db8ceb2294 (diff)
parentf54e67106647e18a5b344450ab7f10c8d49750f6 (diff)
downloadperlweeklychallenge-club-4a5cb6c83f7a527d4efecd7c932ca4bca153b178.tar.gz
perlweeklychallenge-club-4a5cb6c83f7a527d4efecd7c932ca4bca153b178.tar.bz2
perlweeklychallenge-club-4a5cb6c83f7a527d4efecd7c932ca4bca153b178.zip
Merge pull request #7430 from pjcs00/wk200
Week 200 submission
-rw-r--r--challenge-200/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-200/peter-campbell-smith/perl/ch-1.pl79
-rwxr-xr-xchallenge-200/peter-campbell-smith/perl/ch-2.pl71
3 files changed, 151 insertions, 0 deletions
diff --git a/challenge-200/peter-campbell-smith/blog.txt b/challenge-200/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..25923544bf
--- /dev/null
+++ b/challenge-200/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+https://pjcs-pwc.blogspot.com/
diff --git a/challenge-200/peter-campbell-smith/perl/ch-1.pl b/challenge-200/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..d63fb8ea2c
--- /dev/null
+++ b/challenge-200/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2023-01-16
+# PWC 200 task 1
+
+use v5.28;
+use utf8;
+use warnings;
+
+# Task: Write a script to find out all Arithmetic Slices for the given array of integers. An integer array is
+# called arithmetic if it has at least 3 elements and the differences between any three consecutive elements
+# are the same.
+
+# Blog:
+
+my (@tests, $test, @array, $rubric, $j, @diff, $last_diff, $run_starts);
+
+@tests = ([1, 2, 3, 4], [1, 2, 3, 4, 6], [0, 1, 5, 9, 13, 22, 99, 101, 102, 103, 105],
+ [0, 0, 0], [-3, 1, 5, 9], [10, 8, 6, 4], [7, 8, 8, 10, 13, 17], [1, 2, 3, 6, 9],
+ [0, 4, 8, 12, 16, 20, 24, 28, 32]);
+
+# loop over tests
+for $test (@tests) {
+ @array = @$test;
+
+ # initialise
+ $rubric = '';
+ @diff = ();
+
+ # create @diff array and put a dummy value on the end
+ for $j (0 .. scalar(@array) - 2) {
+ $diff[$j] = $array[$j + 1] - $array[$j];
+ }
+ push @diff, 1e9;
+
+ # loop over the @diff array looking for runs of the same number
+ $last_diff = 1e9;
+ $run_starts = -1;
+ for $j (0 .. scalar(@array) - 1) {
+
+ # continuation of a run
+ next if $diff[$j] == $last_diff;
+
+ # end of a run, possible start of next one
+ analyse($run_starts, $j) if $run_starts >= 0;
+ $run_starts = $j;
+ $last_diff = $diff[$j];
+ }
+
+ # report result
+ say qq[\nInput: (] . join(', ', @array) . ')';
+ say qq[Output: ] . ($rubric ? substr($rubric, 0, -2) : '() as no slice found');
+}
+
+sub analyse {
+
+ # split a run into sub-runs (eg 1,2,3,4 => 1,2,3; 2,3,4; 1,2,3,4)
+ my ($start, $end) = @_;
+ my ($length, $sub_length, $offset, $j);
+
+ # minimum run length is 3
+ $length = $end - $start + 1;
+ return if $length < 3;
+
+ # loop over possible sub-run lengths
+ for $sub_length (3 .. $length) {
+
+ # loop over possible starting positions
+ for $offset (0 .. $length - $sub_length) {
+
+ # add sub-run to rubric
+ $rubric .= '(';
+ for $j ($start + $offset .. $start + $offset + $sub_length - 1) {
+ $rubric .= qq[$array[$j], ];
+ }
+ $rubric =~ s|, $|), |;
+ }
+ }
+}
diff --git a/challenge-200/peter-campbell-smith/perl/ch-2.pl b/challenge-200/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..b675ce9e64
--- /dev/null
+++ b/challenge-200/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2023-01-16
+# PWC 200 task 2
+
+use v5.28;
+use utf8;
+use warnings;
+
+# Task: Write a program that accepts any positive integer and draws that number as a horizontal sequence of
+# ASCII seven segment displays.
+
+# Blog:
+
+my (@tests, @truth, $test, @digit, @display, $offset, $points, $row, $column, $segments, $segment);
+
+@truth = qw[abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg];
+
+@tests = (200, 31415926535, 9876543210);
+
+# each digit is 7 lines high and 7 characters wide
+# each digit starts 9 characters further right than the previous one
+
+# create digits => character . row . column
+$digit[ord('a')] = '-00 -01 -02 -03 -04 -05 -06';
+$digit[ord('b')] = '|16 |26';
+$digit[ord('c')] = '|46 |56';
+$digit[ord('d')] = '-60 -61 -62 -63 -64 -65 -66';
+$digit[ord('e')] = '|40 |50';
+$digit[ord('f')] = '|10 |20';
+$digit[ord('g')] = '-30 -31 -32 -33 -34 -35 -36';
+
+# loop over tests
+for $test (@tests) {
+ @display = ();
+ $offset = 0;
+
+ # blank display
+ for $row (0 .. 6) {
+ for $column (0 .. length($test) * 9) {
+ $display[$row][$column] = ' ';
+ }
+ }
+
+ # loop over digits in $test and blank area of display
+ while ($test =~ m|(.)|g) { # digit
+
+ # loop over segments for this digit
+ $segments = $truth[$1];
+
+ # draw these segments in $display
+ while ($segments =~ m|(.)|g) {
+ $points = $digit[ord($1)];
+ while ($points =~ m|(.)(\d)(\d)|g) {
+ $display[$2][$3 + $offset] = $1;
+ }
+ }
+
+ # move right 9 characters width (7 for digit plus 2 blank)
+ $offset += 9;
+ }
+
+ # show the display
+ for $row (0 .. 6) {
+ for $column (0 .. $offset - 2) {
+ print $display[$row][$column];
+ }
+ say '';
+ }
+}
+