MATLAB, 360 363 290 304 295 byte
Vedi in fondo al post per come testare il vecchio codice con Octave.
Questo codice prende il nome dell'elemento (incluso Kalium, ecc.) E visualizza l'output in formato ASCII ora che le regole sono cambiate.
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')
Le regole sono cambiate da quando ho scritto il codice per richiedere un output ASCII. Ho aggiornato il mio codice per farlo a scapito di 14 byte. Ho salvato 9 byte eliminando reshape () e rendendo la a
matrice la forma giusta per cominciare.
Ecco una spiegazione di come funziona:
%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')
Questo è l'output di Hydrogen (ignora i punti, sono per evitare che le linee vengano rimosse quando vengono mostrate qui):
.
.
.
.
xH .
.
.
.
.
.
Ed ecco l'uscita per il calcio.
.
xx .
xx .
.
xxxCa xxx.
xxx xxx.
.
xx .
xx .
.
E l'output per Natrium, che ora funziona correttamente (prima di Natrium si tradurrebbe in azoto!).
.
x .
xx .
.
xxNa x .
xx x .
.
xx .
.
.
La nuova versione del codice non funziona con Octave in quanto utilizza, spiral()
presente solo in MATLAB.
Puoi comunque testare il vecchio codice usando l' interprete online Octave :
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')
Eseguilo, quindi inserisci una stringa come: 'Idrogeno' (inclusi i segni di virgolette). Una volta fatto, dovrai fare clic sul pulsante Espandi trama (si presenta come un piccolo simbolo grafico nell'angolo in alto a destra dell'interprete) per farlo mostrare tutto. In Octave sfortunatamente aggiunge linee che uniscono i punti, questo non accade in MATLAB. Ma almeno ti consente di testare la logica dietro di esso. Come ho già detto, questo è ancora un output grafico, ma hai l'idea di come vengono cercati gli elementi.