From 8997201a0016d11fb25e0d94e75d9b6d2cbcd221 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Apr 2020 08:14:25 +0200 Subject: First task done. --- challenge-058/luca-ferrari/raku/ch-1.p6 | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 challenge-058/luca-ferrari/raku/ch-1.p6 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 } + }; + + } +} -- cgit From 0ab7ec2eca9dcd7442a34039ca9bfcc25be58363 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Apr 2020 19:08:51 +0200 Subject: Task 2 done. --- challenge-058/luca-ferrari/raku/ch-2.p6 | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 challenge-058/luca-ferrari/raku/ch-2.p6 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; + } +} -- cgit From b1b74a5f9dd360dac35e9bb7408839ef2756d923 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Apr 2020 19:10:19 +0200 Subject: Blog links --- challenge-058/luca-ferrari/blog-1.txt | 1 + challenge-058/luca-ferrari/blog-2.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-058/luca-ferrari/blog-1.txt create mode 100644 challenge-058/luca-ferrari/blog-2.txt 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 -- cgit