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 | |
| 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')
| -rw-r--r-- | challenge-182/james-smith/README.md | 62 | ||||
| -rw-r--r-- | challenge-182/james-smith/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-182/james-smith/perl/ch-1.pl | 25 | ||||
| -rw-r--r-- | challenge-182/james-smith/perl/ch-2.pl | 31 |
4 files changed, 71 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) - diff --git a/challenge-182/james-smith/blog.txt b/challenge-182/james-smith/blog.txt new file mode 100644 index 0000000000..12cafb2abf --- /dev/null +++ b/challenge-182/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-182/james-smith diff --git a/challenge-182/james-smith/perl/ch-1.pl b/challenge-182/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..800616c949 --- /dev/null +++ b/challenge-182/james-smith/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ [5, 2, 9, 1, 7, 6], 2 ], + [ [4, 2, 3, 1, 5, 0], 4 ], +); + +is( max_index(@{$_->[0]}), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub max_index { + my $m=0; + $_[$_]>$_[$m] && ($m=$_) for 0..$#_; + $m; +} + diff --git a/challenge-182/james-smith/perl/ch-2.pl b/challenge-182/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..35312ecbe0 --- /dev/null +++ b/challenge-182/james-smith/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ [qw(/a/b/c/1/x.pl /a/b/c/d/e/2/x.pl /a/b/c/d/3/x.pl /a/b/c/4/x.pl /a/b/c/d/5/x.pl)], '/a/b/c' ], + [ [qw(/a/b/a.txt /a/b/c/d/e/q.txt /a/b/c.txt)], '/a/b' ], + [ [qw(/a/b.txt /c/d.txt)], '' ], + [ [qw(/a/q.txt)], '/a' ], + [ [qw(/aa/q.txt /ab/q.txt)], '' ], + [ [qw(/z/aa/q.txt /z/ab/q.txt)], '/z' ], +); + +is( common_path(@{$_->[0]}), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub common_path { + my $l = shift; + ## Compute the common string.... + $l = substr $l, 0, length( (($_^$l) =~ m{^(\0+)})[0]) for @_; + ## Remove the trailing "/"s + substr $l, 0, rindex $l, '/'; +} + |
