Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

edited body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998

First, I sorted the entire table by masses to get a non-decreasing sequence. Then I computed the successive differences between those, because the sequence is rising rather slowly. In fact, the steps are always less than 13. That allows me to use base 13 (and then recover the masses by truncating that array to the correct length and summing all elements). However, we can do better: there is only a single 1312, and the next smaller step is 6. So we can replace that 1312 by a 7 and use base 8, which saves quite a bit.

First, I sorted the entire table by masses to get a non-decreasing sequence. Then I computed the successive differences between those, because the sequence is rising rather slowly. In fact, the steps are always less than 13. That allows me to use base 13 (and then recover the masses by truncating that array to the correct length and summing all elements). However, we can do better: there is only a single 13, and the next smaller step is 6. So we can replace that 13 by a 7 and use base 8, which saves quite a bit.

First, I sorted the entire table by masses to get a non-decreasing sequence. Then I computed the successive differences between those, because the sequence is rising rather slowly. In fact, the steps are always less than 13. That allows me to use base 13 (and then recover the masses by truncating that array to the correct length and summing all elements). However, we can do better: there is only a single 12, and the next smaller step is 6. So we can replace that 12 by a 7 and use base 8, which saves quite a bit.

added 2398 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998

Explanation

Explanation will followOf course, the main compression comes from base encoding: to compress any array, interpret it as digits in some sufficiently large base to get a single number - then get the base-256 (or similar) digits of that number and turn them into characters. That process is reversible, such that we can recover the array from that string.

The above code contains two such base encodings: one for the abbreviations, and one for the masses. To make those two arrays more amenable to base encoding (i.e. to lower the base, as that makes the overall number and string significantly shorter), I did some preprocessing on the table.

First, I sorted the entire table by masses to get a non-decreasing sequence. Then I computed the successive differences between those, because the sequence is rising rather slowly. In fact, the steps are always less than 13. That allows me to use base 13 (and then recover the masses by truncating that array to the correct length and summing all elements). However, we can do better: there is only a single 13, and the next smaller step is 6. So we can replace that 13 by a 7 and use base 8, which saves quite a bit.

