aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-10-13 15:04:54 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-10-13 15:04:54 +0100
commitaaded2bab8eac9197a35818fe0a0576467894f99 (patch)
tree67da4327f32ead71567510704dbbd3dda5cc2f7c
parentfea9757734113223c3f8702fd1c16fd774fef395 (diff)
downloadperlweeklychallenge-club-aaded2bab8eac9197a35818fe0a0576467894f99.tar.gz
perlweeklychallenge-club-aaded2bab8eac9197a35818fe0a0576467894f99.tar.bz2
perlweeklychallenge-club-aaded2bab8eac9197a35818fe0a0576467894f99.zip
pushed code for ch-1 &2
-rw-r--r--challenge-134/james-smith/perl/ch-1.pl37
-rw-r--r--challenge-134/james-smith/perl/ch-2.pl27
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-134/james-smith/perl/ch-1.pl b/challenge-134/james-smith/perl/ch-1.pl
new file mode 100644
index 0000000000..d18ca48222
--- /dev/null
+++ b/challenge-134/james-smith/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+use Benchmark qw(cmpthese timethis);
+use Data::Dumper qw(Dumper);
+
+my $S = 10;
+my @s = (1,0,2..$S-1); ## Cheat we jump into the first pemutation which
+ ## doesn't start with a "0" 1023456789 - avoids
+ ## us having to check for a leading 0...
+my $count=5;
+
+do {
+ say @s;
+} while next_perm && --$count;
+
+
+sub next_perm {
+ my($i,$j,$p);
+ ## Find largest index for which Si+1 > Si
+ ( $s[$_] < $s[$_+1] ) && ( $i=$_ ) foreach 0 .. $S-2; ## Find i
+
+ return unless defined $i; ## Got to the end of the list of permutations
+
+ ## Find latest index for which Sj > Si for j>i
+ ( $s[$i] < $s[$_] ) && ( $j=$_ ) foreach $i+1 .. $S-1; ## Find j
+
+ ## Flip i & jth elements..., then all numbers greater than i..
+ ( $s[$i], $s[$j] ) = ( $s[$j], $s[$i] ); ## Flip numbers over...
+ @s[ $i+1 .. $S-1 ] = @s[ reverse $i+1 .. $S-1 ]; ## Flip remaining list
+ return 1;
+}
+
diff --git a/challenge-134/james-smith/perl/ch-2.pl b/challenge-134/james-smith/perl/ch-2.pl
new file mode 100644
index 0000000000..bf886c8166
--- /dev/null
+++ b/challenge-134/james-smith/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+use Benchmark qw(cmpthese timethis);
+use Data::Dumper qw(Dumper);
+
+my @TESTS = (
+ [ [3,3], 6 ],
+ [ [3,5], 11 ],
+);
+
+is( distinct_terms(@{$_->[0]}), $_->[1] ) foreach @TESTS;
+
+done_testing();
+
+sub distinct_terms {
+ my($m,$n,%x) = @_;
+ for my $i (1..$m) {
+ $x{$i*$_}++ for 1..$n;
+ }
+ return scalar keys %x;
+}
+