I’ve been doing the Weekly
Challenges. The
latest
involved mucking about with words. (Note that this is open until 14
May 2023.)
Task 1: Registration Number
You are given a list of words and a random registration number.
Write a script to find all the words in the given list that has
every letter in the given registration number.
This is a fiddly definition. As I read it: for each word, if every
letter from the registration is found in the word, add it to the
output. It doesn't matter whether the candidate word has other letters
that aren't in the registration. In Ruby:
Helper function: given a word, lower-case it, and return a set of all
the letters present in it.
def word2set(word)
return Set.new(word.downcase.chars.find_all {|i| i >= 'a' && i <= 'z'})
end
Main function:
def registrationnumber(words, reg)
out = []
words.each do |w|
Build the set of letters in the registration. (In some languages I
built this once and cloned the set for each pass.)
ss = word2set(reg)
Use the same helper function to get a list of candidate letters. For
each of them,
word2set(w).each do |char|
If the letter is in the set,
if ss.include?(char) then
delete it from the set,
ss.delete(char)
and if the set is empty
if ss.length == 0 then
note the "matching" word and go on to the next one.
out.push(w)
break
end
end
end
end
return out
end
Task 2: Word Stickers
You are given a list of word stickers and a target word.
Write a script to find out how many word stickers is needed to make
up the given target word.
Each "sticker" is a set of letters which can be used individually, and
there's an unlimited supply of each one. To me the obvious data
structure is a hash with keys as the letters and values as the count
of available letters. This can of course be done with a standard hash,
but where a specialised class was readily available I used it; in Raku
there's the BagHash
, in Python the Counter
, and the latter
inspired a similar crate for rust.
In Perl, though:
sub wordstickers($stickers, $word) {
Set up the hash of letters and counts in the target word. (Quite
similar to building the set in task 1.)
my %w;
map {$w{$_}++} split '',lc($word);
Also make a copy of this to check whether the target can be made at
all.
my $t = dclone(\%w);
my @stick;
For each sticker,
foreach my $s (@{$stickers}) {
Set up its hash of letters and counts.
my %f;
map {$f{$_}++} split '',lc($s);
Also delete from t
the letters present in the sticker.
map {delete $t->{$_}} keys %f;
Push it onto the stickers list.
push @stick, \%f;
}
If there are any letters left in t
they were in the target but not
on any sticker, so we can't make the target.
if (scalar %{$t}) {
return 0;
}
Set up a breadth-first search (because we want the result with lowest
depth).
my @stack = ([\%w, 0]);
For each entry on the stack:
while (scalar @stack > 0) {
my $st = shift @stack;
If there are no letters remaining to be found, return the result.
if (scalar %{$st->[0]} == 0) {
return $st->[1];
} else {
New depth is one more than the old depth.
my $n = $st->[1] + 1;
Try each sticker.
foreach my $sti (@stick) {
Clone the list of remaining letters.
my $sp = dclone($st->[0]);
my $v = 0;
For each letter on the sticker,
foreach my $l (keys %{$sti}) {
If it's also in the remaining letters,
if (exists $sp->{$l}) {
This is an interesting sticker.
$v = 1;
Find out how many of this letter will be left in the word after
applying the sticker.
my $p = $sp->{$l} - $sti->{$l};
If there will be any,
if ($p > 0) {
store that value in the list of remaining letters,
$sp->{$l} = $p;
} else {
otherwise delete that letter from the list.
delete $sp->{$l};
}
}
}
If it was an interesting sticker, i.e. it removed any letters at all,
push it onto the stack for further evaluation.
if ($v) {
push @stack, [$sp, $n];
}
}
}
}
}
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.