From 22e94ab37f455c7803eeb11b891a867b91f33826 Mon Sep 17 00:00:00 2001 From: Noud Aldenhoven Date: Fri, 6 Mar 2020 17:39:25 +0100 Subject: Solutions to challenge 050 task 1 and 2 in Raku by Noud --- challenge-050/noud/raku/ch-1.p6 | 37 +++++++++++++++++++++++++++++++ challenge-050/noud/raku/ch-2.p6 | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 challenge-050/noud/raku/ch-1.p6 create mode 100644 challenge-050/noud/raku/ch-2.p6 diff --git a/challenge-050/noud/raku/ch-1.p6 b/challenge-050/noud/raku/ch-1.p6 new file mode 100644 index 0000000000..830194163f --- /dev/null +++ b/challenge-050/noud/raku/ch-1.p6 @@ -0,0 +1,37 @@ +# Merge Intervals +# +# Write a script to merge the given intervals where ever possible. +# +# [2,7], [3,9], [10,12], [15,19], [18,22] +# +# The script should merge [2, 7] and [3, 9] together to return [2, 9]. +# +# Similarly it should also merge [15, 19] and [18, 22] together to return [15, +# 22]. +# +# The final result should be something like below: +# +# [2, 9], [10, 12], [15, 22] +# + +sub merge-int(@intervals) { + if (@intervals.elems < 2) { + return @intervals; + } + + # Sort intervals ascending. + @intervals = @intervals.sort; + + my ($a1, $a2) = @intervals.head; + for 1..^@intervals.elems -> $i { + if ($a2 < @intervals[$i][0]) { + return [($a1, max($a2, @intervals[$i-1][1])), + |(merge-int(@intervals[$i..*]))]; + } + } + + return [($a1, max($a2, @intervals[*-1][1])),]; +} + +say merge-int([(2, 3), (-1, 2), (5, 6)]); +say merge-int([(2, 7), (3, 9), (10, 12), (15, 19), (18, 22)]); diff --git a/challenge-050/noud/raku/ch-2.p6 b/challenge-050/noud/raku/ch-2.p6 new file mode 100644 index 0000000000..2ebe3172dd --- /dev/null +++ b/challenge-050/noud/raku/ch-2.p6 @@ -0,0 +1,48 @@ +# Noble Integer +# +# You are given a list, @L, of three or more random integers between 1 and 50. +# A Noble Integer is an integer N in @L, such that there are exactly N integers +# greater than N in @L. Output any Noble Integer found in @L, or an empty list +# if none were found. +# +# An interesting question is whether or not there can be multiple Noble +# Integers in a list. +# +# For example, +# +# Suppose we have list of 4 integers [2, 6, 1, 3]. +# +# Here we have 2 in the above list, known as Noble Integer, since there are +# exactly 2 integers in the list i.e.3 and 6, which are greater than 2. +# +# Therefore the script would print 2. + +sub noble-int(@L) { + return @L.grep(-> $n { @L.grep({ $_ > $n }).elems == $n }).unique; +} + +my @L1 = [2, 6, 1, 3]; +say @L1; +say noble-int(@L1); + +my @L2 = (50.rand.Int for 1..100); +say @L2; +say noble-int(@L2); + +# An interesting question is whether or not there can be multiple Noble +# Integers in a list. +# +# Answer: Depends on your definition of multiple. For example, the list +# +# [2, 2, 6, 1, 3] +# +# contains two Noble integers, 2 and 2. If you exclude non-unique examples I +# think there are no multiple Noble integers. Suppose there would be a list +# with multiple (unique) Noble integers. Let N be the smallest Noble integer in +# the list. I.e. there are N integers larger than N in the list. Let M be +# another Noble integer in the list. I.e. there are M integers larger than M in +# the list. All integers larger than M are also larger than N, so the number +# of integers larger than M is less than N. Hence M < N. But that contradicts +# the assumption than N was the smallest Noble integer, i.e. we just found a +# smaller Noble integer than N. Therefore, by contraction, there can only be +# one (unique) Noble integer in the list. -- cgit