{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "source": [ "If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.\n", "\n", "If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?\n", "\n", "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. The use of \"and\" when writing out numbers is in compliance with British usage." ] }, { "cell_type": "code", "collapsed": false, "input": [ " def pro_one(num) # 1..19\n", " return \"\" if num == 0\n", " %w(zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen)[num]\n", " end\n", " \n", " pro_one(5)" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 3, "text": [ "\"five\"" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ " def pro_ten(num) # 20..99\n", " return pro_one(num) if num < 20\n", " tens = %w(zero ten twenty thirty forty fifty sixty seventy eighty ninety)\n", " \n", " ten, one = num.to_s.split(\"\").map{|i|i.to_i}\n", " return tens[ten] + \" \" + pro_one(one)\n", " end\n", " \n", " pro_ten(56)" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 4, "text": [ "\"fifty six\"" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "def pro_hundred(num) #100..999\n", " return pro_ten(num) if num < 100\n", " return \"one thousand\" if num == 1000\n", " hun, ten_one = [num.to_s[0].to_i, num.to_s[1..2].to_i]\n", " return pro_one(hun) + \" hundred and \" + pro_ten(ten_one) if ten_one != 0\n", " return pro_one(hun) + \" hundred\"\n", "end\n", "\n", "pro_hundred(430)" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 5, "text": [ "\"four hundred and thirty \"" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "(1..1000).to_a.map{|i| pro_hundred(i).gsub(/[ \\-]/, \"\").length }.inject &:+" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 6, "text": [ "21124" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "pro_hundred(115).gsub(/[ \\-]/, \"\").length" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 7, "text": [ "20" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "outputs": [] } ] } ] }