{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21124" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "special_cases = {1: \"one\", 2: \"two\", 3: \"three\", 4: \"four\", 5: \"five\", 6: \"six\", 7: \"seven\", 8: \"eight\", 9: \"nine\",\n", " 10: \"ten\", 11: \"eleven\", 12: \"twelve\", 13: \"thirteen\", 15: \"fifteen\", 18: \"eighteen\",\n", " 20: \"twenty\", 30: \"thirty\", 40: \"forty\", 50: \"fifty\", 60: \"sixty\", 70: \"seventy\", 80: \"eighty\",\n", " 90: \"ninety\"}\n", "\n", "\n", "def number_to_words(n):\n", " assert n <= 1000\n", " if special_cases.get(n):\n", " return special_cases[n]\n", " if 14 <= n <= 19:\n", " return special_cases[n % 10] + \"teen\"\n", " if 20 <= n <= 99:\n", " return special_cases[n // 10 * 10] + \"-\" + special_cases[n % 10]\n", " if 100 <= n <= 999:\n", " if n % 100 == 0:\n", " return special_cases[n // 100] + \" \" + \"hundred\"\n", " return special_cases[n // 100] + \" \" + \"hundred and \" + number_to_words(n % 100)\n", " if n == 1000:\n", " return \"one thousand\"\n", "\n", "\n", "all_numbers = \"\"\n", "for i in range(1, 1000+1):\n", " all_numbers += number_to_words(i)\n", "all_numbers = all_numbers.replace('-', '')\n", "all_numbers = all_numbers.replace(' ', '')\n", "len(all_numbers)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" }, "title": "Number letter counts" }, "nbformat": 4, "nbformat_minor": 2 }