C, 775 771 chars
char*e[]={"h","he","li","be","b","c","n","o","f","ne","na","mg","al","si","p","s","cl","ar","k","ca","sc","ti","v","cr","mn","fe","co","ni","cu","zn","ga","ge","as","se","br","kr","rb","sr","y","zr","nb","mo","tc","ru","rh","pd","ag","cd","in","sn","sb","te","i","xe","cs","ba","la","ce","pr","nd","pm","sm","eu","gd","tb","dy","ho","er","tm","yb","lu","hf","ta","w","re","os","ir","pt","au","hg","tl","pb","bi","po","at","rn","fr","ra","ac","th","pa","u","np","pu","am","cm","bk","cf","es","fm","md","no","lr","rf","db","sg","bh","hs","mt","ds","rg","cn","uut","fl","uup","lv","uus","uu",0};
b[99],n;
c(w,o,l)char*w,*o,**l;{
return!*w||!strncmp(*l,w,n=strlen(*l))&&c(w+n,o+sprintf(o,",%d",l-e+1),e)||*++l&&c(w,o,l);
}
main(){
while(gets(b))c(b,b+9,e)&&printf("%s%s\n",b,b+9);
}
Input: Word per line, must be lowercase. usr/share/dict/words
is fine.
Output: Word and numbers, e.g.: acceptances,89,58,15,73,7,6,99
Logic:
c(w,o,l)
checks the word w
, starting with element l
.
Two-way recursion is used - if the first element matches the head of the element list, check the remainer of w
against the full element list. If this match fails, check the word against the tail of the list.
The buffer o
accumulates the element numbers along the successful path. After a match, it will contain the list of numbers, and is printed.
Issues:
The list isn't encoded efficiently - too much "
and ,
". But this way it's easy to use. I'm sure it can be much improved, without too much cost in code.
'The' number code? What about cases where there's more than one? E.g. CO vs Co. – Peter Taylor – 2012-06-09T07:13:11.540
I guess it depends on your code. Both are valid. – beary605 – 2012-06-09T07:33:32.163
3As I'm reading the responses below, I noticed one place that everybody could cut out a few characters. They might remove Co, Si, Sc, Os, Hs, Po, Pb, Np, No, Yb, Cs, and maybe others from their list of elements, since they can all be constructed from other elements. – PhiNotPi – 2012-06-09T12:35:29.947
@PhiNotPi, I considered dropping elements which aren't used, but that one's arguable because someone might want to use a different word list (or even one from a different language). I think I could get away with reducing the elements you suggest to one character each. – Peter Taylor – 2012-06-09T12:51:31.073
1Not Ytterbium, that's my favorite element! – Rob – 2012-06-09T15:22:28.493
2Just to clarify, the elements that I listed can always be safely removed. For example, Ytterbium can always be replaced with a Yttrium and a Boron, no matter what language the word list is in. – PhiNotPi – 2012-06-09T21:00:33.807
1I'm not quite sure whether I understand the task completely: Shall we find matching words for the elements until every element was printed, or shall we print every word from the dict, which can be combined from the element table? Or something else? – user unknown – 2012-06-10T03:50:59.390
Every word that can made from the elements that is in the dictionary. Elements can and will have to repeat. – Rob – 2012-06-10T06:22:21.383
@Rob: I would have received a notification about your comment, if you had tagged it with an @ sign, and my name. While typing the first characters of a name, you get a suggestion to complete it with TAB. The original writer of a Q or A doesn't need to be tagged - he gets informed automatically. – user unknown – 2012-06-12T15:01:45.440