I’ve been doing the Weekly
Challenges. The
latest
involved combinations and counting. (Note that this ends today.)
Task 1: Complete Day
You are given an array of integers, @hours
.
Write a script to return the number of pairs that forms a complete day.
A complete day is defined as a time duration that is an exact
multiple of 24 hours.
A pretty strraightforward two-parter here:
(1) generate each possible 2-combination (using library functions)
(2) check all combination sums for mod 24 == 0.
Lua:
function completeday(a)
local out = 0
for _, c in combinations(a, 2) do
if (c[1] + c[2]) % 24 == 0 then
out = out + 1
end
end
return out
end
Task 2: Maximum Frequency
You are given an array of positive integers, @ints
.
Write a script to return the total frequencies of elements in the
given array such that those elements all have the maximum frequency.
And this one falls into three parts:
(1) get list into a freqency-counted list
(2) determine the maximum freqency
(3) see how many times that maximum frequency occurs.
Raku:
sub maximumfrequency(@a) {
Build the counted list:
my %c;
for @a -> $n {
%c{$n}++;
}
Find maximum frequency:
my $mx = %c.values.max;
Count occurrences.
return $mx * %c.values.grep({$_ == $mx}).elems;
}
Full code on
github.
Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.