diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-07 23:16:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-07 23:16:31 +0100 |
| commit | 760586d6e41587ce42955ac4a68615be40a22130 (patch) | |
| tree | 263f387df59db033f4e919d25f5a967aafec90a9 | |
| parent | d8d1554685e5f0d531979a9630c15f4cfdb4760f (diff) | |
| parent | b68c286b0380e39dd628379867fae9c6ec06ace3 (diff) | |
| download | perlweeklychallenge-club-760586d6e41587ce42955ac4a68615be40a22130.tar.gz perlweeklychallenge-club-760586d6e41587ce42955ac4a68615be40a22130.tar.bz2 perlweeklychallenge-club-760586d6e41587ce42955ac4a68615be40a22130.zip | |
Merge pull request #8030 from drbaggy/master
Added short cutters
| -rw-r--r-- | challenge-215/james-smith/README.md | 44 | ||||
| -rw-r--r-- | challenge-215/james-smith/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-215/james-smith/perl/ch-2.pl | 14 |
3 files changed, 59 insertions, 27 deletions
diff --git a/challenge-215/james-smith/README.md b/challenge-215/james-smith/README.md index b5ed43623f..12dc6c8035 100644 --- a/challenge-215/james-smith/README.md +++ b/challenge-215/james-smith/README.md @@ -23,28 +23,33 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-215/ja To solve this problem we loop though each string to make sure the letters in alphabetical order. -We note that if the words are less than 3 characters long then they will be default in alphabetical order so we return 0. +We note that if the words are 1 character long then they will be default in alphabetical order so we return 0. -Looping through the letters - we start by getting the signum of the difference between the first two letters (and store in `$f`). We then loop through the remaining letters comparing letter by letter. +Looping through the letters - we just see if one is greater than or equal to the previous one - if it isn't we update the counter and move on to the next word. - * If the letters are the same we do nothing; - * If the first two letter are different then we update the update the value of `$f` with the difference between the letter - * If we find that `$f` has a different signum then wer add 1 to the count and jump to the end of the loop +Note we use a ternary to replace this `if`/`else` for compactness. ```perl sub non_alpha { - my $c = 0; - return 0 if length $_[0] <3; + return 0 if length $_[0] <2; + my($c,$f)=0; for(@_) { - my($f,$s,@rest)=split//; - $f = $f cmp $s; - ($s ne $_) && ($f ||= $s cmp $_) != ($s cmp $_) ? ($c++,last) - : ($s=$_) - for @rest; + $f=''; + $f gt $_ ? ($c++,last) : ($f=$_) for split //; } $c } +``` + +We can compact this by converting the inner `for` into a `map` - note the `last` was on the inner loop - and is the same as a `next` on the outer loop... So here we have to now use `next` not `laat` +```perl +sub non_alpha_compact { + return 0 if length $_[0] <2; + my($c,$f)=0; + $f='', map { $f gt $_ ? ($c++,next) : ($f=$_) } split // for @_; + $c +} ``` # TASK #2: Number Placement @@ -74,3 +79,18 @@ sub insert_zero_simultaneous { $c>0?0:1 } ``` + +We can get some performance improvements by short cutting the loop, by checking the value of $c at each stage rather than just at the end. This is most important if the number of inserts is relatively low in comparison to the size of the list. + +```perl +sub insert_zero_shortcut { + my($s,$c) = (0,shift); + $_ ? ( $c-= $s>2 && int(($s-1)/2), $s=0, $c>0 || return 1 ) : $s++ for @_,1; + 0; +} + +sub insert_zero_simultaneous_shortcut { + my($s,$c) = (0,shift); + $_ ? ( $c-= $s>2 && $s-2, $s=0, $c>0 || return 1 ) : $s++ for @_,1; + 0; +} diff --git a/challenge-215/james-smith/perl/ch-1.pl b/challenge-215/james-smith/perl/ch-1.pl index cf18bf5ca0..651cd7bf40 100644 --- a/challenge-215/james-smith/perl/ch-1.pl +++ b/challenge-215/james-smith/perl/ch-1.pl @@ -8,29 +8,27 @@ use Test::More; my @TESTS = ( [ ['abc', 'xyz', 'tsu'], 1 ], [ ['rat', 'cab', 'dad'], 3 ], - [ ['baa', 'ill', 'zzy', 'abc' ], 0 ], + [ ['baa', 'ill', 'zzy', 'abc' ], 2 ], [ ['x', 'y', 'z'], 0 ] ); -my @TESTS2 = ( - [ [ 1, [1,0,0,0,1] ], 1 ], - [ [ 2, [1,0,0,0,1] ], 0 ], - [ [ 3, [1,0,0,0,0,0,0,0,1] ], 1 ], - [ [ 3, [1,0,0,0,0,0,0,0] ], 1], -); - sub non_alpha { - my $c = 0; - return 0 if length $_[0] <3; + return 0 if length $_[0] <2; + my($c,$f)=0; for(@_) { - my($f,$s,@rest)=split//;say $_; - $f = $f cmp $s; - ($s ne $_) && ($f ||= $s cmp $_) != ($s cmp $_) - ? ($c++,last) - : ($s=$_) for @rest; + $f=''; + $f gt $_ ? ($c++,last) : ($f=$_) for split //; } $c } +sub non_alpha_compact { + return 0 if length $_[0] <2; + my($c,$f)=0; + $f='', map { $f gt $_ ? ($c++,next) : ($f=$_) } split // for @_; + $c +} + + is( non_alpha( @{$_->[0]} ), $_->[1] ) for @TESTS; done_testing(); diff --git a/challenge-215/james-smith/perl/ch-2.pl b/challenge-215/james-smith/perl/ch-2.pl index f01950dbd8..88eb2787e7 100644 --- a/challenge-215/james-smith/perl/ch-2.pl +++ b/challenge-215/james-smith/perl/ch-2.pl @@ -24,7 +24,21 @@ sub insert_zero_simultaneous { $c>0?0:1 } +sub insert_zero_shortcut { + my($s,$c) = (0,shift); + $_ ? ( $c-= $s>2 && int(($s-1)/2), $s=0, $c>0 || return 1 ) : $s++ for @_,1; + 0; +} + +sub insert_zero_simultaneous_shortcut { + my($s,$c) = (0,shift); + $_ ? ( $c-= $s>2 && $s-2, $s=0, $c>0 || return 1 ) : $s++ for @_,1; + 0; +} + is( insert_zero( $_->[0][0], @{$_->[0][1]} ), $_->[1] ) for @TESTS; is( insert_zero_simultaneous( $_->[0][0], @{$_->[0][1]} ), $_->[1] ) for @TESTS; +is( insert_zero_shortcut( $_->[0][0], @{$_->[0][1]} ), $_->[1] ) for @TESTS; +is( insert_zero_simultaneous_shortcut( $_->[0][0], @{$_->[0][1]} ), $_->[1] ) for @TESTS; done_testing(); |
