diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-05-01 20:06:15 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-05-01 20:06:15 +0100 |
| commit | c6be7d34d3ed637697b41e15db176dcb40b84c9a (patch) | |
| tree | 3d96ded09b71698332b90b214e44d4c6535dddb1 /challenge-058 | |
| parent | 5f46c7d200bf20b2d1e0eacdc333903725fe5270 (diff) | |
| download | perlweeklychallenge-club-c6be7d34d3ed637697b41e15db176dcb40b84c9a.tar.gz perlweeklychallenge-club-c6be7d34d3ed637697b41e15db176dcb40b84c9a.tar.bz2 perlweeklychallenge-club-c6be7d34d3ed637697b41e15db176dcb40b84c9a.zip | |
- Ooops missed the solutions.
Diffstat (limited to 'challenge-058')
| -rw-r--r-- | challenge-058/laurent-rosenfeld/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-058/laurent-rosenfeld/perl/ch-1.pl | 20 | ||||
| -rw-r--r-- | challenge-058/laurent-rosenfeld/perl/ch-2.pl | 38 | ||||
| -rw-r--r-- | challenge-058/laurent-rosenfeld/raku/ch-1.p6 | 19 | ||||
| -rw-r--r-- | challenge-058/laurent-rosenfeld/raku/ch-2.p6 | 26 |
5 files changed, 104 insertions, 0 deletions
diff --git a/challenge-058/laurent-rosenfeld/blog.txt b/challenge-058/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..28de197452 --- /dev/null +++ b/challenge-058/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2020/05/-perl-weekly-challenge-58-compare-versions-and-ordered-lineup.html diff --git a/challenge-058/laurent-rosenfeld/perl/ch-1.pl b/challenge-058/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..7eef15dc6b --- /dev/null +++ b/challenge-058/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; +use feature "say"; +use Test::More tests => 5; + +sub cmp_versions { + my ($v1, $v2) = @_; + s/_/.00/g for ($v1, $v2); + my @a1 = split /[._]/, $v1; + my @a2 = split /[._]/, $v2; + $a1[2] = 0 unless defined $a1[2]; + $a2[2] = 0 unless defined $a2[2]; + $a1[0] <=> $a2[0] || $a1[1] cmp $a2[1] || $a1[2] cmp $a2[2]; +} + +is cmp_versions('0.1', '1.1'), -1, "Two-part version numbers"; +is cmp_versions('2.0', '1.2'), 1, "Two-part version numbers"; +is cmp_versions('1.2', '1.2.5'), -1, "Two-part and three-part version numbers"; +is cmp_versions('1.2.1', '1.2_1'), 1, "With underscore"; +is cmp_versions('1.2.1', '1.2.1'), 0, "Three-part version numbers"; diff --git a/challenge-058/laurent-rosenfeld/perl/ch-2.pl b/challenge-058/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..88d5e7e40b --- /dev/null +++ b/challenge-058/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,38 @@ +use strict; +use warnings; +use feature qw /say/; + +# Heights +my @H = qw/27 21 37 4 19 52 23 64 1 7 51 17 24 50 3 2 + 34 40 47 20 8 56 14 16 42 38 62 53 31 41 55 59 + 48 12 32 61 9 60 46 26 58 25 15 36 11 44 63 28 + 5 54 10 49 57 30 29 22 35 39 45 43 18 6 13 33/; + +# Number taller people in front +my @T = qw/6 41 1 49 38 12 1 0 58 47 4 17 26 1 61 12 + 29 3 4 11 45 1 32 5 9 19 1 4 28 12 2 2 + 13 18 19 3 4 1 10 16 4 3 29 5 49 1 1 24 + 2 1 38 7 7 14 35 25 0 5 4 19 10 13 4 12/; + +# mapping sizes to number of taller people before +my %mapping; +@mapping{@H} = @T; + +my @result; +for my $height (sort { $a <=> $b } @H) { + my $rank = $mapping{$height}; + # Looking for the right slot: we start with the + # number of taller people, and add 1 for each + # defined value before the place where we will + # end up placing the current item + my $i = 0; + while ($i <= $rank) { + $rank++ if defined $result[$i++]; + } + $result[$rank] = $height; +} +if (0 == grep { not defined $_ } @result) { + say "@result"; +} else { + say "No solution!"; +} diff --git a/challenge-058/laurent-rosenfeld/raku/ch-1.p6 b/challenge-058/laurent-rosenfeld/raku/ch-1.p6 new file mode 100644 index 0000000000..27af59b76f --- /dev/null +++ b/challenge-058/laurent-rosenfeld/raku/ch-1.p6 @@ -0,0 +1,19 @@ +use v6 +use Test; +plan 5; + +sub cmp-versions ($v1 is copy, $v2 is copy) { + constant %order = reverse Order.enums; + s:g/_/.00/ for $v1, $v2; + my @a1 = split /<[._]>/, $v1; + my @a2 = split /<[._]>/, $v2; + $_[2] = 0 unless defined $_[2] for @a1, @a2; + return %order{@a1[0] <=> @a2[0] || @a1[1] <=> @a2[1] + || @a1[2] cmp @a2[2]}; +} + +is cmp-versions('0.1', '1.1'), -1, "Two-part version numbers"; +is cmp-versions('2.0', '1.2'), 1, "Two-part version numbers"; +is cmp-versions('1.2', '1.2.5'), -1, "Two-part and three-part version numbers"; +is cmp-versions('1.2.1', '1.2_1'), 1, "With underscore"; +is cmp-versions('1.2.1', '1.2.1'), 0, "Three-part version numbers"; diff --git a/challenge-058/laurent-rosenfeld/raku/ch-2.p6 b/challenge-058/laurent-rosenfeld/raku/ch-2.p6 new file mode 100644 index 0000000000..744c6d19bc --- /dev/null +++ b/challenge-058/laurent-rosenfeld/raku/ch-2.p6 @@ -0,0 +1,26 @@ +use v6; + +# Heights +my @H = < 27 21 37 4 19 52 23 64 1 7 51 17 24 50 3 2 + 34 40 47 20 8 56 14 16 42 38 62 53 31 41 55 59 + 48 12 32 61 9 60 46 26 58 25 15 36 11 44 63 28 + 5 54 10 49 57 30 29 22 35 39 45 43 18 6 13 33 >; + +# Number taller people in front +my @T = < 6 41 1 49 38 12 1 0 58 47 4 17 26 1 61 12 + 29 3 4 11 45 1 32 5 9 19 1 4 28 12 2 2 + 13 18 19 3 4 1 10 16 4 3 29 5 49 1 1 24 + 2 1 38 7 7 14 35 25 0 5 4 19 10 13 4 12 >; + +# mapping sizes to number of taller people before +my %mapping; +%mapping{@H} = @T; + +my @result; +for @H.sort -> $height { + my $rank = %mapping{$height}; + my $i = 0; + $rank++ if defined @result[$i++] while $i <= $rank; + @result[$rank] = $height; +} +say 0 == (grep { ! defined $_ }, @result).elems ?? "@result[]" !! "No solution!"; |
