Skip to content

Instantly share code, notes, and snippets.

@shravanasati
Created May 31, 2024 13:16
Show Gist options
  • Save shravanasati/b45938a9cae5dd0b0695bf5b9b974233 to your computer and use it in GitHub Desktop.
Save shravanasati/b45938a9cae5dd0b0695bf5b9b974233 to your computer and use it in GitHub Desktop.
check if a sentence can be written using only elements from the periodic table
interface PeriodicTableElement {
no: number,
symbol: string,
name: string
}
const elements: PeriodicTableElement[] = [
{ no: 1, symbol: "H", name: "Hydrogen" },
{ no: 2, symbol: "He", name: "Helium" },
{ no: 3, symbol: "Li", name: "Lithium" },
{ no: 4, symbol: "Be", name: "Beryllium" },
{ no: 5, symbol: "B", name: "Boron" },
{ no: 6, symbol: "C", name: "Carbon" },
{ no: 7, symbol: "N", name: "Nitrogen" },
{ no: 8, symbol: "O", name: "Oxygen" },
{ no: 9, symbol: "F", name: "Fluorine" },
{ no: 10, symbol: "Ne", name: "Neon" },
{ no: 11, symbol: "Na", name: "Sodium" },
{ no: 12, symbol: "Mg", name: "Magnesium" },
{ no: 13, symbol: "Al", name: "Aluminium" },
{ no: 14, symbol: "Si", name: "Silicon" },
{ no: 15, symbol: "P", name: "Phosphorus" },
{ no: 16, symbol: "S", name: "Sulfur" },
{ no: 17, symbol: "Cl", name: "Chlorine" },
{ no: 18, symbol: "Ar", name: "Argon" },
{ no: 19, symbol: "K", name: "Potassium" },
{ no: 20, symbol: "Ca", name: "Calcium" },
{ no: 21, symbol: "Sc", name: "Scandium" },
{ no: 22, symbol: "Ti", name: "Titanium" },
{ no: 23, symbol: "V", name: "Vanadium" },
{ no: 24, symbol: "Cr", name: "Chromium" },
{ no: 25, symbol: "Mn", name: "Manganese" },
{ no: 26, symbol: "Fe", name: "Iron" },
{ no: 27, symbol: "Co", name: "Cobalt" },
{ no: 28, symbol: "Ni", name: "Nickel" },
{ no: 29, symbol: "Cu", name: "Copper" },
{ no: 30, symbol: "Zn", name: "Zinc" },
{ no: 31, symbol: "Ga", name: "Gallium" },
{ no: 32, symbol: "Ge", name: "Germanium" },
{ no: 33, symbol: "As", name: "Arsenic" },
{ no: 34, symbol: "Se", name: "Selenium" },
{ no: 35, symbol: "Br", name: "Bromine" },
{ no: 36, symbol: "Kr", name: "Krypton" },
{ no: 37, symbol: "Rb", name: "Rubidium" },
{ no: 38, symbol: "Sr", name: "Strontium" },
{ no: 39, symbol: "Y", name: "Yttrium" },
{ no: 40, symbol: "Zr", name: "Zirconium" },
{ no: 41, symbol: "Nb", name: "Niobium" },
{ no: 42, symbol: "Mo", name: "Molybdenum" },
{ no: 43, symbol: "Tc", name: "Technetium" },
{ no: 44, symbol: "Ru", name: "Ruthenium" },
{ no: 45, symbol: "Rh", name: "Rhodium" },
{ no: 46, symbol: "Pd", name: "Palladium" },
{ no: 47, symbol: "Ag", name: "Silver" },
{ no: 48, symbol: "Cd", name: "Cadmium" },
{ no: 49, symbol: "In", name: "Indium" },
{ no: 50, symbol: "Sn", name: "Tin" },
{ no: 51, symbol: "Sb", name: "Antimony" },
{ no: 52, symbol: "Te", name: "Tellurium" },
{ no: 53, symbol: "I", name: "Iodine" },
{ no: 54, symbol: "Xe", name: "Xenon" },
{ no: 55, symbol: "Cs", name: "Caesium" },
{ no: 56, symbol: "Ba", name: "Barium" },
{ no: 57, symbol: "La", name: "Lanthanum" },
{ no: 58, symbol: "Ce", name: "Cerium" },
{ no: 59, symbol: "Pr", name: "Praseodymium" },
{ no: 60, symbol: "Nd", name: "Neodymium" },
{ no: 61, symbol: "Pm", name: "Promethium" },
{ no: 62, symbol: "Sm", name: "Samarium" },
{ no: 63, symbol: "Eu", name: "Europium" },
{ no: 64, symbol: "Gd", name: "Gadolinium" },
{ no: 65, symbol: "Tb", name: "Terbium" },
{ no: 66, symbol: "Dy", name: "Dysprosium" },
{ no: 67, symbol: "Ho", name: "Holmium" },
{ no: 68, symbol: "Er", name: "Erbium" },
{ no: 69, symbol: "Tm", name: "Thulium" },
{ no: 70, symbol: "Yb", name: "Ytterbium" },
{ no: 71, symbol: "Lu", name: "Lutetium" },
{ no: 72, symbol: "Hf", name: "Hafnium" },
{ no: 73, symbol: "Ta", name: "Tantalum" },
{ no: 74, symbol: "W", name: "Tungsten" },
{ no: 75, symbol: "Re", name: "Rhenium" },
{ no: 76, symbol: "Os", name: "Osmium" },
{ no: 77, symbol: "Ir", name: "Iridium" },
{ no: 78, symbol: "Pt", name: "Platinum" },
{ no: 79, symbol: "Au", name: "Gold" },
{ no: 80, symbol: "Hg", name: "Mercury" },
{ no: 81, symbol: "Tl", name: "Thallium" },
{ no: 82, symbol: "Pb", name: "Lead" },
{ no: 83, symbol: "Bi", name: "Bismuth" },
{ no: 84, symbol: "Po", name: "Polonium" },
{ no: 85, symbol: "At", name: "Astatine" },
{ no: 86, symbol: "Rn", name: "Radon" },
{ no: 87, symbol: "Fr", name: "Francium" },
{ no: 88, symbol: "Ra", name: "Radium" },
{ no: 89, symbol: "Ac", name: "Actinium" },
{ no: 90, symbol: "Th", name: "Thorium" },
{ no: 91, symbol: "Pa", name: "Protactinium" },
{ no: 92, symbol: "U", name: "Uranium" },
{ no: 93, symbol: "Np", name: "Neptunium" },
{ no: 94, symbol: "Pu", name: "Plutonium" },
{ no: 95, symbol: "Am", name: "Americium" },
{ no: 96, symbol: "Cm", name: "Curium" },
{ no: 97, symbol: "Bk", name: "Berkelium" },
{ no: 98, symbol: "Cf", name: "Californium" },
{ no: 99, symbol: "Es", name: "Einsteinium" },
{ no: 100, symbol: "Fm", name: "Fermium" },
{ no: 101, symbol: "Md", name: "Mendelevium" },
{ no: 102, symbol: "No", name: "Nobelium" },
{ no: 103, symbol: "Lr", name: "Lawrencium" },
{ no: 104, symbol: "Rf", name: "Rutherfordium" },
{ no: 105, symbol: "Db", name: "Dubnium" },
{ no: 106, symbol: "Sg", name: "Seaborgium" },
{ no: 107, symbol: "Bh", name: "Bohrium" },
{ no: 108, symbol: "Hs", name: "Hassium" },
{ no: 109, symbol: "Mt", name: "Meitnerium" },
{ no: 110, symbol: "Ds", name: "Darmstadtium" },
{ no: 111, symbol: "Rg", name: "Roentgenium" },
{ no: 112, symbol: "Cn", name: "Copernicium" },
{ no: 113, symbol: "Nh", name: "Nihonium" },
{ no: 114, symbol: "Fl", name: "Flerovium" },
{ no: 115, symbol: "Mc", name: "Moscovium" },
{ no: 116, symbol: "Lv", name: "Livermorium" },
{ no: 117, symbol: "Ts", name: "Tennessine" },
{ no: 118, symbol: "Og", name: "Oganesson" },
]
const symbolToObject = elements.reduce((acc: { [x: string]: [y: number, z: string]; }, element: { symbol: { toLowerCase: () => string | number; }; no: number; name: string; }) => {
acc[element.symbol.toLowerCase()] = [element.no, element.name];
return acc;
}, {});
function capitalize(s: string): string {
return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase()
}
let sentenceOriginal = ""
if (Bun.argv.length > 2) {
sentenceOriginal = Bun.argv.slice(2).join(" ")
} else {
for await (const chunk of Bun.stdin.stream()) {
const chunkText = Buffer.from(chunk).toString()
sentenceOriginal += chunkText
}
}
if (sentenceOriginal.length == 0) {
console.log("error: no arguments passed")
process.exit(1)
}
let sentence = sentenceOriginal.trim().toLowerCase().replaceAll(" ", "")
// console.log(sentence)
let tokens: string[] = []
let i = 0;
let cantSpell = false;
while (i < sentence.length && !cantSpell) {
for (let offset = 2; offset > 0; offset--) {
let token = sentence.slice(i, i + offset);
if (symbolToObject[token]) {
i += offset;
tokens.push(token);
break;
} else if (offset == 1) {
console.log("can't be spelled :(")
cantSpell = true;
break;
}
}
}
if (!cantSpell) {
console.log(`congrats, the sentence "${sentenceOriginal}" can be spelled using elements from the periodic table`)
console.log(tokens.map(capitalize).join(" "))
console.log(tokens.map((e) => symbolToObject[e][1]).join(" "))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment