I’ve been doing the Weekly
Challenges. The
latest
involved finding acronyms and string similarity. (Note that this ends
today.)
Task 1: Acronyms
You are given an array of words and a word.
Write a script to return true if concatenating the first letter of each word in the given array matches the given word, return false otherwise.
So basically I want to concatenate the first letter of each string in
the array.
JavaScript:
function acronyms(a, b) {
let os = "";
for (let c of a) {
os += c.charAt(0);
}
return os == b;
}
Task 2: Friendly Strings
You are given two strings.
Write a script to return true if swapping any two letters in one
string match the other string, return false otherwise.
This isn't the full Levenshtein distance of challenge 96, so I just
swap letters experimentally to see if I get a match. Perl:
sub friendlystrings($a, $b) {
foreach my $i (0 .. length($a) - 2) {
foreach my $j ($i + 1 .. length($a) - 1) {
my $c = $a;
substr($c, $i, 1) = substr($a, $j, 1);
substr($c, $j, 1) = substr($a, $i, 1);
if ($c eq $b) {
return 1;
}
}
}
0;
}
(Python annoys because although it treats strings as arrays in most
respects you apparently can't write to a position in a string using
array notation.)
Full code on
github.