Java Code Challenge is a new 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!
This is a beefier 2-part challenge and may take a bit longer to do- I'm really excited to see what solutions you come up with!
The inhabitants of the planet Splurth are building their own periodic table of the elements. Just like Earth's periodic table has a chemical symbol for each element (
H for Hydrogen,
Li for Lithium, etc.), so does Splurth's. However, their chemical symbols must follow certain rules:
- All chemical symbols must be exactly two letters, so
Bis not a valid symbol for Boron.
- Both letters in the symbol must appear in the element name, but the first letter of the element name does not necessarily need to appear in the symbol. So
Hgis not valid for Mercury, but
- The two letters must appear in order in the element name. So
Vris valid for Silver, but
Rvis not. To be clear, both
Amare valid for Magnesium, because there is both an
athat appears after an
m, and an
mthat appears after an
- If the two letters in the symbol are the same, it must appear twice in the element name. So
Nnis valid for Xenon, but
As a member of the Splurth Council of Atoms and Atom-Related Paraphernalia, you must determine whether a proposed chemical symbol fits these rules.
Write a function that, given two strings, one an element name and one a proposed symbol for that element, determines whether the symbol follows the rules. If you like, you may parse the program's input and output the result, but this is not necessary.
The symbol will have exactly two letters. Both element name and symbol will contain only the letters a-z. Both the element name and the symbol will have their first letter capitalized, with the rest lowercase. (If you find that too challenging, it's okay to instead assume that both will be completely lowercase.)
Spenglerium, Ee -> true Zeddemorium, Zr -> true Venkmine, Kn -> true Stantzon, Zt -> false Melintzum, Nn -> false Tullium, Ty -> false
Optional Bonus Challenges
- Given an element name, find the valid symbol for that name that's first in alphabetical order. E.g.
Gozerium -> Ei,
Slimyrine -> Ie.
- Given an element name, find the number of distinct valid symbols for that name. E.g.
Zuulon -> 11.