blob: 74219c000cd89850f10fa92a954a914a1640237d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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...
|