My try to solve Project Euler no 22 using Ruby
def get_file_as_string(filename) data = '' f = File.open(filename, "r") f.each_line do |line| data += line end return data end def calc_alpha(alpha) ttl = 0 i = 0 begin ttl += alpha[i].ord - 64 i += 1 end until i == alpha.length return ttl end ##### MAIN ##### names_data = get_file_as_string '~\src\euler22\names.txt' sompret = names_data.gsub('"','') array = sompret.split(/,/) array.sort! running_total = 0 array.each_index do |idx| running_total += calc_alpha(array[idx]) * (idx + 1) end print 'Result:: ' print running_total
No comments:
Post a Comment