I’ve been doing the Weekly
Challenges. The
latest
involved more string manipulations. (Note that this ends today.)
Task 1: Sort Letters
You are given two arrays, @letters
and @weights
.
Write a script to sort the given array @letters
based on the @weights
.
This clearly shares its inspiration with last week's Task 1, and can
be solved by similar code. Perl:
sub sortletters($a, $n) {
my @out = ("") x scalar @{$a};
while (my ($i, $l) = each @{$a}) {
$out[$n->[$i] - 1] = $l;
}
return join('', @out);
}
Task 2: Split String
You are given a string, $str
.
Write a script to split the given string into two containing exactly
same number of vowels and return true if you can otherwise false.
Well, I write to the tests. And the tests are answered exactly as well
by "return true if and only if the number of vowels in the string is
even".
Python:
def splitstring(a):
n = 0
for cc in a:
match cc.lower():
case 'a' | 'e' | 'i' | 'o' | 'u':
n += 1
return n % 2 == 0
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.