17
\$\begingroup\$

In atomic physics and quantum chemistry, the electron configuration is the distribution of electrons of an atom in atomic orbitals. For example, the electron configuration of the neon atom is 1s2 2s2 2p6. (From Wikipedia)

Challenge

Your challenge is to take a number representing the atomic number of an element and output the electron configuration of that element as defined by the Aufbau principle.

Iron (26) has the electron configuration 1s2 2s2 2p6 3s2 3p6 3d6 4s2. However, superscripts are unnecessary; the output for 26 should be along the lines of 1s2 2s2 2p6 3s2 3p6 3d6 4s2.

Specification

  • You do not have to handle any inputs outside of the range 1 <= n <= 118.
  • Your output should look something like the test cases, but you may use any non-digit character/characters (aside from s, p, d, and f) to delimit the different orbitals.
  • You must return/print a string containing the orbital names/values/delmiters; you cannot simply return/print an array.
  • You do not need to handle any exceptions to the Aufbau principle; where there are exceptions, printing the "incorrect" configuration is fine.

Examples:

Input -> Valid output            -> Invalid output
16    -> 1s2 2s2 2p6 3s2 3p4     -> 1s22s22p63s23p4
16    -> 1s2, 2s2, 2p6, 3s2, 3p4 -> [[1, 2], [2, 2], [2, 6], [3, 2], [3, 4]]
17    -> 1s2+2s2+2p6+3s2+3p5     -> 1s2s2s2s2p6p3s2s3p5

Here is a list of all the electronic orbitals. The maximum values they can contain are below the name:

name: 1s 2s 2p 3s 3p 3d 4s 4p 4d 5s 5p 4f 5d 6s 6p 5f 6d 7s 7p
max:  2  2  6  2  6  10 2  6  10 2  6  14 10 2  6  14 10 2  6

Test Cases

Input -> Output
1     -> 1s1
2     -> 1s2
16    -> 1s2 2s2 2p6 3s2 3p4
50    -> 1s2 2s2 2p6 3s2 3p6 3d10 4s2 4p6 4d10 5s2 5p2
115   -> 1s2 2s2 2p6 3s2 3p6 3d10 4s2 4p6 4d10 5s2 5p6 4f14 5d10 6s2 6p6 5f14 6d10 7s2 7p3

Here is a complete list and a reference implementation of sorts (Try it online!)

Winning Condition

As this is , the shortest code wins!

\$\endgroup\$
  • 2
    \$\begingroup\$ IIRC every challenge that runs on a finite set of inputs with a constant output is a candidate for the kolmogorov tag. Nice challenge. \$\endgroup\$ – Uriel Dec 12 '17 at 0:33
  • 6
    \$\begingroup\$ In the test cases, 3d seems to be filled before 4s, 4d before 5s, 6s after 4f and 5d, which violate the Madelung rule. Should we golf programs that print the incorrect electron configurations on the pastebin? \$\endgroup\$ – JungHwan Min Dec 12 '17 at 1:29
  • 5
    \$\begingroup\$ Also, there are exceptions to the Aufbau principle (like Chromium (atomic no. 24) having 4s1 3d5 instead of 4s2 3d4). I see that it was asked in the sandbox post but was never answered. Do we ignore that issue? \$\endgroup\$ – JungHwan Min Dec 12 '17 at 1:37
  • 1
    \$\begingroup\$ OMG I swear I was going to post this exact same question...today \$\endgroup\$ – FantaC Dec 12 '17 at 3:21
  • 1
    \$\begingroup\$ @Uriel status-completed \$\endgroup\$ – MD XF Dec 12 '17 at 17:43
2
+100
\$\begingroup\$

Jelly, 63 62 56 55 bytes

ḊFµi@€QḤ’Ḥ
“ŒµḊuÆẓƙỊ’D,“çƥ÷£ḟ’ṃ“spdf”¤µxÇZ
¢ḣŒg'µQ€żL€K

Try it online!

