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

minor update
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
s=>(i=`23>3c9@94=4E33:3Q0I3:9[020G0O3@74M9=4A6@4;7J5H96?9K0L723E4343A4R40A0T9D3J5H4?4?3J329>9P9;359:0B9^3=95h6:9<6M0}E0@0N3<3;09<6M0~D0@0N3<3;0:9@4<4<6H3I3`.replace(/\D/g,s=>"2".repeat(Buffer(s)[0]-56))[h=parseInt(s,36)*184%901])-9?s[0]+[s[i-1]]:`Ag,Fe,Hg,,W,Sn,K,Cu,Pb,,Sb,Au,,Na`.split`,`[h%418%48%15]||'Uu'+s[4]

Try it online! Try it online!

  • Group 1: 12 elements whose symbol is the first letter of their name. Example:, such as Carbon -> C.

  • Group 2: 91 elements whose symbol is the first letter of their name, followed by the \$n\$-th letter (0-indexed) with \1ドル\le n \le6\$. Examples: Helium -> He (\$n=1\$), Zirconium -> Zr (\$n=2\$).

  • Group 3: 15 elements that do not belong to the other groups. Example:, such as Iron -> Fe.

It is worth noting that \44ドル\$44 symbols simply consist of the first two letters of the element name. That's almost 50% of group 2. These ones will be encoded with a "2", so that's the padding character that we are goingwant to use in our lookup string.

The corresponding lookup string is \899ドル\$ characters long, which is pretty big.