To encode the abbreviations, we pad the single-character elements with Q (which doesn't appear in any abbreviation), join them all together and take the difference with A or a (such that each letter becomes a number between 0 and 25 inclusive). This is then encoded in base 26. The abbreviations can be recovered by splitting the base-26 digits into pairs of 2 and removing all 16s (which correspond to the Qs).

Here is the code:

e# Decode the abbreviations
"gibberish"256b26b
 e# Get the base-26 digits representing the letters.
2/ e# Split into pairs.
Gf- e# Remove 16s (Qs).
e# Process the input
qeu e# Read input and convert to upper case.
'Af- e# Subtract the characters from 'A' to get numbers.
a#) e# Find the position in the decoded array.
e# Find the mass
"gibberish"258b8b
 e# Get the base-8 digits representing the mass increments.
7Cer e# Replace the 7 by a 12.
< e# Truncate, based on the position of the input in the abbreviations.
:+ e# Sum all the increments.

Explanation will follow.

Explanation

Of course, the main compression comes from base encoding: to compress any array, interpret it as digits in some sufficiently large base to get a single number - then get the base-256 (or similar) digits of that number and turn them into characters. That process is reversible, such that we can recover the array from that string.

The above code contains two such base encodings: one for the abbreviations, and one for the masses. To make those two arrays more amenable to base encoding (i.e. to lower the base, as that makes the overall number and string significantly shorter), I did some preprocessing on the table.

First, I sorted the entire table by masses to get a non-decreasing sequence. Then I computed the successive differences between those, because the sequence is rising rather slowly. In fact, the steps are always less than 13. That allows me to use base 13 (and then recover the masses by truncating that array to the correct length and summing all elements). However, we can do better: there is only a single 13, and the next smaller step is 6. So we can replace that 13 by a 7 and use base 8, which saves quite a bit.

To encode the abbreviations, we pad the single-character elements with Q (which doesn't appear in any abbreviation), join them all together and take the difference with A or a (such that each letter becomes a number between 0 and 25 inclusive). This is then encoded in base 26. The abbreviations can be recovered by splitting the base-26 digits into pairs of 2 and removing all 16s (which correspond to the Qs).

Here is the code:

e# Decode the abbreviations
"gibberish"256b26b
 e# Get the base-26 digits representing the letters.
2/ e# Split into pairs.
Gf- e# Remove 16s (Qs).
e# Process the input
qeu e# Read input and convert to upper case.
'Af- e# Subtract the characters from 'A' to get numbers.
a#) e# Find the position in the decoded array.
e# Find the mass
"gibberish"258b8b
 e# Get the base-8 digits representing the mass increments.
7Cer e# Replace the 7 by a 12.
< e# Truncate, based on the position of the input in the abbreviations.
:+ e# Sum all the increments.
added 2398 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998

CJam, (削除) 201 (削除ここまで) 196195 bytes

"z­ds#Ä|ñRX3⁄4Wàã»3 ̈ÝmØÞîQ!^ ̈æ_lÈ=éö(o<%zh\
6lõÊrÇ"üæ1Îe3©ü^<ùÉkIxX ̄þÄäj¤ý ̈,N8jËäVaw{V ́ãW3⁄4]ÊU ̧¡51⁄2ê.uÛïÇtà=ÒÙ}]n[ê3xOpÖ"252b26b2®Ö_<U
Zká++ØÙ/aQÒ?»<yÙ>K_þÉ«2 ́EaÄ¡8ÕþÀ!-,ñ©ZÒàF"256b26b2/Gf-q31f&:i:(qeu'Af-a#)"?F* ́õEP9&#ßYo\ìXí ̈x)ô_Ém|0xð+"274b8b7Cer<"#ØmÔwXÍÒA5(Wâ£ogg5ÂßòtwÆÚ^ØW¶°\"258b8b7Cer<:+

I'm sure there are a bunch of unprintable characters in there, so use [this permalink for testing][1].

I used [this script][2] to encode the abbreviations and masses as two numbers, and [this script][3] to generate the base encoding. this permalink for testing (These are mostly for myself if I need to change anything.)

Explanation will follow.

[1]: http://cjam.aditsu.net/#code=%22%04%C3%BC%C3%A61%C3%8Ee3%C2%A9%C3%BC%5E%3C%C3%B9%C3%89kI%C2%93x%C2%9C%C2%9BX%C2%AF%0C%C3%BE%C3%84%C3%A4j%C2%A4%C3%BD%C2%A8%2C%C2%97N%C2%888%1Fj%C3%8B%02%C3%A4V%C2%AA%C2%86w%7B%C2%8B%C2%AE%C3%96_%3C%10U%0A%C2%99Zk%C3%A1%2B%2B%C2%93%C2%96%C2%87%C3%98%C3%99%2F%C2%AAQ%C3%92%C2%85%C2%82%3F%C2%8C%C2%BB%13%C2%92%3C%C2%80%C2%92%18%C2%90y%C3%99%3EK%C2%99_%0F%C3%BE%C2%86%C3%89%C2%AB2%C2%B4E%13%C2%AA%C3%84%C2%9A%C2%A1%1B8%C3%95%C3%BE%C3%80!%C2%9F%0F%16%C2%82-%2C%C3%B1%1D%C2%A9Z%C2%8D%C3%92%C3%A0F%22256b26b2%2FGf-qeu'Af-a%23 )%22%02%23%C3%98%C2%9Fm%C3%94%06wX%C3%8D%C2%84%C3%92A%1E%C2%915(W%C3%A2%C2%A3ogg5%C3%82%C3%9F%C3%B2tw%C3%86%C3%9A%5E%C3%98W%C2%B6%1A%C2%B0%C2%A5%22258b8b7Cer%3C%3A%2B&input=Mn [2]: http://cjam.aditsu.net/#code=qN%2FSf%252f%3E%7B )~%2B(2'Qe%5Deua%5C%2B%7D%25%7BW%3D%7D%24z(s'Afm26bp%0A~0%5C%2B%7B_%40-%5C%7D*%3B%5DC7er8bp&input=1%20%20%20%20%20%20%20%20%20Hydrogen%20%20%20%20%20H%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201%20%20%20%20%20%20%20%20%0A2%20%20%20%20%20%20%20%20%20Helium%20%20%20%20%20%20%20He%20%20%20%20%20%20%20%20%20%20%20%20%20%20%204%20%20%20%20%20%20%20%20%0A3%20%20%20%20%20%20%20%20%20Lithium%20%20%20%20%20%20Li%20%20%20%20%20%20%20%20%20%20%20%20%20%20%207%20%20%20%20%20%20%20%20%0A4%20%20%20%20%20%20%20%20%20Beryllium%20%20%20%20Be%20%20%20%20%20%20%20%20%20%20%20%20%20%20%209%20%20%20%20%20%20%20%20%0A5%20%20%20%20%20%20%20%20%20Boron%20%20%20%20%20%20%20%20B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2011%20%20%20%20%20%20%20%0A6%20%20%20%20%20%20%20%20%20Carbon%20%20%20%20%20%20%20C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2012%20%20%20%20%20%20%20%0A7%20%20%20%20%20%20%20%20%20Nitrogen%20%20%20%20%20N%20%20%20%20%20%20%20%20%20%20...%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20238%20%20%20%20%20%20%0A93%20%20%20%20%20%20%20%20Neptunium%20%20%20%20Np%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20237%20%20%20%20%20%20%0A94%20%20%20%20%20%20%20%20Plutonium%20%20%20%20Pu%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20244%20%20%20%20%20%20%0A95%20%20%20%20%20%20%20%20Americium%20%20%20%20Am%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20243%20%20%20%20%20%20%0A96%20%20%20%20%20%20%20%20Curium%20%20%20%20%20%20%20Cm%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20247%20%20%20%20%20%20%0A97%20%20%20%20%20%20%20%20Berkelium%20%20%20%20Bk%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20247%20%20%20%20%20%20%0A98%20%20%20%20%20%20%20%20Californium%20%20Cf%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20251%20%20%20%20%20%20%0A99%20%20%20%20%20%20%20%20Einsteinium%20%20Es%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20252%20%20%20%20%20%20%0A100%20%20%20%20%20%20%20Fermium%20%20%20%20%20%20Fm%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20257%20 [3]: http://cjam.aditsu.net/#code=363180297747591790604930618402396129685962856524967156551691803198533587214560772335382797%20258b_%3Ae%3Ep_%2Cp%3Acp&input=Mn

CJam, (削除) 201 (削除ここまで) 196 bytes

"z­ds#Ä|ñRX3⁄4Wàã»3 ̈ÝmØÞîQ!^ ̈æ_lÈ=éö(o<%zh\
6lõÊrÇ{V ́ãW3⁄4]ÊU ̧¡51⁄2ê.uÛïÇtà=ÒÙ}]n[ê3xOpÖ"252b26b2/Gf-q31f&:i:(a#)"?F* ́õEP9&#ßYo\ìXí ̈x)ô_Ém|0xð+"274b8b7Cer<:+

I'm sure there are a bunch of unprintable characters in there, so use this permalink for testing.

Explanation will follow.

CJam, (削除) 201 (削除ここまで) 195 bytes

"üæ1Îe3©ü^<ùÉkIxX ̄þÄäj¤ý ̈,N8jËäVaw{®Ö_<U
Zká++ØÙ/aQÒ?»<yÙ>K_þÉ«2 ́EaÄ¡8ÕþÀ!-,ñ©ZÒàF"256b26b2/Gf-qeu'Af-a#)"#ØmÔwXÍÒA5(Wâ£ogg5ÂßòtwÆÚ^ØW¶°\"258b8b7Cer<:+

I'm sure there are a bunch of unprintable characters in there, so use [this permalink for testing][1].

I used [this script][2] to encode the abbreviations and masses as two numbers, and [this script][3] to generate the base encoding. (These are mostly for myself if I need to change anything.)

Explanation will follow.

[1]: http://cjam.aditsu.net/#code=%22%04%C3%BC%C3%A61%C3%8Ee3%C2%A9%C3%BC%5E%3C%C3%B9%C3%89kI%C2%93x%C2%9C%C2%9BX%C2%AF%0C%C3%BE%C3%84%C3%A4j%C2%A4%C3%BD%C2%A8%2C%C2%97N%C2%888%1Fj%C3%8B%02%C3%A4V%C2%AA%C2%86w%7B%C2%8B%C2%AE%C3%96_%3C%10U%0A%C2%99Zk%C3%A1%2B%2B%C2%93%C2%96%C2%87%C3%98%C3%99%2F%C2%AAQ%C3%92%C2%85%C2%82%3F%C2%8C%C2%BB%13%C2%92%3C%C2%80%C2%92%18%C2%90y%C3%99%3EK%C2%99_%0F%C3%BE%C2%86%C3%89%C2%AB2%C2%B4E%13%C2%AA%C3%84%C2%9A%C2%A1%1B8%C3%95%C3%BE%C3%80!%C2%9F%0F%16%C2%82-%2C%C3%B1%1D%C2%A9Z%C2%8D%C3%92%C3%A0F%22256b26b2%2FGf-qeu'Af-a%23 )%22%02%23%C3%98%C2%9Fm%C3%94%06wX%C3%8D%C2%84%C3%92A%1E%C2%915(W%C3%A2%C2%A3ogg5%C3%82%C3%9F%C3%B2tw%C3%86%C3%9A%5E%C3%98W%C2%B6%1A%C2%B0%C2%A5%22258b8b7Cer%3C%3A%2B&input=Mn [2]: http://cjam.aditsu.net/#code=qN%2FSf%252f%3E%7B )~%2B(2'Qe%5Deua%5C%2B%7D%25%7BW%3D%7D%24z(s'Afm26bp%0A~0%5C%2B%7B_%40-%5C%7D*%3B%5DC7er8bp&input=1%20%20%20%20%20%20%20%20%20Hydrogen%20%20%20%20%20H%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201%20%20%20%20%20%20%20%20%0A2%20%20%20%20%20%20%20%20%20Helium%20%20%20%20%20%20%20He%20%20%20%20%20%20%20%20%20%20%20%20%20%20%204%20%20%20%20%20%20%20%20%0A3%20%20%20%20%20%20%20%20%20Lithium%20%20%20%20%20%20Li%20%20%20%20%20%20%20%20%20%20%20%20%20%20%207%20%20%20%20%20%20%20%20%0A4%20%20%20%20%20%20%20%20%20Beryllium%20%20%20%20Be%20%20%20%20%20%20%20%20%20%20%20%20%20%20%209%20%20%20%20%20%20%20%20%0A5%20%20%20%20%20%20%20%20%20Boron%20%20%20%20%20%20%20%20B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2011%20%20%20%20%20%20%20%0A6%20%20%20%20%20%20%20%20%20Carbon%20%20%20%20%20%20%20C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2012%20%20%20%20%20%20%20%0A7%20%20%20%20%20%20%20%20%20Nitrogen%20%20%20%20%20N%20%20%20%20%20%20%20%20%20%20...%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20238%20%20%20%20%20%20%0A93%20%20%20%20%20%20%20%20Neptunium%20%20%20%20Np%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20237%20%20%20%20%20%20%0A94%20%20%20%20%20%20%20%20Plutonium%20%20%20%20Pu%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20244%20%20%20%20%20%20%0A95%20%20%20%20%20%20%20%20Americium%20%20%20%20Am%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20243%20%20%20%20%20%20%0A96%20%20%20%20%20%20%20%20Curium%20%20%20%20%20%20%20Cm%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20247%20%20%20%20%20%20%0A97%20%20%20%20%20%20%20%20Berkelium%20%20%20%20Bk%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20247%20%20%20%20%20%20%0A98%20%20%20%20%20%20%20%20Californium%20%20Cf%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20251%20%20%20%20%20%20%0A99%20%20%20%20%20%20%20%20Einsteinium%20%20Es%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20252%20%20%20%20%20%20%0A100%20%20%20%20%20%20%20Fermium%20%20%20%20%20%20Fm%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20257%20 [3]: http://cjam.aditsu.net/#code=363180297747591790604930618402396129685962856524967156551691803198533587214560772335382797%20258b_%3Ae%3Ep_%2Cp%3Acp&input=Mn

deleted 61 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998
Loading
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998
Loading

AltStyle によって変換されたページ (->オリジナル) /