Periodic Table of Elements - Code Golf
C, 452
A good hash function helps. There may be better ones. (Improvements suggested by @ugoren et al.)
h;main(c){char*p="Sn)Cu&V$U#Mo#Rf#Sg&Cs#Y+FTa%Rb)S'Nd#GaK&Mg'Zr$PtPm%ReUuo#SmDy(Ac$Lu%W&CaRa(Cf)EuB#Ds%Md$Uus*NpIn$H&YbIr*BeEs*Tc#I(FlRuC#ThSrBh/NaCoLrKr&Nb$CePb$Ne'Am)At*PdLa#Tl%HgMt,CrTbBk$Rh&Rn4TeZn$HfAg%Fm)Xe$AlScFePo$As'HeO#LvN&DbGe#Ho&Mn$Cd+Ni$Rg$HsBr$AuSi#Pr&Uup#Se*Ti#Tm$Er$Sb&PPu&Cm$GdBa'Cn&UutLiFr#Ar#Bi#NoOs%Pa4Cl";while((c=getchar())>64)h=(h+c%32+74)*311%441;while(h)if(*p<65?h-=*p++-34,0:1)for(h--;*++p>96;);do putchar(*p++);while(*p>96);}
Ungolfed with comments:
int h;
main(c) {
/* Hashed element symbols. Characters #$% etc. are used as padding: */
char *p="Sn)Cu&V$U#Mo#Rf#Sg&Cs#Y+FTa%Rb)S'Nd#GaK&Mg'Zr$PtPm%ReUuo#SmDy(Ac$Lu%W&C"
"aRa(Cf)EuB#Ds%Md$Uus*NpIn$H&YbIr*BeEs*Tc#I(FlRuC#ThSrBh/NaCoLrKr&Nb$CeP"
"b$Ne'Am)At*PdLa#Tl%HgMt,CrTbBk$Rh&Rn4TeZn$HfAg%Fm)Xe$AlScFePo$As'HeO#Lv"
"N&DbGe#Ho&Mn$Cd+Ni$Rg$HsBr$AuSi#Pr&Uup#Se*Ti#Tm$Er$Sb&PPu&Cm$GdBa'Cn&Uu"
"tLiFr#Ar#Bi#NoOs%Pa4Cl";
/* This hash function gives a unique result for each element: */
while((c=getchar())>64) h=(h+c%32+74)*311%441;
/* Find the corresponding position in the hashed data */
while(h) {
if(*p<65?h-=*p++-34,0:1) { /* Step over an abbreviation */
for(h--;*++p>96;); /* Skip padding */
}
}
/* Output first uppercase character and all following lowercase characters: */
do {
putchar(*p++);
} while(*p>96);
}
I used brute force to find this hash; This was the only one with a hash size of ≤512 that had no collisions. I didn't check alternative spellings though, and there might be better functions with different algorithms (e.g. using XOR instead of addition).
The hash function maps text strings to values from 0 to 440. "Tin" hashes to zero, so "Sn" is at the start of the table. The next 7 positions are empty. To keep the code compact, this is indicated by the ASCII value 34+7=41 (")"). Next comes "Copper" (8), four empty cells (34+4=38="&"), and "Vanadium" (13). After calculating a hash, the program steps through the table, subtracting 1 for every capital letter followed by 0 or more lowercase letters, and subtracting (ASCII VALUE)-34 for every non-alphabet character. When the value reaches zero, we have found the correct result.
CJam, 337 297 293 232 220 201 200 bytes
leu:E2f^3b2+4%_"53N5903CVCT4":i3/=~Ef+b\%]53b"gØâ^ÃP·^À4ξ^ß^E5^W^Ma{áª^B¤±´oòæ»^XÊQÑ»4žDÙÝòÙ 0^ÝþKa6^Ó£,Ûkû¥¡ùh^E"256b7b6a/0a3**<)5md@5a/,(" ¬^GH/N¿·%^U^RU1²Bd
òë^м~í^ÌéáU"256b25b'Af+2/='J-'Q/"UU"*\)_5=)*E%2<?(\el
The above code uses caret notation, since it contains control characters.
At the cost of 24 additional bytes (for a total of 224), those characters can be avoided.
leu:E2f^3b2+4%_"53N5903CVCT4":i3/=~Ef+b\%]53b
"' NwEvKv6e8@]jO4G)=b{L!v@hFQ/oCN)*|BRxvNRL+LO7NI(pLs4[d87$Q%8R\t+' M5JU"
{32f-95b}:B~7b6a/0a3**<)5md@5a/,(
"&y.$h*z^t\rQPUc]l8F h$=18C^r|vD~S"
B25b'Af+2/='J-'Q/"UU"*\)_5=)*E%2<?(\el
You can try this code in the CJam interpreter.
Test cases
$ base64 -d > elements.cjam <<< bGV1OkUyZl4zYjIrNCVfIjUzTjU5MDNDVkNUNCI6aTMvPX5FZitiXCVdNTNiImfY4oNQt4A0zr6fBTUXDWF74aoCpLG0b/LmuxjKUdG7NMW+RNnd8tmgMJ3+S2E2k6Ms22v7paH5aAUiMjU2YjdiNmEvMGEzKio8KTVtZEA1YS8sKCIgrAdIL06/tyUVElUxskJkCvLrkLx+7Yzp4VUiMjU2YjI1YidBZisyLz0nSi0nUS8iVVUiKlwpXzU9KSpFJTI8PyhcZWw=
$ cksum elements.cjam
952664534 200 elements.cjam
$ for e in Carbon NiTROGen Sodium Gold Silver Tin; do LANG=en_US cjam elements.cjam <<< $e; echo; done
C
N
Na
Au
Ag
Sn
How it works
The first step is to read the element name from STDIN and apply a rather elaborated hash function, which maps all element names in the range [0, 225]:
l eu :E " Read a line from STDIN, convert to uppercase and save in E. ";
2 f^ " XOR each character code with 2. ";
3 b " Convert to integer; consider the resulting array a base 3 number. ";
2 + 4 % " Add 2 and take the result modulo 4. Result: R ";
"53N5903CVCT4":i " Push [53 51 78 53 57 48 51 67 86 67 84 52]. ";
3 / = " Retrieve the chunk of length 3 that corresponds to R. Result: C ";
~ E f+ " Add C[2] to all character codes of E. ";
b " Convert to integer; consider the resulting array a base C[1] number. ";
\ % " Take the integer modulo C[0]. Result: M ";
] 53 b " Push H := 53 * R + M. ";
Many element symbols are formed by the first and second, first and third, first and fourth, first and fifth or first and tenth (which is just the first) character of the element's English name. We are going to represent these elements by numbers from 0 to 4 respectively. All remaining elements (represented by 5) will require a lookup table.
The resulting table can be pushed as follows:
"gØâ^ÃP·^À4ξ^ß^E5^W^Ma{áª^B¤±´oòæ»^XÊQÑ»4žDÙÝòÙ 0^ÝþKa6^Ó£,Ûkû¥¡ùh^E"256b7b6a/0a3**
The array of character codes gets converted from base 256 to base 7 and 6's are replaced by runs of three 0's.
This is the decision table D:
[4 0 0 0 1 0 0 0 0 0 0 3 0 2 0 1 0 0 0 0 0 0 0 0 4 1 1 0 0 0 0 2 0 4 0 5 2 0 0 3 4 0 0 0 0 4 0 1 0 0 3 1 0 0 2 1 1 1 0 0 0 1 0 5 5 0 0 2 0 0 0 5 5 0 0 0 5 0 3 0 0 0 0 5 0 0 0 0 0 0 0 0 5 2 3 0 1 0 5 0 4 0 0 0 0 4 0 5 0 0 0 0 0 5 0 0 0 2 5 1 4 1 5 0 0 0 5 0 0 5 1 1 0 0 0 0 0 0 2 0 5 0 0 0 3 1 0 2 0 0 0 2 0 0 0 5 0 0 0 0 1 0 0 0 0 0 4 0 2 2 5 2 0 0 5 1 0 0 0 0 4 0 5 0 0 3 5 0 0 5 0 1 0 0 0 2 0 0 0 0 0 5 0 4 0 0 0 0 0 0 0 0 3 0 4 0 0 1 2 2 0 0 0 0 0]
The necessary action for the element with hash 1, e.g., corresponds to the first element of the this array. Array elements that do not correspond to any element's hash are also zero, which allows the (0 0 0) ↦ 6 compression.
Now, we interpret D for hash H.
< ) 5 md " Push D[:H-1] (D[H-1] / 5) (D[H-1] % 5). ";
@ 5a / , ( " Count the number of 5's in D[:H-1] (by splitting at [5]). Result: I ";
Next, we push the lookup table. If we append j to single-character symbols and replace Uu with Q, each symbol will be exactly two characters long. It can be pushed as follows:
" ¬^GH/N¿·%^U^RU1²Bd
òë^м~í^ÌéáU"256b25b'Af+2/
The array of character codes gets converted from base 256 to base 25, the character code of A gets added to all digits (casting to Character in the process) and the result is split into chunks of length two.
This is the lookup table L:
["QP" "YB" "PD" "SN" "QO" "QT" "QS" "SB" "KJ" "TM" "FE" "PB" "AU" "WJ" "CN" "SG" "RF" "CM" "CU" "HG" "NA" "RG" "AG"]
Now, we proceed to compute potential element names.
= " Push L[I]. ";
'J - 'Q / "UU" * " Remove J's and replace Q's with runs of two U's. ";
\ ) _ 5 = ) * " Push S := (D[H-1] % 5 + 1) * ((D[H-1] % 5 + 1 == 5) + 1). ";
E % " Push every Sth character of E. ";
2 < " Discard all but the first two characters. ";
The stack now contains
B M N
where B is the Boolean D[H-1] / 5, M is the name retrieved from the lookup table an N is the element name formed by selecting characters from E.
We're almost done:
? " If B, push M; else, push N. ";
( \ " Extract the first character from the string. ";
el " Convert the rest to lowercase. ";
JavaScript ES6, 690 708 bytes
for(n=prompt()[l='toLowerCase'](i=0);!(c='HHeLiBeBCNFNeNaMgAlSiPSClArKCaScTiVCrMnFeCoNiCuZnGaGeAsSeBrKrRbSrYZrNbMoTcRuRhPdAgCdInSnSbTeIXeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaWReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaUNpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCnUutFlUupLvUusUuo'.match(x=/[A-Z][a-z]*/g)['HyHeLitBeryBorCarNitFluNeonSoMagAlSiliPhSuChlArgPotCalcScTitVChrManIroCobNicCoppZinGalGeArsSelBrKRubStYttrZirNioMoTecRuthenRhoPaSilvCadInTinAnTelIoXCaeBaLanCePraNeodPromSaEuGadTerDyHoErThuYtteLuHafTaTuRheOsIriPlaGoMerThaLeBiPolAsRadoFrRadiAcThoProtUrNepPluAmCuBerkCaliEiFeMenNoLawRutherDuSeaBohHasMeiDaRoCopeUnuntFleUnunpLivUnunsUnuno'.match(x).map(a=>a[l]()).indexOf(n.slice(0,i++))]);)
alert(c)
The first array holds the symbols, and the second array holds the minimum letters necessary to tell which element is being referred to. Thanks to core1024 and edc65 for helping shorten it. Test at http://jsfiddle.net/xjdev4m6/2/. Slightly more readable:
n=prompt().toLowerCase()
e='HyHeLitBeryBorCarNitFluNeonSoMagAlSiliPhSuChlArgPotCalcScTitVChrManIroCobNicCoppZinGalGeArsSelBrKRubStYttrZirNioMoTecRuthenRhoPaSilvCadInTinAnTelIoXCaeBaLanCePraNeodPromSaEuGadTerDyHoErThuYtteLuHafTaTuRheOsIriPlaGoMerThaLeBiPolAsRadoFrRadiAcThoProtUrNepPluAmCuBerkCaliEiFeMenNoLawRutherDuSeaBohHasMeiDaRoCopeUnuntFleUnunpLivUnunsUnuno'.match(x=/[A-Z][a-z]*/g).map(a=>a.toLowerCase())
s='HHeLiBeBCNFNeNaMgAlSiPSClArKCaScTiVCrMnFeCoNiCuZnGaGeAsSeBrKrRbSrYZrNbMoTcRuRhPdAgCdInSnSbTeIXeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaWReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaUNpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCnUutFlUupLvUusUuo'.match(x)
for(i=0;i<7;i++){
if(c=s[e.indexOf(n.slice(0,i))]){
alert(c,i=8) // i=8 breaks out of the loop so the result is only alerted once
}
}