Electron Configuration Diagrams

13

1

Challenge

Given an element name, output its electron configuration.

Input

Your input will be the full name of each element (the range being from Hydrogen, 1, to Calcium, 20) with some exceptions - you should be able to account for the following inputs:

Natrium - This is sodium
Kalium - This is potassium

Note that inputs "sodium" and "potassium" must still be valid.

The first letter will always be capitalised.

Output

You can refer to this webpage for guidance on how to generate an electron configuration diagram.

The following list shows the maximum number of electrons in each shell:

  • 1st shell - 2 electrons
  • 2nd shell - 8 electrons
  • 3rd shell - 8 electrons
  • 4th shell - 14 electrons (although the maximum number of electrons in this shell required is 2)

An example output of the electron configuration is the following:

In the centre of the diagram must be the element's one to two letter symbol.

Either dots or crosses may be used and where they are placed does not matter.

The output does not have to be exactly like this but it does have to be an ASCII graphic. The one thing it cannot be is in the form 2.8.8.1 or any other form like this.

The circles are not necessary

Built in functions which access the periodic table of elements or produce diagrams or electron configurations are hereby disallowed.

Winning

The shortest program in bytes wins.

Beta Decay

Posted 2015-10-28T21:20:59.977

Reputation: 21 478

1Do we need to show the electrons in pairs? – lirtosiast – 2015-10-28T21:34:59.917

5I recommend requiring one of ASCII art or graphical output, otherwise submissions aren't really comparable. – Alex A. – 2015-10-28T22:18:11.447

Is the first letter always capitalised (I hope so...) – Tom Carpenter – 2015-10-29T01:16:34.483

Also in the link they draw them differently - you have both electrons on the first shell at the top, they have one at the top, one at the bottom. – Tom Carpenter – 2015-10-29T01:38:58.490

I assume we can use any symbol to represent an electron (for example a large dot or even a square.) Do we need to include the element symbol in the centre? – Level River St – 2015-10-29T04:20:42.607

Oh and having just looked at the existing answer, do we have to draw the circles? – Level River St – 2015-10-29T04:21:47.080

Also, existing answers seem to have ignored the bit about charge. I suggest you remove it (you didn't specify a range for the charge anyway). Note that Ca-- is theoretically possible, though unlikely. Would the two additional electrons go into the expansion of the 3rd shell to from 8 to 18 (which is where they really belong) or elsewhere? – Level River St – 2015-10-29T04:31:13.470

1

For more accurate info on what happens after 20, see here: https://en.wikipedia.org/wiki/Electron_shell

– Level River St – 2015-10-29T04:34:35.740

@TomCarpenter As long as you show x many electrons on each shell, it doesn't matter where you place the dots/crosses – Beta Decay – 2015-10-29T06:54:35.760

Answers

3

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.

Tom Carpenter

Posted 2015-10-28T21:20:59.977

Reputation: 3 990

Now prints in ASCII. – Tom Carpenter – 2015-10-30T02:20:06.347

2

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.

Mego

Posted 2015-10-28T21:20:59.977

Reputation: 32 998

In the dictionary, couldn't you put the values in a single string like "H 1" and split them at the space later? – Beta Decay – 2015-10-29T07:36:35.620

1That's a long cup of coffee ;) – Beta Decay – 2015-10-29T19:27:41.883

0

j=0
do
{
if(elnum=1)
{
draw_circle(100,100,50)
draw_sprite_ext(spr_electron,sprite num,100,100+50,direction,c_white,c_alpha)

}
else
{
if(elnum=2)
{
draw_circle(100,100,50)
draw_sprite_ext(spr_electron,sprite num,100,100+50,direction,c_white,c_alpha)
draw_sprite_ext(spr_electron,sprite num,100,100+50,direction,c_white,c_alpha)
}
if(j>1&&j<=8)
{
if(j>5)
angdeviation=5
else
angdeviation=-5
draw_circle(100,100,100)
draw_sprite_ext(spr_electron,sprite num,100+length_dirx(j*100+angdeviation),100+length_diry(j*100+angdeviation),direction,c_white,c_alpha)
}
}

}until(j<=enum)

user67059

Posted 2015-10-28T21:20:59.977

Reputation: 1

3Welcome to the site. What language is this in? I would recommend adding a heart like other submissions with all the relevant information. Also since this is [tag:code-golf] you should aim to minimize the length of your program. I can't be sure because I don't know what language you are using here but it looks like you have a good deal of excess whitespace. Most languages have a tips page listing a bunch of helpful golfing tips you can use. I would recommend checking out the page for the language you use here. – Post Rock Garf Hunter – 2017-03-17T05:50:47.440

I am quite sure this is GML (the propietary Game Maker Language): https://en.wikipedia.org/wiki/GameMaker_Studio#GameMaker_Language and https://docs.yoyogames.com/source/dadiospice/002_reference/001_gml%20language%20overview/

– Jerry Jeremiah – 2018-07-18T01:21:47.140

@WW other than the two blank lines I don't see any obvious excess whitespace. The lines don't appear to end with semicolons or some other delimiter so I wouldn't be surprised that there can't be more than one thing on a line. But the variable names could be shorter.... – Jerry Jeremiah – 2018-07-18T01:34:34.583

I am not sure how the "sprite num" part works - from the documentation it looks like it should be an integer and spaces in a variable name wouldn't be allowed: https://docs.yoyogames.com/source/dadiospice/002_reference/drawing/drawing%20sprites%20and%20backgrounds/draw_sprite_ext.html

– Jerry Jeremiah – 2018-07-18T01:38:54.603