Skip to content

Instantly share code, notes, and snippets.

@jedwardsol
Last active November 17, 2021 16:09
Show Gist options
  • Save jedwardsol/0ba076db6babf22fb27914ad763dd0ca to your computer and use it in GitHub Desktop.
Save jedwardsol/0ba076db6babf22fb27914ad763dd0ca to your computer and use it in GitHub Desktop.
Word to elements
#include <iostream>
#include <format>
#include <vector>
#include <sstream>
#include <string_view>
#include <set>
#include <map>
extern std::map<std::string_view,std::string_view> elementNames;
const std::set<std::string_view> periodicTable
{
"h", "he",
"li","be", "b", "c", "n", "o", "f", "ne",
"na","mg", "al","si","p", "s", "cl","ar",
"k", "ca","sc","ti","v", "cr","mn","fe","co","ni","cu","zn","ga","ge","as","se","br","kr",
"rb","sr","y", "zr","nb","mo","tc","ru","rh","pd","ag","cd","in","sn","sb","te","i", "xe",
"cs","ba", "hf","ta","w", "re","os","ir","pt","au","hg","tl","pb","bi","po","at","rn",
"fr","ra", "rf","db","sg","bh","hs","mt","ds","rg","cn","nh","fl","mc","lv","ts","og",
"la","ce","pr","nd","pm","sm","eu","gd","tb","dy","ho","er","tm","yb","lu",
"ac","th","pa","u", "np","pu","am","cm","bk","cf","es","fm","md","no","lr",
};
bool go(std::string_view word, std::vector<std::string_view> &result)
{
if(word.empty())
{
return true;
}
for(size_t size : {2,1})
{
if(word.size() >= size)
{
auto symbol = word.substr(0,size);
if(periodicTable.contains(symbol))
{
result.push_back(symbol);
auto remainder = word.substr(size);
if(go(remainder,result))
{
return true;
}
else
{
result.pop_back();
}
}
}
}
return false;
}
auto format(auto &result)
{
std::ostringstream symbols;
std::ostringstream names;
for(auto const &s : result)
{
symbols << s << ' ';
names << elementNames[s] << ' ';
}
return std::format("{:30} {}",symbols.str(), names.str());
}
int main(int argc, char *argv[])
{
std::vector<std::string_view> args(argv+1, argv+argc);
if(!args.empty())
{
for(auto word : args)
{
std::vector<std::string_view> result;
std::cout << std::format("{:<20} : ",word);
if(go(word,result))
{
std::cout << format(result);
}
std::cout << '\n';
}
}
else
{
for(auto [symbol,name] : elementNames)
{
std::vector<std::string_view> result;
if(go(name,result))
{
std::cout << std::format("{:<20} : {}\n",name, format(result));
}
}
}
}
std::map<std::string_view,std::string_view> elementNames
{
{"ac","actinium"},
{"ag","silver"},
{"al","aluminium"},
{"am","americium"},
{"ar","argon"},
{"as","arsenic"},
{"at","astatine"},
{"au","gold"},
{"b", "boron"},
{"ba","barium"},
{"be","beryllium"},
{"bh","bohrium"},
{"bi","bismuth"},
{"bk","berkelium"},
{"br","bromine"},
{"c", "carbon"},
{"ca","calcium"},
{"cd","cadmium"},
{"ce","cerium"},
{"cf","californium"},
{"cl","chlorine"},
{"cm","curium"},
{"cn","copernicium"},
{"co","cobalt"},
{"cr","chromium"},
{"cs","cesium"},
{"cu","copper"},
{"ds","darmstadtium"},
{"db","dubnium"},
{"dy","dysprosium"},
{"er","erbium"},
{"es","einsteinium"},
{"eu","europium"},
{"f", "fluorine"},
{"fe","iron"},
{"fl","flerovium"},
{"fm","fermium"},
{"fr","francium"},
{"ga","gallium"},
{"gd","gadolinium"},
{"ge","germanium"},
{"h", "hydrogen"},
{"he","helium"},
{"hf","hafnium"},
{"hg","mercury"},
{"ho","holmium"},
{"hs","hassium"},
{"i", "iodine"},
{"in","indium"},
{"ir","iridium"},
{"k", "potassium"},
{"kr","krypton"},
{"la","lanthanum"},
{"li","lithium"},
{"lr","lawrencium"},
{"lu","lutetium"},
{"lv","livermorium"},
{"mc","moscovium"},
{"md","mendelevium"},
{"mg","magnesium"},
{"mn","manganese"},
{"mo","molybdenum"},
{"mt","meitnerium"},
{"n", "nitrogen"},
{"na","sodium"},
{"nb","niobium"},
{"nd","neodymium"},
{"ne","neon"},
{"nh","nihonium"},
{"ni","nickel"},
{"no","nobelium"},
{"np","neptunium"},
{"o", "oxygen"},
{"og","oganesson"},
{"os","osmium"},
{"p", "phosphorus"},
{"pa","protactinium"},
{"pb","lead"},
{"pd","palladium"},
{"pm","promethium"},
{"po","polonium"},
{"pr","praseodymium"},
{"pt","platinum"},
{"pu","plutonium"},
{"ra","radium"},
{"rb","rubidium"},
{"re","rhenium"},
{"rf","rutherfordium"},
{"rg","roentgenium"},
{"rh","rhodium"},
{"rh","rhodium"},
{"rn","radon"},
{"ru","ruthenium"},
{"s", "sulphur"},
{"sb","antimony"},
{"sc","scandium"},
{"se","selenium"},
{"sg","seaborgium"},
{"si","silicon"},
{"sm","samarium"},
{"sn","tin"},
{"sr","strontium"},
{"ta","tantalum"},
{"tb","terbium"},
{"tc","technetium"},
{"te","tellurium"},
{"th","thorium"},
{"ti","titanium"},
{"tl","thallium"},
{"tm","thulium"},
{"ts","tennessine"},
{"u", "uranium"},
{"v", "vanadium"},
{"w", "tungsten"},
{"xe","xenon"},
{"y", "yttrium"},
{"yb","ytterbium"},
{"zn","zinc"},
{"zr","zirconium"},
};
void crossCheck()
{
for(auto const &element : periodicTable)
{
if(!elementNames.contains(element))
{
__debugbreak();
}
}
for(auto const&element : elementNames)
{
if(!periodicTable.contains(element.first))
{
__debugbreak();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment