If one assigns $ prices to a letter by its place in the English alphabet i.e. A=1, B=2, .. ..Y=25, Z=26 and the price of a word equals sum of its letters' values - (e.g, DOG is valued @ US$ 26)
find the n-letter words with the lowest and the highest prices:
n=3, 4, .... 10, 11
for i=3:11
low(i)=99999;high(i)=0;
end
loww=string.empty; highw=string.empty;
fid=fopen('c:\words\words.txt','r');
while ~feof(fid)
w=fgetl(fid);
if w==lower(w)
t=sum(w-'a'+1);
lw=length(w);
if lw>=3 && lw<=11
if t<low(lw)
low(lw)=t;
loww(lw)=string(w);
end
if t>high(lw)
high(lw)=t;
highw(lw)=string(w);
end
end
end
end
fclose('all');
for i=3:11
fprintf('%s %d %s %3d\n',loww(i),low(i),highw(i),high(i))
end
finds
>> cheapOrExpensive
aba 4 wry 66
abba 6 xyst 88
abaca 8 muzzy 111
baccae 15 xystus 128
cabbage 21 zyzzyva 151
cabbaged 25 zyzzyvas 170
beachhead 37 susurrous 171
deadheaded 41 puttyroots 189
abracadabra 52 trustworthy 207
|
Posted by Charlie
on 2023-08-16 09:22:52 |