Thanks to user202729 for saving 6 bytes with base decompression!

Explanation

First I construct the list [[1,2,2,3,3,3,4,4,4,5,5,4,5,6,6,5,6,7,7],'sspspdspdspfdspfdsp'] with the code “ŒµḊuÆẓƙỊ’D,“çƥ÷£ḟ’ṃ“spdf”¤ in the second link.

  • “ŒµḊuÆẓƙỊ’ is the number 1223334445545665677 compressed into base 250. D gives turns this into a list of digits.
  • “çƥ÷£ḟ’ṃ“spdf” changes the base 250 number “çƥ÷£ḟ’ into base 4 and indexes it into the string “spdf” yielding 'sspspdspdspfdspfdsp'. This was contributed by user202729.

The list is then take to the fist link by Ç. The first link does the following:

ḊFµQiЀµḤ’Ḥ
ḊF           Dequeue then flatten yields 'sspspd...'. Ṫ doesn't work because it modifies the input.
  µ          New monadic link
   Q         Unique elements → 'spdf'
    iЀ      The index of each of 'sspspd...' into the string 'spdf' → [1,1,2,1,2,3...]
       µ     New monadic link. This prevents Ḥ from acting on the right argument of iЀ.
        Ḥ’Ḥ  Takes [1,1,2,1...] and computes 2(2l+1) → [2,2,6,2,6,10...]

Now back to the second link. With we repeat each of the elements in each sublist of [[1,2,2,3...7],['sspspd...p']] by the numbers in our new list [2,2,6...]. This yields [[1,1,2,2,2,2...],['sssspp...']]. Z zips the two sublist which yields [[1,'s'],[1,'s'],[2,'s']...].

Now to the main link. ¢ calls the second link which yields the final list of tuples described above. Assume the input to the program is 5 as an example.

¢ḣŒg'µQ€żL€K
¢             Calls the second link as a nilad which yields the final list of tuples described above
 ḣ            Takes the first 5 tuples → [[1,'s'],[1,'s'],[2,'s'],[2,'s'],[2,'p']]
  Œg'         Group together runs of equal elements → [[[1,'s'],[1,'s']],[[2,'s'],[2,'s']],[[2,'p']]]
     µ        New monadic link
      Q€      Unique elements of each of these runs
         L€   Length of each of these runs
        ż     Zip these together → [[[1,'s'],2],[[2,'s'],2],[[2,'p'],1]]
           K  Join this list with spaces → 1s2 2s2 2p1
\$\endgroup\$
  • \$\begingroup\$ Any way to compress the sspspdspd... string? \$\endgroup\$ – MD XF Dec 22 '17 at 5:21
  • \$\begingroup\$ @MDXF I tried but it ended up being longer. I also tried to construct it various ways and individual parts were shorter but as a whole it was longer \$\endgroup\$ – dylnan Dec 22 '17 at 13:09
  • \$\begingroup\$ @dylnan “çƥ÷£ḟ’ṃ“spdf”¤ for -6 byte. Used this for base 250 integer and for base decompression. \$\endgroup\$ – user202729 Dec 23 '17 at 8:59
  • \$\begingroup\$ @user202729 nice, thank you! \$\endgroup\$ – dylnan Dec 23 '17 at 16:46
7
\$\begingroup\$

Imperative Tampio, 930 bytes

