Electron Configuration Diagrams

Python 3, 529 bytes

thanks Thomas and Beta for pointing out some things that should've been obvious to me, saving me some bytes

massive improvement: using string slicing instead of dict lookup

s="""    {18}
    {10}{14}
    {2}{6}
    {0}{1}
 {17}{9} %s {3}{11}
 {13}{5}    {7}{15}

    {8}{4}
    {16}{12}
    {19}"""
e="H HeLiBe B C N O F NeNaMgAlSiP S ClArK Ca"
r="hydrogen   helium     lithium    beryllium  boron      carbon     nitrogen   oxygen     fluorine   neon       natrium    sodium     magnesium  aluminium  silicon    phosphoroussulfur     chlorine   argon      kalium     potassium  calcium    "
n=r.find(input().lower())//11
n-=(n>10)+(n>18)
print(s.format(*[' *'[i<=n]for i in range(20)])%e[n*2+1:n*2+3])

Not the prettiest of programs or outputs, but hey, we needed something to kick this challenge off. Try it online.


MATLAB, 360 363 290 304 295 bytes

See at the bottom of the post for how to test the old code with Octave.

This code takes the name of the element (including Kalium, etc.) and displyas the output in ascii format now that the rules have changed.

f=input('');e=1;a=['CPACxxSAMSNxxxxxBLHxCKACSPSAMNNFONCBBLHH';'aorhxxilaoexxxxxeiexa rl  ilgae     eie '];for s=a;n=s(s~=32);if strncmpi(n,f,nnz(n));break;end;e=mod(e,20)+1;end;s=spiral(10);p=[8,18,33,28,23,39,60,53,46,95];p=[p;p+1];o=s*0;o(ismember(s,p(1:21-e)))='x';o(45:46)=a(:,e+20);char(o')

The rules changed since I wrote the code to require an ASCII output. I have updated my code to do this at the expense of 14 bytes. I have saved 9 bytes by getting rid of the reshape() and just making the a matrix the right shape to begin with.

Here is an explanation of how it works:

%Get the name - actually we only need at most the first two characters, but the whole thing will do
f=input('');
e=1;
%This bit makes a map which allows us to find the element (including with
%the names like Kalium. All of the elements appear twice, with the actual
%symbols being the second set. The first set gets all those whose names are
%either more than one character, or don't begin with the first two
%characters of the short for (e.g. Sodium). The string is reshaped into a
%2x40 array. 'Natrium' is a pain in the neck as it as it would get caught
%by 'N' for 'Nitrogen'. I have reversed the element order - so that all the
%ones beginning with N come before N. Some maths is done later on to
%correct for the number of electrons - basically 21-e so 1 becomes 20.
a=['CPACxxSAMSNxxxxxBLHxCKACSPSAMNNFONCBBLHH';'aorhxxilaoexxxxxeiexa rl  ilgae     eie '];

%For each group of 2 in the array of elements
for s=a
    
    %Remove any spaces from the name
    n=s(s~=32);
    
    %Do a comparison of the first one or two characters of the requested string
    if (strncmpi(n,f,nnz(n))) 
        
        %break once the element is found
        break; 
    end
    
    %If not this element add another electron. We wrap around after 20 as there are two copies of each
    e=mod(e,20)+1; 
end
%e is now number of electrons

%Generate an array of points for each electron
s=spiral(10);
p=[8,18,33,28,23,39,60,53,46,95];p=[p;p+1];

%make an output array
o=s*0;

%Plot all the points in is up to and including the number of electrons (see the notes above for why 21-e)
o(ismember(s,p(1:21-e)))='x';

%And add the text in the centre - we extract the element name from the second group appearance in the 'a' array, hence adding 20.
o(45:46)=a(:,e+20);

%Display the result
char(o')

This is the output for Hydrogen (ignore the dots, they are to avoid the lines being removed when showing here):

          .
          .
          .
          .
   xH     .
          .
          .
          .
          .
          .

And here is the output for Calcium.

          .
    xx    .
    xx    .
          .
 xxxCa xxx.
 xxx   xxx.
          .
    xx    .
    xx    .
          .

And the output for Natrium, which now works properly (before Natrium it would result in Nitrogen!).

          .
     x    .
    xx    .
          .
  xxNa x  .
  xx   x  .
          .
    xx    .
          .
          .

The new version of the code doesn't work with Octave as it uses spiral() which is only present in MATLAB.

You can however test the old code using the Octave online interpreter:

f=input('');e=1;a=['CPACxxSAMSNxxxxxBLHxCKACSPSAMNNFONCBBLHH';'aorhxxilaoexxxxxeiexa rl  ilgae     eie '];for s=a;n=s(s~=32);if strncmpi(n,f,nnz(n));break;end;e=mod(e,20)+1;end;u=14:(34-e);r=floor(u/8);t=u*pi/4;polar(t,r,'o');text(0,0,a(:,e+20)','horizontalalignment','c') 

Run that, then enter a string like: 'Hydrogen' (including the quote marks). Once it is done, you will have to click the expand plot button (looks like a little graph symbol in the top right corner of the interpreter) to get it to show the full thing. In Octave it unfortunately add lines joining the points, this does not happen in MATLAB. But at least it allows you to test the logic behind it. As I say, this is still a graphical output, but you get the idea of how the elements are looked up.