aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-06-28 18:00:16 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-06-28 18:00:16 +0100
commitdc4693c0c75d2a4eda1882dd4f6b3128c08f349c (patch)
treeda7c6caf920a901b856ea18672e987b083a0c201
parent55477e94d571fcefcc949906b08a034936d1b96a (diff)
downloadperlweeklychallenge-club-dc4693c0c75d2a4eda1882dd4f6b3128c08f349c.tar.gz
perlweeklychallenge-club-dc4693c0c75d2a4eda1882dd4f6b3128c08f349c.tar.bz2
perlweeklychallenge-club-dc4693c0c75d2a4eda1882dd4f6b3128c08f349c.zip
updated object with notes
-rw-r--r--challenge-119/james-smith/perl/ch-2.pl28
1 files changed, 24 insertions, 4 deletions
diff --git a/challenge-119/james-smith/perl/ch-2.pl b/challenge-119/james-smith/perl/ch-2.pl
index bfb76d1534..ffa5b6c22f 100644
--- a/challenge-119/james-smith/perl/ch-2.pl
+++ b/challenge-119/james-smith/perl/ch-2.pl
@@ -10,6 +10,7 @@ use Data::Dumper qw(Dumper);
my @TESTS = ( [5,13],[10,32],[60,2223] );
+is( no_11_fr($_->[0]), $_->[1] ) foreach @TESTS;
is( no_11_filter($_->[0]), $_->[1] ) foreach @TESTS;
is( no_11_filter_regex($_->[0]), $_->[1] ) foreach @TESTS;
is( no_11_array( $_->[0]), $_->[1] ) foreach @TESTS;
@@ -17,6 +18,7 @@ is( no_11_object( $_->[0]), $_->[1] ) foreach @TESTS;
cmpthese(10_000,{
'filter' => sub { no_11_filter($_->[0]) foreach @TESTS; },
+ 'filter' => sub { no_11_filter($_->[0]) foreach @TESTS; },
'object' => sub { no_11_object($_->[0]) foreach @TESTS; },
'regex' => sub { no_11_filter_regex($_->[0]) foreach @TESTS; },
'array' => sub { no_11_array( $_->[0]) foreach @TESTS; },
@@ -53,6 +55,15 @@ sub no_11_filter {
&& 0 > index( $v,'11');
}
}
+sub no_11_fr {
+ my $n = shift;
+ my $v = 0;
+ while(1) {
+ return $v unless $n;
+ $v++;
+ $n-- if $v =~ m{\A(?:12|13|2|3)*1?\Z};
+ }
+}
sub no_11_filter_regex {
my $n = shift;
my $v = 0;
@@ -77,29 +88,38 @@ use overload '++' => 'incr';
use overload '""' => 'str';
sub new {
- my $x = [0];
+ my $x = [];
bless $x, 'Three';
return $x;
}
sub has_double_one {
+## Does number contain "11"
my($f,@v) = @{$_[0]};
while(@v) {
- return 1 if ($f == 1) && $v[0] == 1;
+ return 1 if $f == 1 && $v[0] == 1;
$f = shift @v;
}
return 0;
}
+
sub incr {
+## Override for "++" get next element in sequence
my $v = shift;
- my $ptr;
+ my $ptr = -1;
+ ## This is just a case of adding 1 to the last number
+ ## and rolling carries...
for( $ptr = @{$v}-1; $ptr>-1 && ++$v->[$ptr]>3; $ptr--) {
$v->[$ptr]=1;
}
- unshift @{$v},1 if $ptr < 0;
+ ## And finally add a "1" to the start if we have rolled
+ ## over the last bit 3 -> 11; 33->111; 333->1111 etc
+ unshift @{$v}, 1 if $ptr < 0;
}
sub str {
+ ## Override for "stringification" just
+ ## joins numbers into a string
return join '',@{$_[0]};
}