Yöllä on ilot.Olkoon oma ilo uusi yö, jonka iloja ovat ilo"1s",ilo"2s",ilo"2p",ilo"3s",ilo"3p",ilo"3d",ilo"4s",ilo"4p",ilo"4d",ilo"5s",ilo"5p",ilo"4f",ilo"5d",ilo"6s",ilo"6p",ilo"5f",ilo"6d",ilo"7s"ja ilo"7p".Olkoon iso yö uusi yö, jonka iloja ovat 2,2,6,2,6,10,2,6,10,2,6,14,10,2,6,14,10,2 ja 6.Kun iso luku juo ison ilon,iso ilo näyttää oman yön,missä oma yö on oman ilon ensimmäinen ilo ja ujo ilo on ison yön ensimmäinen ilo,jos iso luku on suurempi kuin ujo ilo,niin iso ilo näyttää ujon ilon,iso ilo näyttää ilon" ",oman ilon iloiksi asetetaan oman ilon ilot toisesta alkaen,ison yön iloiksi asetetaan ison yön ilot toisesta alkaen ja iso luku vähennettynä ujolla ilolla juo ison ilon ja,jos iso luku on pienempi tai yhtä suuri kuin ujo ilo,niin iso ilo näyttää ison luvun.Olkoon oma muuttuja uusi muuttuja.Kun iso sivu avautuu,omaan muuttujaan luetaan luku ja oman muuttujan arvo juo ison sivun.

Yöllä on ilot.Olkoon oma ilo uusi yö, jonka iloja ovat ilo"1s",ilo"2s",ilo"2p",ilo"3s",ilo"3p",ilo"3d",ilo"4s",ilo"4p",ilo"4d",ilo"5s",ilo"5p",ilo"4f",ilo"5d",ilo"6s",ilo"6p",ilo"5f",ilo"6d",ilo"7s"ja ilo"7p".Olkoon iso yö uusi yö, jonka iloja ovat 2,2,6,2,6,10,2,6,10,2,6,14,10,2,6,14,10,2 ja 6.Kun iso luku juo ison ilon,iso ilo näyttää oman yön,missä oma yö on oman ilon ensimmäinen ilo ja ujo ilo on ison yön ensimmäinen ilo,jos iso luku on suurempi kuin ujo ilo,niin iso ilo näyttää ujon ilon,iso ilo näyttää ilon" ",oman ilon iloiksi asetetaan oman ilon ilot toisesta alkaen,ison yön iloiksi asetetaan ison yön ilot toisesta alkaen ja iso luku vähennettynä ujolla ilolla juo ison ilon ja,jos iso luku on pienempi tai yhtä suuri kuin ujo ilo,niin iso ilo näyttää ison luvun.Olkoon oma muuttuja uusi muuttuja.Kun iso sivu avautuu,omaan muuttujaan luetaan luku ja oman muuttujan arvo juo ison sivun.

Online version

It is a very straightforward implementation. In the golfed version I simply replaced the words with short words like ilo, , iso, oma, etc.

Ungolfed:

Listalla on alkiot.

Olkoon lyhyt orbitaalilista uusi lista, jonka alkioita ovat orbitaali "1s", orbitaali "2s", orbitaali "2p", orbitaali "3s", orbitaali "3p", orbitaali "3d", orbitaali "4s", orbitaali "4p", orbitaali "4d", orbitaali "5s", orbitaali "5p", orbitaali "4f", orbitaali "5d", orbitaali "6s", orbitaali "6p", orbitaali "5f", orbitaali "6d", orbitaali "7s" ja orbitaali "7p".

Olkoon lyhyt maksimilista uusi lista, jonka alkioita ovat 2, 2, 6, 2, 6, 10, 2, 6, 10, 2, 6, 14, 10, 2, 6, 14, 10, 2 ja 6.

Kun jaetaan orbitaaleille pienehkö elektronimäärä nykyisellä sivulla,

  • nykyinen sivu näyttää nykyisen orbitaalin, missä nykyinen orbitaali on lyhyen orbitaalilistan ensimmäinen alkio ja nykyinen maksimi on lyhyen maksimilistan ensimmäinen alkio,
  • jos pienehkö elektronimäärä on suurempi kuin nykyinen maksimi, niin

    • nykyinen sivu näyttää nykyisen maksimin,
    • nykyinen sivu näyttää välin " ",
    • lyhyen orbitaalilistan alkioiksi asetetaan lyhyen orbitaalilistan alkiot toisesta alkaen,
    • lyhyen maksimilistan alkioiksi asetetaan lyhyen maksimilistan alkiot toisesta alkaen
    • ja jaetaan orbitaaleille pienehkö elektronimäärä vähennettynä nykyisellä maksimilla nykyisellä sivulla,
  • ja, jos pienehkö elektronimäärä on pienempi tai yhtä suuri kuin nykyinen maksimi,
    • niin nykyinen sivu näyttää pienehkön elektronimäärän.

