3
\$\begingroup\$

This question already has an answer here:

Your task is simple: given a number as an input, you have to print the English name of the chemical element with that atomic number, up to and including ununoctium. For example, for an input of 77, your program has to print Iridium, and nothing else.

You can use any language, but no library (or other, in-built feature) which includes the names of the elements is allowed, your program has to store or generate the names on its own. Use names in wikipedia for the spelling of the names of the elements.

Shortest code wins, not sooner than 1 days after first valid answer.

\$\endgroup\$

marked as duplicate by Dr. belisarius, Toto, Johannes Kuhn, Kevin Brown, flornquake Jun 12 '13 at 17:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    \$\begingroup\$ It would be nice to know that en.wikipedia.org/wiki/Periodic_table_(large_version) is THE (not just that we are free to use it) periodic table for this challenge, otherwise someone might use older names for some of the elements, which might result in shorter code. \$\endgroup\$ – Fors Jun 4 '13 at 17:32
  • 1
    \$\begingroup\$ Writing an own compression mechanism is probably longer than using zlib, so challenge here is "who can invoke zlib with the shortest code" \$\endgroup\$ – Johannes Kuhn Jun 4 '13 at 21:54
  • \$\begingroup\$ It would be more modern to use the wikipedia page as source, and create a program that read that page and extract correct information, libraries allowed. \$\endgroup\$ – aka.nice Jun 7 '13 at 21:46
  • \$\begingroup\$ As added challenge, why not output it's electronic configuration (orbitals) while at it? That adds a bit of math to the problem, with a slight overhead in the data file. \$\endgroup\$ – Shark Jun 11 '13 at 12:55
  • \$\begingroup\$ The rule should be to use the official IUAPAC names. (That means aluminium (not aluminum) and sulfur (not sulphur).) \$\endgroup\$ – TRiG Feb 25 '14 at 0:41
3
\$\begingroup\$

Tcl, 567 577

