diff options
Diffstat (limited to 'challenge-106')
| -rw-r--r-- | challenge-106/pete-houston/awk/ch-1.awk | 41 | ||||
| -rw-r--r-- | challenge-106/pete-houston/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-106/pete-houston/perl/ch-2.pl | 62 |
3 files changed, 130 insertions, 0 deletions
diff --git a/challenge-106/pete-houston/awk/ch-1.awk b/challenge-106/pete-houston/awk/ch-1.awk new file mode 100644 index 0000000000..2c2972bdd5 --- /dev/null +++ b/challenge-106/pete-houston/awk/ch-1.awk @@ -0,0 +1,41 @@ +#!/usr/bin/gawk -f
+#===============================================================================
+#
+# FILE: 10601.awk
+#
+# USAGE: ./10601.awk < infile
+#
+# DESCRIPTION: Output maximum difference between adjacent integers.
+# Input should be a set of integers separated by
+# whitespace (incl. newlines)
+#
+# REQUIREMENTS: Inputs must all be integers otherwise they will be
+# skipped
+# NOTES: Tested on gawk 4.1.1
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 30/03/21
+#===============================================================================
+
+{
+ for (i = 1; i <= NF; i++) {
+ k = NR "." i
+ if (1 == match ($i, /-?[0-9]+/)) {
+ ints[k] = $i
+ }
+ }
+}
+END {
+ asort (ints, s, "@val_num_asc")
+ max = 0
+ last = "z"
+ for (n in s) {
+ if (last != "z") {
+ diff = s[n] - last
+ if (diff > max) { max = diff }
+ }
+ last = s[n]
+ }
+ print "\n" max
+}
diff --git a/challenge-106/pete-houston/perl/ch-1.pl b/challenge-106/pete-houston/perl/ch-1.pl new file mode 100644 index 0000000000..caa6be7348 --- /dev/null +++ b/challenge-106/pete-houston/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 10601.pl +# +# USAGE: ./10601.pl N [ N ... ] +# +# DESCRIPTION: Output maximum difference between adjacent integers. +# +# REQUIREMENTS: Arguments must all be integers otherwise they will be +# skipped +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 29/03/21 +#=============================================================================== + +use strict; +use warnings; + +my @ints = sort { $a <=> $b } grep { /^-?\d+$/ } @ARGV; +my $max = 0; +for my $i (1 .. $#ints) { + my $diff = $ints[$i] - $ints[$i - 1]; + $max = $diff if $diff > $max; +} +print "$max\n"; diff --git a/challenge-106/pete-houston/perl/ch-2.pl b/challenge-106/pete-houston/perl/ch-2.pl new file mode 100644 index 0000000000..00387ab138 --- /dev/null +++ b/challenge-106/pete-houston/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 10602.pl +# +# USAGE: ./10602.pl NUMERATOR DENOMINATOR +# +# DESCRIPTION: Print decimal quotient with bracketed recurrences +# +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 29/03/21 +#=============================================================================== + +use strict; +use warnings; + +my ($n, $d) = @ARGV; +die "Infinity\n" unless $d; +my $q = '' . ($n / $d); + +# Look for recurrences in the fractional part +my ($int, $dec) = split /\./, $q; +my $maxlen = length ($dec); +my @recur; + +OUTER: for my $charno (1 .. int ($maxlen / 2)) { + for my $init (0 .. $charno - 1) { + my $pos = $init; + my $reps = 0; + my $start = $pos; + my $pat = substr ($dec, $pos, $charno); + while ($pos < $maxlen - 2 * $charno) { + $pos += $charno; + my $next = substr ($dec, $pos, $charno); + if ($next eq $pat) { + $reps++; + } else { + $pat = $next; + $start = $pos; + $reps = 0; + } + } + if ($reps) { + push @recur, { start => $start, reps => $reps, pat => $pat }; + } + } +} + +if (1 < @recur) { + @recur = sort { + $a->{start} <=> $b->{start} + || + $b->{reps} <=> $a->{reps} + } @recur; +} +if (@recur) { + $dec = substr ($dec, 0, $recur[0]->{start}) . "($recur[0]->{pat})"; + +} +print "$int.$dec\n"; |