Olkoon mukava muuttuja uusi muuttuja.

Kun nykyinen sivu avautuu,

  • mukavaan muuttujaan luetaan luku
  • ja jaetaan orbitaaleille mukavan muuttujan arvo nykyisellä sivulla.

Online version

Translation:

A list has items.

Let the short orbital list be a new list, its items are the orbital "1s", the orbital "2s", the orbital "2p", the orbital "3s", the orbital "3p", the orbital "3d", the orbital "4s", the orbital "4p", the orbital "4d", the orbital "5s", the orbital "5p", the orbital "4f", the orbital "5d", the orbital "6s", the orbital "6p", the orbital "5f", the orbital "6d", the orbital "7s" and the orbital "7p".

Let the short maximum list be a new list, its items are 2, 2, 6, 2, 6, 10, 2, 6, 10, 2, 6, 14, 10, 2, 6, 14, 10, 2 and 6.

When a small number of electrons is divided to the orbitals at the current page,

  • the current page shows the current orbital, where the current orbital is the first item in the short orbital list and the current maximum is the first element in the short maximum list,
  • if the small number of electrons is greater than the current maximum,
    • the current page shows the current maximum,
    • the current page shows the space " ",
    • the elements of the short orbital list are set to be the elements of the short orbital list beginning from the second,
    • the elements of the short maximum list are set to be the elements of the short maximum list beginning from the second
    • and the small number of electrons subtracted by one is divided to the orbitals at the current page,
  • and, if the small number of electrons is less than or equal to the current maximum,
    • the current page shows the small number or electrons.

Let the nice variable be a new variable.

When the current page opens,

  • a number is read to the nice variable
  • and the value of the nice variable is divided to the orbitals at the current page.

The translation is approximate, I had to change the word order to make the English more natural.

\$\endgroup\$
  • 1
    \$\begingroup\$ wtf that's good... \$\endgroup\$ – FantaC Dec 13 '17 at 20:12
  • \$\begingroup\$ Surely there is a more tacit language that has all the features of this one. \$\endgroup\$ – No one Dec 19 '17 at 22:55
  • \$\begingroup\$ Could you be so kind as to add a translation into English so we can understand this language? \$\endgroup\$ – Zacharý Dec 23 '17 at 2:12
  • \$\begingroup\$ @Zacharý I added it. \$\endgroup\$ – fergusq Dec 23 '17 at 8:50
6
\$\begingroup\$

Python 2, 129 128 bytes

-1 byte thanks to notjagan

n=input()
d='spdf'.find
s='sspspdspdspfdspfdsp'
i=0
while n>0:c=s[i];t=d(c)*4+2;print`s[:i].count(c)-~d(c)`+c,min(t,n);n-=t;i+=1

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Charcoal, 72 bytes

