Skip to content

Instantly share code, notes, and snippets.

@mh-github
Created March 31, 2013 18:30
Show Gist options
  • Select an option

  • Save mh-github/5281532 to your computer and use it in GitHub Desktop.

Select an option

Save mh-github/5281532 to your computer and use it in GitHub Desktop.
Ruby program to find if a word is made up of periodic table elements' symbols.
#!/usr/bin/ruby
periodic_elements = ["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", "la", "ce", "pr", "nd", "pm", "sm", "eu", "gd", "tb", "dy", "ho", "er", "tm", "yb", "lu", "hf", "ta", "w", "re", "os", "ir", "pt", "au", "hg", "tl", "pb", "bi", "po", "at", "rn", "fr", "ra", "ac", "th", "pa", "u", "np", "pu", "am", "cm", "bk", "cf", "es", "fm", "md", "no", "lr", "unq", "unp", "unh", "uns", "uno", "une", "unn"]
$found_array = []
$solution_array = []
headless_word_arr = []
$word = ARGV[0]
=begin
function check_solution, param = string
if string is the word, we're done
else
loop through solution array, appending string to each element
if we get the word, we're done
if we get the first substring, add the substring to the solution array
=end
def check_solution(str)
puts "found #{str}"
$found_array << str
print "found elements = "
$found_array.each {|x| print x, " "}
puts
if str == $word then
puts "Success. Word is composed of periodic table elements"
exit(0)
end
for i in (0 .. $solution_array.length-1) do
partial = $solution_array[i] + str
if partial == $word then
$solution_array << partial
puts "Solution set so far..."
$solution_array.each {|x| print x, " "}
puts
puts "------------------"
puts "Success. Word is composed of periodic table elements"
exit(0)
end
if $word.index(partial) == 0 then
$solution_array << partial
end
end
puts "Solution set so far..."
$solution_array.each {|x| print x, " "}
puts
puts "------------------"
end
puts "Processing #{$word}"
puts "------------------"
$word = $word.downcase
=begin
take first character in word
if character is a periodic_element
add character to solution array
else if word has more than one character and
first two characters are a periodic_element
add first two characters to solution array
check solution
if first character is u
if first three characters are a periodic_element
add first three characters to solution array
=end
found = false
first_character = $word[0, 1]
if periodic_elements.include?(first_character) then
found = true
$solution_array << first_character
check_solution(first_character)
end
if $word.length > 1 then
first_two_characters = $word[0, 2]
if periodic_elements.include?(first_two_characters) then
found = true
$solution_array << first_two_characters
check_solution(first_two_characters)
end
end
if first_character == "u" then
first_three_characters = $word[0, 3]
if periodic_elements.include?(first_three_characters) then
found = true
$solution_array << first_three_characters
end
end
if found == false then
puts "Given word is NOT made up of periodic table elements"
exit
end
=begin
for each character in word from second position onwards
if character is 'u'
if u and two successive characters are a periodic element
check solution with u and two successive characters
if character is a periodic_element
check solution with character
if character + next character is a periodic_element
check solution with character + next character
=end
headless_word = $word.slice(1, $word.length)
headless_word.scan(/./) {|c| headless_word_arr << c}
for i in (0 .. headless_word_arr.length-1) do
if headless_word_arr[i] == "u" then
if i < headless_word_arr.length-2 then
if periodic_elements.include?(
headless_word_arr[i] +
headless_word_arr[i+1] +
headless_word_arr[i+2]) then
check_solution(headless_word_arr[i] +
headless_word_arr[i+1] +
headless_word_arr[i+2])
end
end
end
if periodic_elements.include?(headless_word_arr[i]) then
check_solution(headless_word_arr[i])
end
if i < headless_word_arr.length-1 &&
periodic_elements.include?(headless_word_arr[i] + headless_word_arr[i+1]) then
check_solution(headless_word_arr[i] + headless_word_arr[i+1])
end
end
# If we have reached here, can't make the word
puts "Given word is NOT made up of periodic table elements"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment