Over a million developers have joined DZone.

Java Code Challenge: Chemical Symbol Naming — Part Two

The sequel to Sam Atkinson's most popular Code Challenge yet. Try to solve it and see what happens!

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Check out part one here if you haven't completed it yet.

Java Code Challenge is a regular segment taking the best challenge from Reddit's dailyprogrammer. Things are a little different here as we're focused on Java. A working solution is not enough; we're looking for the cleanest Java code with tests. 3rd party libraries are welcome but if you can do it without it will be easier for others to comprehend.

If you can fit your solution in the comments then go for it, but preferably put your answer in GitHub and link in the comments. Next week we'll be sharing the best solutions and sharing the best code practices we see!


The Splurth Council of Atoms and Atom-Related Paraphernalia has decided to keep their current naming conventions, as listed in the Easy challenge, but to add a preference system. So while there are still 6 valid symbols for the element Iron, the preferred symbol is Ir. The second-most preferred symbol is Io, then InRoRn, and finally On. A symbol is preferred based on how early in the element name its first letter is, followed by how early its second letter is.

In the case of repeated letters like in NeonEo is preferred to En, even though an n is closer to the beginning of Neon than the o is. This is because it's the second n that's used in the symbol En, since the second letter in the symbol must appear after the first.

When the Council receives a new element to add to the table, it chooses the most preferred valid symbol for that element that's not already taken by another element. For instance, if Chlorine were the first element added, then it would get the symbol Ch. If Chromium was added later, it would get the symbol Cr. If Cesium and Cerium were then added, they would get the symbols Ce and Ci. If there are no valid symbols for the new element.... well, that's why the Council needs you.

Details and Examples

The Council has decided to wipe the table clean and start afresh. The list of all 366 elements known to Splurthians are set to be assigned a symbol, one by one, in the order in that text file, following the preference rules above.

Determine the symbol assigned to each element in the list. For instance, you should find that Protactinium is assignedPt, Californium is assigned Cf, and Lionium is assigned Iu.

Find the first element that will not be able to have a symbol assigned, because when you get to it all the valid symbols for it are taken. (You can stop assigning symbols at this point if you like.) Post this element along with your solution.

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

clean code,code challenge

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}