Nθ≔”{⊞″I⌀⁼C$Pπ3”α≔⁰ιW›θ⁰«§”o⧴∨c▷⎇_'l|”ι§αι≔⁺×⁴⌕spdf§αι²εI⌊⟦εθ⟧→≔⊕ιι≔⁻θεθ

Try it online!

Here you have the verbose version.

\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 102 bytes

n=>'0010120120132013201'.replace(/./g,k=>n?++p[m=k*4+2,n-=e=m>n?n:m,k]+'spdf'[k]+e+' ':'',p=[0,1,2,3])

Test cases

let f =

n=>'0010120120132013201'.replace(/./g,k=>n?++p[m=k*4+2,n-=e=m>n?n:m,k]+'spdf'[k]+e+' ':'',p=[0,1,2,3])

console.log(f(  1)) // -> 1s1
console.log(f(  2)) // -> 1s2
console.log(f( 16)) // -> 1s2 2s2 2p6 3s2 3p4
console.log(f( 50)) // -> 1s2 2s2 2p6 3s2 3p6 3d10 4s2 4p6 4d10 5s2 5p2
console.log(f(115)) // -> 1s2 2s2 2p6 3s2 3p6 3d10 4s2 4p6 4d10 5s2 5p6 4f14 5d10 6s2 6p6 5f14 6d10 7s2 7p3

Formatted and commented

n =>                          // given the atomic number n
  '0010120120132013201'       // list of azimuthal quantum numbers
  .replace(/./g, k =>         // replace each character k in the above string with:
    n ?                       //   if n does not equal 0:
      ++p[                    //     ++p[k] = updated principal quantum number
        m = k * 4 + 2,        //       m = maximum number of electrons
        n -=                  //       subtract from n:
          e = m > n ? n : m,  //         e = min(m, n) = number of electrons
        k                     //       index actually used to access the p[] array
      ] +                     //     followed by:
      'spdf'[k] +             //     the label
      e + ' '                 //     and the number of electrons
    :                         //   else:
      '',                     //     an empty string
    p = [0, 1, 2, 3]          //   initial list of principal quantum numbers
  )                           // end of replace()
\$\endgroup\$
2
\$\begingroup\$

Swift, 177 175 156 bytes

Loosly based on @Arnauld's Javascript answer

func f(n:Int){var i=n,a=0,b=[0,1,2,3];[0,0,1,0,1,2,0,1,2,0,1,3,2,0,1,3,2,0,1].map{a=$0*4+2;b[$0]+=1;i>0 ?print(b[$0],"spdf".map{$0}[$0],min(a,i)):();i-=a}}

Try it online!

Without the spaces in the electron groups, 190 187 169 bytes:

func f(n:Int){var i=n,a=0,b=[0,1,2,3];[0,0,1,0,1,2,0,1,2,0,1,3,2,0,1,3,2,0,1].map{a=$0*4+2;b[$0]+=1;i>0 ?print(b[$0],"spdf".map{$0}[$0],min(a,i),separator:""):();i-=a}}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 260 187 167 156 152 147 143 138 bytes

i,*m;f(e){for(m=L"...",i=0;e>0;printf("%.2s%d ","1s2s2p3s3p3d4s4p4d5s5p4f5d6s6p5f6d7s7p"+i++*2,(e-=*m)<0?*m+e:*m++));}

Try it online! Golfed from the reference implementation.

StackExchange removes unprintables, so the value of m is replaced with "...".

Here is a reversible hexdump of the program, since it uses unprintables in a string, which replaces the integer array {2,2,6,2,6,10,2,6,10,2,6,14,10,2,6,14,10,2,6} with the literal byte values of the integers.

00000000: 692c 2a6d 3b66 2865 297b 666f 7228 6d3d  i,*m;f(e){for(m=
00000010: 4c22 0202 0602 065c 6e02 065c 6e02 060e  L".....\n..\n...
00000020: 5c6e 0206 0e5c 6e02 0622 2c69 3d30 3b65  \n...\n..",i=0;e
00000030: 3e30 3b70 7269 6e74 6628 2225 2e32 7325  >0;printf("%.2s%
00000040: 6420 222c 2231 7332 7332 7033 7333 7033  d ","1s2s2p3s3p3
00000050: 6434 7334 7034 6435 7335 7034 6635 6436  d4s4p4d5s5p4f5d6
00000060: 7336 7035 6636 6437 7337 7022 2b69 2b2b  s6p5f6d7s7p"+i++
00000070: 2a32 2c28 652d 3d2a 6d29 3c30 3f2a 6d2b  *2,(e-=*m)<0?*m+
00000080: 653a 2a6d 2b2b 2929 3b7d                 e:*m++));}

Alternatively, you could just copy the code from the TIO link.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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