diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-04-27 21:49:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-27 21:49:30 +0100 |
| commit | 31c6b18c4e68d2daaa67b9b99434d30790cceb73 (patch) | |
| tree | 80c3bb19bb2890abc0d2c169c26fcc01a4180524 | |
| parent | d3c14b339cbca7deb172be82c50007f7dd119085 (diff) | |
| parent | b1b74a5f9dd360dac35e9bb7408839ef2756d923 (diff) | |
| download | perlweeklychallenge-club-31c6b18c4e68d2daaa67b9b99434d30790cceb73.tar.gz perlweeklychallenge-club-31c6b18c4e68d2daaa67b9b99434d30790cceb73.tar.bz2 perlweeklychallenge-club-31c6b18c4e68d2daaa67b9b99434d30790cceb73.zip | |
Merge pull request #1641 from fluca1978/pwc-58
Pwc 58
| -rw-r--r-- | challenge-058/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-058/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-058/luca-ferrari/raku/ch-1.p6 | 45 | ||||
| -rw-r--r-- | challenge-058/luca-ferrari/raku/ch-2.p6 | 49 |
4 files changed, 96 insertions, 0 deletions
diff --git a/challenge-058/luca-ferrari/blog-1.txt b/challenge-058/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..1fa0fc8b8a --- /dev/null +++ b/challenge-058/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/04/27/PerlWeeklyChallenge58.html#task1 diff --git a/challenge-058/luca-ferrari/blog-2.txt b/challenge-058/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..8b9c526c0f --- /dev/null +++ b/challenge-058/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/04/27/PerlWeeklyChallenge58.html#task2 diff --git a/challenge-058/luca-ferrari/raku/ch-1.p6 b/challenge-058/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..4ade326bf0 --- /dev/null +++ b/challenge-058/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,45 @@ +#!env raku +# +# Perl Weekly Challenge 58 +# +# Task 1 +# +# Compare two given version number strings v1 and v2 such that: +# +# If v1 > v2 return 1 +# If v1 < v2 return -1 +# Otherwise, return 0 +# +# The version numbers are non-empty strings containing only digits, +# and the dot (“.”) and underscore (“_”) characters. +# (“_” denotes an alpha/development version, and has a lower precedence than a dot, “.”). Here are some examples: +# +# v1 v2 Result +# ------ ------ ------ +# 0.1 < 1.1 -1 +# 2.0 > 1.2 1 +# 1.2 < 1.2_5 -1 +# 1.2.1 > 1.2_1 1 +# 1.2.1 = 1.2.1 0 +# Version numbers may also contain leading zeros. You may handle these how you wish, as long as it’s consistent. + + +sub MAIN() { + my Str @test = '0.1', '1.1' + , '2.0', '1.2' + , '1.2', '1.2_5' + , '1.2.1', '1.2_1' + , '1.2.1', '1.2.1'; + + + for @test -> $v1, $v2 { + my $v1n = $v1.Str.subst( '.', 9, :g ).subst( '_', '0', :g ); + my $v2n = $v2.Str.subst( '.', 9, :g ).subst( '_', '0', :g ); + say "Comparing $v1 vs $v2 -> " ~ do given $v1n <=> $v2n { + when More { 1 } + when Less { -1 } + when Same { 0 } + }; + + } +} diff --git a/challenge-058/luca-ferrari/raku/ch-2.p6 b/challenge-058/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..656be20648 --- /dev/null +++ b/challenge-058/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,49 @@ +#!env raku +# +# Perl Weekly Challenge 58 +# +# Task 2 +# +# Write a script to arrange people in a lineup according to how many taller people are in front of each person in line. You are given two arrays. @H is a list of unique heights, in any order. @T is a list of how many taller people are to be put in front of the corresponding person in @H. The output is the final ordering of people’s heights, or an error if there is no solution. + +# Here is a small example: + +# @H = (2, 6, 4, 5, 1, 3) # Heights +# @T = (1, 0, 2, 0, 1, 2) # Number of taller people in front + +# The ordering of both arrays lines up, so H[i] and T[i] refer to the same person. For example, +# there are 2 taller people in front of the person with height 4, and there is 1 person in front of the person with height 1. +# +# As per the last diagram, your script would then output the ordering (5, 1, 2, 6, 3, 4) in this case. (The leftmost element is the “front” of the array.) + +sub MAIN(){ + my @H = 2, 6, 4, 5, 1, 3; + my @T = 1, 0, 2, 0, 1, 2; + + + # build an hash to map heights to tallers + my %HT; + for 0 ..^ @H.elems { + %HT{ @H[ $_ ] } = @T[ $_ ]; + } + + + # evaluate all possible solutions + for %HT.keys.permutations -> @solution { + # the leftmost element must have a taller set to zero! + next if %HT{ @solution[ 0 ] } != 0; + + # the solution is good if the number of tallers for + # every element is equal to the values of tallers + my $ok = True; + + for 1 ..^ @solution.elems { + my $height = @solution[ $_ ]; + my $tallers = %HT{ $height }; + $ok = False if @solution[ 0 .. $_-1].grep( * > $height ) != $tallers; + last if ! $ok; + } + + say @solution if $ok; + } +} |
