diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-09-12 06:33:14 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-09-12 06:33:14 +0100 |
| commit | 41f624e2bd19f232bb4691f8f234e16b1fc3592f (patch) | |
| tree | f50d300693fc51cf6034ddca7a4aa5c43bda035d /challenge-182/james-smith/README.md | |
| parent | 01526b67494ca6a4d95568de3083b38ffdf13d9d (diff) | |
| download | perlweeklychallenge-club-41f624e2bd19f232bb4691f8f234e16b1fc3592f.tar.gz perlweeklychallenge-club-41f624e2bd19f232bb4691f8f234e16b1fc3592f.tar.bz2 perlweeklychallenge-club-41f624e2bd19f232bb4691f8f234e16b1fc3592f.zip | |
first pass
Diffstat (limited to 'challenge-182/james-smith/README.md')
| -rw-r--r-- | challenge-182/james-smith/README.md | 62 |
1 files changed, 14 insertions, 48 deletions
diff --git a/challenge-182/james-smith/README.md b/challenge-182/james-smith/README.md index 744dd8f15c..f38d21ecd9 100644 --- a/challenge-182/james-smith/README.md +++ b/challenge-182/james-smith/README.md @@ -1,7 +1,7 @@ -[< Previous 180](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-180/james-smith) | -[Next 182 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-182/james-smith) +[< Previous 181](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-181/james-smith) | +[Next 183 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-183/james-smith) -# The Weekly Challenge 181 +# The Weekly Challenge 182 You can find more information about this weeks, and previous weeks challenges at: @@ -13,66 +13,32 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-181/james-smith +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-182/james-smith # Task 1 - Sentence Order -***You are given a paragraph. Write a script to order each sentence alphanumerically and print the whole paragraph.*** +***You are given a list of integers. Write a script to find the index of the first biggest number in the list.*** ## Solution ```perl -sub parse { - ( join '. ', - map { join ' ', sort { lc($a) cmp lc($b) || $a cmp $b } split } - split /[.]\s*/, $_[0] - ).'.' +sub max_index { + my $m=0; + $_[$_]>$_[$m] && ($m=$_) for 0..$#_; + $m; } ``` -Lets work backwards through the `parse` function. - - * We first chunk into sentences `split /[.]\s*/` - * For each sentence we split into words `split` (Which is split `$_` on whitespace if no parameters passed) - * Then we sort the words - primarily by *lexical order* - and if the word appers twice we sort in *ASCII* order - * We join back each word into a sentence - * We join the sentences back into the paragraph - * Finally we add the trailing full-stop which we have removed... - # Task 2 - Hot Day -***You are given file with daily temperature record in random order. Write a script to find out days hotter than previous day.*** - -## Assumption - -Even though data is an a random order we will assume that all dates are present between the start and end - as the problem is ill-defined otherwise. {You could compare dates and either give warnings or start new sequences. +***Given a list of absolute Linux file paths, determine the deepest path to the directory that contains all of them.*** ## Solution -We will split the code in two - - * first parses the file and stores it in date sorted order - * second looks for the *hot days* - -```perl -sub get_file { - open my $fh, q(<), $_[0]; - map { m{(\d{4}-\d\d-\d\d),\s+(\d+)}?[$1,$2]:() } sort <$fh> -} -``` - -To sort the file into date order we just need to sort the lines of the file - as the "prefix" is date. So `get_file`: - - * opens the file - * sorts the lines - in string order - * then parses each line into date and temperature - if the line is not in the right format we ignore the line - the callback returns `()`. - ```perl -sub hot_day { - my $day = shift; - map { $_->[1] > $day->[1] ? $_->[0] : (), ($day=$_)x 0 } @_ +sub common_path { + my $l = shift; + $l = substr $l, 0, length( (($_^$l) =~ m{^(\0+)})[0]) for @_; + substr $l, 0, rindex $l, '/'; } ``` - -`hot_day` just loops through those entries and outputs the hot days. We again use the trick of returning the empty list to turn the `map` into a `grep`. We update `$day` in loop, but this would get returned so we use the `x 0` trick to make this an empty list ( `x 0` on an array repeats in `0` times) - |
