diff options
| author | 冯昶 <seaker@qq.com> | 2020-09-21 14:20:42 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2020-09-21 14:20:42 +0800 |
| commit | bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb (patch) | |
| tree | 877181cfde26b706346d3468269e4674d75da772 /challenge-079/james-smith | |
| parent | ec09b571a6f2186fec8870a071a8d5d38596c850 (diff) | |
| parent | 5ac16ac7e9826137e0da5597e954f4992c66205d (diff) | |
| download | perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.tar.gz perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.tar.bz2 perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-079/james-smith')
| -rw-r--r-- | challenge-079/james-smith/README.md | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/challenge-079/james-smith/README.md b/challenge-079/james-smith/README.md new file mode 100644 index 0000000000..74219c000c --- /dev/null +++ b/challenge-079/james-smith/README.md @@ -0,0 +1,60 @@ +Solutions by James Smith. + +# Challenge 1 - leader + +There are two solutions to this - the naive one works left to right, but the optimal one runs right to left. + +```perl +sub leader { ## Most efficient way is to work backwards on this rather + ## than forwards... + + return 0 unless @_; ## If nothing passed return 0 as requested. + + my @R = my $max = pop @_; ## Last one will always be a "leader"... + foreach ( reverse @_ ) { ## Work forward and unshift the value if it is now a leader + unshift @R, $max = $_ if $max < $_; ## greater than max value (and update max at the same time) + } + + return @R; ## Return array of leaders... +} +``` + +Notes: + + * This is an O(n) solution where the forward solution is O(n^2); + +# Challenge 2 - left rotate + +This is a simpler challenge as this is something Perl is even better at - rotating an array is just +an array slice of the array with the indicies itself rotated... + +The index rotation is easy: "offset" .. "size of list", 0 .. ("offset" - 1); + +So the code reduces to: + +```perl +sub rotate { + my $a = shift; + ## First parameter is an arrayref containing the values to be rotated + ## Remaining parameters are the offsets for each rotation + + ## This is a great use of array slicing to achieve what we need + ## Indexes go from offset -> size-1 & 0 -> offset-1 + ## Perl nicely handles the case where the value to the left of + ## the double dot is higher than the value to the right + + print " [@{[ @{$a}[ $_..(@{$a}-1), 0..($_-1) ] ]}]\n" foreach @_; + + ## Let us use the @{[ ]} trick to embed content into the print + ## statement - this is a very useful and often under used feature + ## of perl which makes for simpler code when rendering output... + ## this is nice because print "@A" an print @A are subtly different + ## with the "@A" putting spaces (default value of $") +} +``` + +Notes: + + * We use Arrayref slice notation @{ $arrayref }[ @index_list ] to do the rotation + + * We use "@{[ ... ]}" string interpolation to insert these values into the output... |