puts [lindex [zlib i =SÑä\ ü(­EH¡Îmöêþý2û0%C¤éÆæï¿Tï¢r\"§ÔVOÔfÝçzgQáA\;¸Í¸.ß÷>^´Dcb´¯CÊ®èp2­ÞxG£QËvåª2ì§k¤±èµ4åJzî\v2axqÊ^w¶\töÏäªÒ£%`M15g+Ð4¶ù\vÉþ\]júiÓ\t¡êDí:Ð.¦86¦ñùÒû¦ëhÞmv\;ºçÔ\}þ45Q\;â&º¼éb®~_׬®5ftÐ`y»\fÅ´VN`ͺðmHDË6«6jßÈþ&1åüÙý\n»sFO\\\n¥ÜË¿Ñeú0^y@Z\\*ÏP<ÙDuIåVâªC<_f\]ôãÉС¸Â+@ÔvAÏ1Í2úLBFèÕîE`7¡:j^jÊëó^PÒÑF·IG(¦cî*Lj´Í\nìÑ-äùH1Ç=Òçoriñ2rÁaÍØ4Ê1xۯسe\{ú5llîer\ ³ÒÛ¡åxÖ\nþ(>ܶÔbYGì-Ú\vW5F\{бM- Ý\$0\ Ï3iG£ä\r/\{¾UÞÜa^jæ·Ùùaú?±äþ] [gets stdin]]

Who said that you can not embed binary data in your program code?
(zlib requires Tcl 8.6)

base64 encoded version:

cHV0cyBbbGluZGV4IFt6bGliIGkgPVPRkuRcIAj8FX+NKBOtRUihzm326v79EDL7MCVDpOnG5u+/VO+i
clwip4rUVk/UZt3ngXqTZw5R4ZRBD1w7uM24Lt/3Pl60RBtjYrSvQ8qu6HAyjh0Brd54R6NRy3blqjLs
p2uksei1NOVKAQF67lx2MmF4cQbKXpqBHXe2XHQO9gYGz+Sq0qMln2BNMTVnKwfQNLb5XHbJ/lxdF2r6
aZzTXHSh6kTtAQY60C6mgYSeODam8fnS+5oGputo3m2Ydp5cO7rn1Fx9/jQ1UVw74iaHExG6j4K86WKu
jH5f16wBrjVmdBmP0GCTeRu7XGbFlbSPVk5gzbrwbUhEyxs2qzZq38j+JjHlA/wb2f1cbrtzRk9cXFxu
A6Xcjsu/0WX6MF55QI9aXFwql88FilA82UR1SeUeVuKdqpBDPF9mXF30jOPJ0JqhuMIrQIzUdkGai88x
zTL6TEJG6NUY7kVgmjcDoZI6al5qyuvzXoRQ0tFGt0maRyimDmPuKkxqtM2NXG7sltEYLeT5SDHHPdLn
b3JpGIDxMnLBYc0H2Ag0h8oxeNuHr9gQs2Vce4n6NWxsmgLuZY5yB1wgF7PS26HleNZcbv4oPty2F9QQ
nGJZR+wHgi3aGVx2VzVGGFx70LFNjhctoN1cJJcwlVwgzzOGaRZHo+RcchYvDlx7vghV3p/chWFeaua3
HgPZ+WH6P7HkHf4HXSBbZ2V0cyBzdGRpbl1d

History:

  • changed compression mechanism. Saves me 10 chars.
\$\endgroup\$
  • \$\begingroup\$ First time I've seen a Tcl accepted answer! \$\endgroup\$ – sergiol May 24 '17 at 1:00
3
\$\begingroup\$

Python, 69 + 536 = 605 (lazy solution)

I took the liberty of not capitalizing the names of the elements. That allows me to shave off extra 41 bytes. zlib is most likely not the best compressor for plaintext, it takes 1292 bytes down to 536.

python file:

import zlib
print (eval(zlib.decompress(open('a').read())))[input()]

data file a (b64 encoded for web):

eJxNVNGS5CAI/Jd5vj+62gdUJlpDMIU6t9mvPzSQ2YetZVCbpmny9/H488hnkrohzxCpjF0DKj1f
UUA5ybKhSp3XIkhYAZfuT+v3eQVPGlUK4zzGdavVdL3fYWNsVww09sJX3AqVuK4euTb9k9FmetBz
yKyXySFBtuti7dAMKgJFA4rAVquXDgb/BgbLxix1dzK8gdKZqMX6qgGor77iC2lljgMnhZ/CUf9t
4FpsKLsXAGmoTyYBJLRkmJUW55ecR18FZIRiTJoqx/2Kz97lin6KqBBXzKUGo1rpDAn5agxjZrSX
Mnr2gpJd50NZeseq7Xt1ECFZ5+Wj0SQFSmOvfC5somFUiqIt+t/INnWfXQC7Q/o2w8UroiUPgYY1
nVaNf8WHaoLurAa74+CQepiukCq5MTqKSZDOpo+tfq5kgPd5z4NuMe8sje5KZXg6qJKe7pvh4K31
ZVu5dazNdRIf1kGgWl38KqU5EpQ4ZGmWb08QwjwLpe06l+VS8mlC6xNjCira4toUATbjio8LYr+b
1z24Zeu/Doa48RgPbcE5jn4XU3rFoKMPVDf55futK1OeVew6FlYVHP6pzjbfISc19NuK1XB/HuCf
4M19elAUzVpII/heI+gnY/OPRxYfhW/ujqWz+yaB7CpScmtX5L75UHQRUelaycGDfWWehFLfn/yB
vlZU1Pm7izjPmur1+VXj+vH1H5EjvoY=
\$\endgroup\$
  • \$\begingroup\$ zlib isn't even the best compressor for zlib-compatible output. kzip, 7zip, and zopfli are among the tools which beat it. \$\endgroup\$ – Peter Taylor Jun 4 '13 at 21:43
  • \$\begingroup\$ "your program has to store or generate the names on its own" that obviously means no external files. \$\endgroup\$ – vsz Jun 5 '13 at 3:07
  • \$\begingroup\$ @vsz: For me it isn't automatically clear from that, that all the code has to be in one file. If the limitation is that I cannot have a purely data file, then I will move the printing and indexing inside the zlibbed data. But really I'm just wondering why make such an arbitrary rule. \$\endgroup\$ – shiona Jun 5 '13 at 14:50
  • \$\begingroup\$ Would it not be better to have the b64 text in the python file to save bytes by not needing the open and read methods? \$\endgroup\$ – George Willcox May 21 '17 at 16:03
3
\$\begingroup\$

GolfScript, 577 characters

~(
'ž�®Ñ/ž!Z!Ó1ã„9î¸dÊÓªí[o@ÁlD®�K®ÆšTB˜Þ6(ËH¼¡)ßÝ•|O¨U˜É/Œ�DÚvU$.âxŠ²¤Æ³n’t8[¢êqHyÍlÂSbYÈ*1~˜|†­ï¨ŸÊ*Þk0âg%&‘Ÿ¶2ö¼Eë˜Áaq[¦Ä;ø7[–ί_ªtë½¢Ÿ\‰`Ãö”{ÉOUÈÜ¿Ò‰Fös–ß<µ‰x�#¶w+ìÊ®¼äH$̪¦ùoh�7™ÐbÙÑîâóÂø8Ö;•9ÁÃ)T°¤vWOUð~È¡jžK¯V¹8K?«à‚ª–0)ÚFj]îõc„ ¼´¢�Ã#æ¯Uƒ¡Mh$Šº^9V$Ùš`½†2±Žžk™+®5ª.„bC x%XJ<ç†78N¥5á2ù@<ù��ž£ªØ¸ÅÆŒç™ Î½¨ñøŽjZ±@,, íªluzÀ¡‹ØÈ[$5† S¾,‰‚TÊ_"ñÍÈ�» Úﺠ@ð_D«�.kX©(0ÎX]®c´c.Åm,‰æªQ)•‚t®§>‰j)tŸ.Z!�|0Ň̢‰ùc êš™‡ãÂÓT<xsY(ª2!¦93㜤¡�Ç"mÎç£COKcÎ}¶!0Ö·£—s­ÂÞíÛê”[lœX£€Ã6{È‚¡�ˆFâóVP8‡4�ës†´ïç]Ó{±éˆñz~bºs8¿U/ªY²Öm4 ÍP3CI7‹'
{32-}%218base 32base{96+}%''+'j'/'ium`'*'`'/=([32-]\+

Base64 encoded version:

fignnp2u0S+eIVoh0zHjhDnuuGTK06rtW29AwWxEroFLrsaaVEKY3jYoy0i8oSnf3ZV8T6hVmMkv
jIFE2nZVJC7ieIqypMazbpJ0OFui6nFIec1swlNiWcgqMX6YfIat76ifyireazDiZyUmkZ+2Mva8
ReuYwWFxW3+mxDv4N1uWzq9fqnTrvaKfXIlgw/aUe8lPVcjcv9KJRvZzlt88tYl4nSO2dyvsyq68
5EgkzKqm+W9okDeZ0GLZ0e7i88L4ONY7lTnBwylUsKR2V09V8H7IoWqeS69WuThLP6vggqqWMCna
Rmpd7vVjhKC8tKKdwyPmr1WDoU1oJIq6XjlWJNmaYL2GMrGOnmuZK641qi6EYkMgeCVYSjznhjc4
TqV/NeEy+UA8+YGNnqOq2LjFxoznmSDOvajx+I5qWrFALCwg7apsdXrAoYvYyFskNYYgU74siYJU
yl8i8c3IncK7oNrvuqBA8F9Eq50ua1ipKDDOWF2uY7RjLsVtLInmqlEplYJ0rqc+iWopdJ8uWiGQ
fDDFh8yiifljIOqamYfjwtNUPHhzWSiqMiGmOTPjnKShkMcibc7no0NPS2POfbYhMNa3o5dzrcLD
nu3b6pRbbJxYo4DDNnvIgqGdiEbi81ZQOIc0getzhrTv513Te7HpiPF6fmK6czi/VS+qf1my1m00
IM1QM0NJN4snezMyLX0lMjE4YmFzZSAzMmJhc2V7OTYrfSUnJysnaicvJ2l1bWAnKidgJy89KFsz
Mi1dXCsNCg==

It also does the capitalization correct. No libraries, no zlib, just base encoding. With a little bit tweaking it might be possible to shave off a few chars.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.