def arr_from_words_at_loc word_loc
words_file = File.new(word_loc, "r")
words_string = words_file.gets
words_file.close
words_arr = words_string.split ","
formatted_words_arr = words_arr.collect{ |word| word[1...-1] }
words_arr = nil
formatted_words_arr
end
def is_triangle_num n
return true if ((8*n+1)**0.5)%1 == 0
return false
end
def value_of_char char
char-64
end
def convert_word_to_num word
word.each_char.inject(0) { |wordnum, char| wordnum + value_of_char(char[0])}
end
def num_of_tri_words_in_arr arr
arr.inject(0) { |num, currword|
if is_triangle_num(convert_word_to_num(currword))
num += 1
else
num
end
}
end
words = arr_from_words_at_loc "words.txt"
num_of_tri_words = num_of_tri_words_in_arr words
p num_of_tri_words