Consider a common 5-letter English word.
- If you insert an E after the 2nd letter you will get a common 6-letter English word.
- If instead, you insert an E at the end, you will get another common 6-letter English word.
- If instead, you insert an E after the 4th letter, you will get another common 6-letter word.
Determine these four words.
Source: Adapted from a problem appearing in NPR's Sunday Puzzle in 2019.
The program starts by finding 6-letter words with an E in the third position, then checks that the 5-letter word that remains after the removal of the E is a valid word. Then it tries inserting that E in the other two locations, checking for valid words.
let6=fileread("c:\words\words6.txt");
for i=1:6:length(let6)
w6=let6(i:i+5);
if w6(3)=='e'
w5=[w6(1:2) w6(4:end)];
if isword(w5)
if isword([w5 'e'])
if isword([w5(1:4) 'e' w5(5)])
disp([w5 ' ' w6 ' ' w5 'e ' w5(1:4) 'e' w5(5)])
end
end
end
end
end
where isword is;
function iw=isword(x)
ws=string(x);
xl=length(x);
fn=['c:\words\words' num2str(xl) '.txt'];
words=fileread(fn);
nwords=length(words)/xl;
first=1; last=nwords;
while first<=last
mid=floor((first+last)/2);
word=string(words(xl*(mid-1)+1:xl*mid));
if word==ws
break
end
if word>ws
last=mid-1;
end
if word<ws
first=mid+1;
end
end
if word==ws
iw=true;
else
iw=false;
end
end
finds
>> fourCredibleWords
spars spears sparse spares
>>
|
Posted by Charlie
on 2023-02-28 09:37:31 |