But, by design, we have many runs of consecutive \2ドル\$'s in there. Our compression strategy is to replace each run of \2ドル\$'s of length \2ドル\le n\le69\$\2ドル\le n\le70\$ with the character whose ASCII code is \$n+56\$ (that's the range going from ":" to "~").

The compressed string is only \132ドル\$ characters long.

You can see the decompression process here:Try it online!

Try it online!

(NB: For group 1, s[-1] is undefined and [undefined] is coerced to an empty string.)

There are 4 remaining superheavy elements that are not included above. WeBecause they are referred to by their temporary names and thanks to the systematic nomenclature , we can build the symbols for these ones by concatenating "Uu" with the fifth letter of their name:

s=>(i=`23>3c9@94=4E33:3Q0I3:9[020G0O3@74M9=4A6@4;7J5H96?9K0L723E4343A4R40A0T9D3J5H4?4?3J329>9P9;359:0B9^3=95h6:9<6M0}E0@0N3<3;0:9@4<4<6H3I3`.replace(/\D/g,s=>"2".repeat(Buffer(s)[0]-56))[h=parseInt(s,36)*184%901])-9?s[0]+[s[i-1]]:`Ag,Fe,Hg,,W,Sn,K,Cu,Pb,,Sb,Au,,Na`.split`,`[h%418%48%15]||'Uu'+s[4]

Try it online!

  • Group 1: 12 elements whose symbol is the first letter of their name. Example: Carbon -> C

  • Group 2: 91 elements whose symbol is the first letter of their name, followed by the \$n\$-th letter (0-indexed) with \1ドル\le n \le6\$. Examples: Helium -> He (\$n=1\$), Zirconium -> Zr (\$n=2\$)

  • Group 3: 15 elements that do not belong to the other groups. Example: Iron -> Fe

It is worth noting that \44ドル\$ symbols simply consist of the first two letters of the element name. That's almost 50% of group 2. These ones will be encoded with a "2", so that's the padding character that we are going to use in our lookup string.

The corresponding lookup string is \899ドル\$ characters long, which is pretty big. But, by design, we have many runs of consecutive \2ドル\$'s in there. Our compression strategy is to replace each run of \2ドル\$'s of length \2ドル\le n\le69\$ with the character whose ASCII code is \$n+56\$. The compressed string is only \132ドル\$ characters long.

You can see the decompression process here:Try it online!

For group 1, s[-1] is undefined and [undefined] is coerced to an empty string.

There are 4 remaining elements that are not included. We can build the symbols for these ones by concatenating "Uu" with the fifth letter of their name:

s=>(i=`23>3c9@94=4E33:3Q0I3:9[020G0O3@74M9=4A6@4;7J5H96?9K0L723E4343A4R40A0T9D3J5H4?4?3J329>9P9;359:0B9^3=95h6:9<6M0~D0@0N3<3;0:9@4<4<6H3I3`.replace(/\D/g,s=>"2".repeat(Buffer(s)[0]-56))[h=parseInt(s,36)*184%901])-9?s[0]+[s[i-1]]:`Ag,Fe,Hg,,W,Sn,K,Cu,Pb,,Sb,Au,,Na`.split`,`[h%418%48%15]||'Uu'+s[4]

Try it online!

  • Group 1: 12 elements whose symbol is the first letter of their name, such as Carbon -> C.

  • Group 2: 91 elements whose symbol is the first letter of their name, followed by the \$n\$-th letter (0-indexed) with \1ドル\le n \le6\$. Examples: Helium -> He (\$n=1\$), Zirconium -> Zr (\$n=2\$).

  • Group 3: 15 elements that do not belong to the other groups, such as Iron -> Fe.

It is worth noting that 44 symbols simply consist of the first two letters of the element name. That's almost 50% of group 2. These ones will be encoded with a "2", so that's the padding character that we want to use in our lookup string.

The corresponding lookup string is \899ドル\$ characters long, which is pretty big.

But, by design, we have many runs of consecutive \2ドル\$'s in there. Our compression strategy is to replace each run of \2ドル\$'s of length \2ドル\le n\le70\$ with the character whose ASCII code is \$n+56\$ (that's the range going from ":" to "~").

The compressed string is only \132ドル\$ characters long.

You can see the decompression process here:

Try it online!

(NB: For group 1, s[-1] is undefined and [undefined] is coerced to an empty string.)

There are 4 remaining superheavy elements that are not included above. Because they are referred to by their temporary names and thanks to the systematic nomenclature , we can build the symbols for these ones by concatenating "Uu" with the fifth letter of their name:

added an explanation
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (Node.js), 298 bytes

s=>(i=`23>3c9@94=4E33:3Q0I3:9[020G0O3@74M9=4A6@4;7J5H96?9K0L723E4343A4R40A0T9D3J5H4?4?3J329>9P9;359:0B9^3=95h6:9<6M0}E0@0N3<3;0:9@4<4<6H3I3`.replace(/\D/g,s=>"2".repeat(Buffer(s)[0]-56))[h=parseInt(s,36)*184%901])-9?s[0]+[s[i-1]]:`Ag,Fe,Hg,,W,Sn,K,Cu,Pb,,Sb,Au,,Na`.split`,`[h%418%48%15]||'Uu'+s[4]

Try it online!

How?

Element groups

We split the elements into 3 groups:

  • Group 1: 12 elements whose symbol is the first letter of their name. Example: Carbon -> C

  • Group 2: 91 elements whose symbol is the first letter of their name, followed by the \$n\$-th letter (0-indexed) with \1ドル\le n \le6\$. Examples: Helium -> He (\$n=1\$), Zirconium -> Zr (\$n=2\$)

  • Group 3: 15 elements that do not belong to the other groups. Example: Iron -> Fe

This means that all elements can be encoded with a single digit: we use \0ドル\$ for group 1, \$n+1\$ for group 2 and \9ドル\$ for group 3. (Obviously, we'll need some extra data for group 3. More on that later.)

It is worth noting that \44ドル\$ symbols simply consist of the first two letters of the element name. That's almost 50% of group 2. These ones will be encoded with a "2", so that's the padding character that we are going to use in our lookup string.

Hash function, lookup string and compression

We apply the following hash function (found by brute-force) on the input string:

h = parseInt(s, 36) * 184 % 901

The corresponding lookup string is \899ドル\$ characters long, which is pretty big. But, by design, we have many runs of consecutive \2ドル\$'s in there. Our compression strategy is to replace each run of \2ドル\$'s of length \2ドル\le n\le69\$ with the character whose ASCII code is \$n+56\$. The compressed string is only \132ドル\$ characters long.

You can see the decompression process here: Try it online!

Decoding

Groups 1 and 2 are decoded the same way:

s[0] + [s[i - 1]]

For group 1, s[-1] is undefined and [undefined] is coerced to an empty string.

For group 3, we use another hash function and another lookup string (also found by brute-force):

`Ag,Fe,Hg,,W,Sn,K,Cu,Pb,,Sb,Au,,Na`.split`,`[h % 418 % 48 % 15]

There are 4 remaining elements that are not included. We can build the symbols for these ones by concatenating "Uu" with the fifth letter of their name:

 v
Ununtrium -> Uut
Ununpentium -> Uup
Ununseptium -> Uus
Ununoctium -> Uuo
 ^

Hence the last part of the code:

|| 'Uu' + s[4]

JavaScript (Node.js), 298 bytes

s=>(i=`23>3c9@94=4E33:3Q0I3:9[020G0O3@74M9=4A6@4;7J5H96?9K0L723E4343A4R40A0T9D3J5H4?4?3J329>9P9;359:0B9^3=95h6:9<6M0}E0@0N3<3;0:9@4<4<6H3I3`.replace(/\D/g,s=>"2".repeat(Buffer(s)[0]-56))[h=parseInt(s,36)*184%901])-9?s[0]+[s[i-1]]:`Ag,Fe,Hg,,W,Sn,K,Cu,Pb,,Sb,Au,,Na`.split`,`[h%418%48%15]||'Uu'+s[4]

Try it online!

JavaScript (Node.js), 298 bytes

s=>(i=`23>3c9@94=4E33:3Q0I3:9[020G0O3@74M9=4A6@4;7J5H96?9K0L723E4343A4R40A0T9D3J5H4?4?3J329>9P9;359:0B9^3=95h6:9<6M0}E0@0N3<3;0:9@4<4<6H3I3`.replace(/\D/g,s=>"2".repeat(Buffer(s)[0]-56))[h=parseInt(s,36)*184%901])-9?s[0]+[s[i-1]]:`Ag,Fe,Hg,,W,Sn,K,Cu,Pb,,Sb,Au,,Na`.split`,`[h%418%48%15]||'Uu'+s[4]

Try it online!

How?

Element groups

We split the elements into 3 groups:

  • Group 1: 12 elements whose symbol is the first letter of their name. Example: Carbon -> C

  • Group 2: 91 elements whose symbol is the first letter of their name, followed by the \$n\$-th letter (0-indexed) with \1ドル\le n \le6\$. Examples: Helium -> He (\$n=1\$), Zirconium -> Zr (\$n=2\$)

  • Group 3: 15 elements that do not belong to the other groups. Example: Iron -> Fe

This means that all elements can be encoded with a single digit: we use \0ドル\$ for group 1, \$n+1\$ for group 2 and \9ドル\$ for group 3. (Obviously, we'll need some extra data for group 3. More on that later.)

It is worth noting that \44ドル\$ symbols simply consist of the first two letters of the element name. That's almost 50% of group 2. These ones will be encoded with a "2", so that's the padding character that we are going to use in our lookup string.

Hash function, lookup string and compression

We apply the following hash function (found by brute-force) on the input string:

h = parseInt(s, 36) * 184 % 901

The corresponding lookup string is \899ドル\$ characters long, which is pretty big. But, by design, we have many runs of consecutive \2ドル\$'s in there. Our compression strategy is to replace each run of \2ドル\$'s of length \2ドル\le n\le69\$ with the character whose ASCII code is \$n+56\$. The compressed string is only \132ドル\$ characters long.

You can see the decompression process here: Try it online!

Decoding

Groups 1 and 2 are decoded the same way:

s[0] + [s[i - 1]]

For group 1, s[-1] is undefined and [undefined] is coerced to an empty string.

For group 3, we use another hash function and another lookup string (also found by brute-force):

`Ag,Fe,Hg,,W,Sn,K,Cu,Pb,,Sb,Au,,Na`.split`,`[h % 418 % 48 % 15]

There are 4 remaining elements that are not included. We can build the symbols for these ones by concatenating "Uu" with the fifth letter of their name:

 v
Ununtrium -> Uut
Ununpentium -> Uup
Ununseptium -> Uus
Ununoctium -> Uuo
 ^

Hence the last part of the code:

|| 'Uu' + s[4]
saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (Node.js), 299298 bytes

s=>(i=`23>3c9@94=4E33:3Q0I3:9[020G0O3@74M9=4A6@4;7J5H96?9K0L723E4343A4R40A0T9D3J5H4?4?3J329>9P9;359:0B9^3=95h6:9<6M0}E0@0N3<3;0:9@4<4<6H3I3`.replace(/\D/g,s=>"2".repeat(Buffer(s)[0]-56))[h=parseInt(s,36)*184%901])-9?s[0]+[s[i-1]]:`,Ag,,Sb,Pb`Ag,Fe,CuHg,,W,Fe,Sn,,K,Cu,Pb,,Sb,Au,Hg,Na`.split`,`[h%105%22]||'Uu'+s[4]`[h%418%48%15]||'Uu'+s[4]

Try it online! Try it online!

JavaScript (Node.js), 299 bytes

s=>(i=`23>3c9@94=4E33:3Q0I3:9[020G0O3@74M9=4A6@4;7J5H96?9K0L723E4343A4R40A0T9D3J5H4?4?3J329>9P9;359:0B9^3=95h6:9<6M0}E0@0N3<3;0:9@4<4<6H3I3`.replace(/\D/g,s=>"2".repeat(Buffer(s)[0]-56))[h=parseInt(s,36)*184%901])-9?s[0]+[s[i-1]]:`,Ag,,Sb,Pb,,Cu,,W,Fe,Sn,,K,,,Au,Hg,Na`.split`,`[h%105%22]||'Uu'+s[4]

Try it online!

JavaScript (Node.js), 298 bytes

s=>(i=`23>3c9@94=4E33:3Q0I3:9[020G0O3@74M9=4A6@4;7J5H96?9K0L723E4343A4R40A0T9D3J5H4?4?3J329>9P9;359:0B9^3=95h6:9<6M0}E0@0N3<3;0:9@4<4<6H3I3`.replace(/\D/g,s=>"2".repeat(Buffer(s)[0]-56))[h=parseInt(s,36)*184%901])-9?s[0]+[s[i-1]]:`Ag,Fe,Hg,,W,Sn,K,Cu,Pb,,Sb,Au,,Na`.split`,`[h%418%48%15]||'Uu'+s[4]

Try it online!

saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading

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