=begin If the numbers 1 to 5 are written out in words: one, two, three, four, five; there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. =end upbound = 1000 words = {1=>'one',2=>'two',3=>'three',4=>'four',5=>'five',6=>'six',7=>'seven',8=>'eight',9=>'nine',10=>'ten',11=>'eleven',12=>'twelve',13=>'thirteen',14=>'fourteen',15=>'fifteen',16=>'sixteen',17=>'seventeen',18=>'eighteen',19=>'nineteen',0=>''} tenwords = {2=>'twenty',3=>'thirty',4=>'forty',5=>'fifty',6=>'sixty',7=>'seventy',8=>'eighty',9=>'ninety'} sum = 0 1.upto upbound do |n| word = '' #lbotest n = n.to_s skip = false if n.length > 2 && !n.match(/^[123456789]{1}[0]+$/) word += 'and' #lbotest end 1.upto n.length do |i| if skip skip = false next end if i == 1 if n.length >= 2 tens = n[-2,2].to_i if !words[tens].nil? word += words[tens] #lbotest skip = true next end end word += words[n[-1,1].to_i] #lbotest elsif i == 2 word += tenwords[n[-2,1].to_i] #lbotest elsif i == 3 && n[-3,1] != '0' word += words[n[-3,1].to_i] #lbotest word += 'hundred' #lbotest elsif i == 4 word += words[n[-4,1].to_i] #lbotest word += 'thousand' #lbotest end end sum += word.length print(word+' '+word.length.to_s+"\n") end print(sum)