Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Word2Element will convert a string of text into elements from the periodic table whose symbols make up the string of text. Of course not all strings of text can be made into an elemental representation, so if it's impossible to convert the application will spit out "Not possible"
Element = Struct.new(:symbol, :name)
elements = []
def word2Element(w, builder, elements)
if w.length == 0
return builder
end
# check for 1 char symbol
single = elements.select { |e| e.symbol.upcase == w.split('')[0] }
if single.length > 0
builder.push(single[0])
recursiveCall = word2Element(w[1..-1], builder, elements)
# if it didn't work out, then it needs to fall through to the 2 char symbol check
if recursiveCall != nil
return recursiveCall
else
builder.pop()
end
end
# if there is only one character left and it's not it's not a symbol, then it's impossible
if w.length == 1
return nil
end
# check for 2 char symbol
double = elements.select { |e| e.symbol.upcase == (w.split('')[0] + w.split('')[1]) }
if double.length > 0
builder.push(double[0])
return word2Element(w[2..-1], builder, elements)
end
return nil
end
puts "Welcome to Word2Element! Enter a word to convert."
word = gets.upcase().gsub(/\s+/, "")
# open up a text file with all of the elements listed in the format:
# SYMBOL ELEMENT_NAME
elemFile = File.open("C:\\Users\\Adam\\Desktop\\elements.txt")
# iterate through all of the lines in the file
for line in elemFile.lines()
tokens = line.split(" ")
elements.push(Element.new(tokens[0], tokens[1]))
end
# needed to make this a function since it's recursive
result = word2Element(word, [], elements)
# print out the results
if result == nil
puts "Not possible"
else
puts
for e in result
puts e.symbol + "\t" + e.name
end
end
=begin
Here's the text file used for this script:
Ac Actinium
Ag Silver
Al Aluminum
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
Co Cobalt
Cn Copernicium
Cr Chromium
Cs Cesium
Cu Copper
Db Dubnium
Ds Darmstadtium
Dy Dysprosium
Er Erbium
Es Einsteinium
Eu Europium
F Fluorine
Fe Iron
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
Md Mendelevium
Mg Magnesium
Mn Manganese
Mo Molybdenum
Mt Meitnerium
N Nitrogen
Na Sodium
Nb Niobium
Nd Neodymium
Ne Neon
Ni Nickel
No Nobelium
Np Neptunium
O Oxygen
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
Rn Radon
Ru Ruthenium
S Sulfur
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
U Uranium
V Vanadium
W Tungsten
Xe Xenon
Y Yttrium
Yb Ytterbium
Zn Zinc
Zr Zirconium
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment