{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "ab326dc1-92ee-4f11-95c9-ec0ab582fb5a", "metadata": {}, "outputs": [], "source": [ "def break_up_number(n):\n", " \"\"\"\n", " Break up a number n into three triplets\n", " representing millions, thousands, and hundreds\n", " and return the triplet values.\n", " \"\"\"\n", " millions = n//1_000_000\n", " remainder = n%1_000_000\n", " \n", " thousands = remainder//1000\n", " hundreds = remainder%1000\n", " \n", " return millions, thousands, hundreds" ] }, { "cell_type": "code", "execution_count": 2, "id": "3a375371-9411-4a0c-aa94-5bf51830efea", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "m = 0, t = 0, h = 123\n" ] } ], "source": [ "m, t, h = break_up_number(123)\n", "print(f'{m = }, {t = }, {h = }')" ] }, { "cell_type": "code", "execution_count": 3, "id": "d0d777b1-1614-4951-9a7f-9fce1af90e78", "metadata": {}, "outputs": [], "source": [ "def break_up_triplet(n):\n", " \"\"\"\n", " Break up a triplet value n into \n", " the hundreds, tens, and ones digits\n", " and return the digit values.\n", " \"\"\"\n", " hundreds_digit = n//100\n", " remainder = n%100\n", " \n", " tens_digit = remainder//10\n", " ones_digit = remainder%10\n", " \n", " return hundreds_digit, tens_digit, ones_digit" ] }, { "cell_type": "code", "execution_count": 4, "id": "d93d5409-b1ae-4cb5-af20-6834ea2cfff3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hd = 1, td = 2, od = 3\n" ] } ], "source": [ "hd, td, od = break_up_triplet(123)\n", "print(f'{hd = }, {td = }, {od = }')" ] }, { "cell_type": "code", "execution_count": 5, "id": "9c50b730-d081-485e-963f-73567aaba4e7", "metadata": {}, "outputs": [], "source": [ "def translate_ones_digit(digit):\n", " \"\"\"\n", " Return the translated ones digit.\n", " \"\"\"\n", " if digit == 1:\n", " return 'one'\n", " elif digit == 2:\n", " return 'two'\n", " elif digit == 3:\n", " return 'three'\n", " elif digit == 4:\n", " return 'four'\n", " elif digit == 5:\n", " return 'five'\n", " elif digit == 6:\n", " return 'six'\n", " elif digit == 7:\n", " return 'seven'\n", " elif digit == 8:\n", " return 'eight'\n", " else:\n", " return 'nine'" ] }, { "cell_type": "code", "execution_count": 6, "id": "cc8e1411-28ff-45a1-a98c-5fb50949ab2d", "metadata": {}, "outputs": [], "source": [ "def translate_teens(digit):\n", " \"\"\"\n", " Return the translated teens digit.\n", " \"\"\"\n", " if digit == 0:\n", " return 'ten'\n", " elif digit == 1:\n", " return 'eleven'\n", " elif digit == 2:\n", " return 'twelve'\n", " elif digit == 3:\n", " return 'thirteen'\n", " elif digit == 4:\n", " return 'fourteen'\n", " elif digit == 5:\n", " return 'fifteen'\n", " elif digit == 6:\n", " return 'sixtern'\n", " elif digit == 7:\n", " return 'seventeen'\n", " elif digit == 8:\n", " return 'eighteen'\n", " elif digit == 9:\n", " return 'nineteen'" ] }, { "cell_type": "code", "execution_count": 7, "id": "2a9dd8a7-eb93-4d8f-8776-43322808e06d", "metadata": {}, "outputs": [], "source": [ "def translate_tens_digit(digit):\n", " \"\"\"\n", " Return the translated tens digit.\n", " \"\"\"\n", " if digit == 2:\n", " return 'twenty' \n", " elif digit == 3:\n", " return 'thirty' \n", " elif digit == 4:\n", " return 'forty' \n", " elif digit == 5:\n", " return 'fifty' \n", " elif digit == 6:\n", " return 'sixty' \n", " elif digit == 7:\n", " return 'seventy' \n", " elif digit == 8:\n", " return 'eighty' \n", " else:\n", " return 'ninety' " ] }, { "cell_type": "code", "execution_count": 8, "id": "3e35a6a9-32a1-41fb-b20c-c1b64cf7a8c5", "metadata": {}, "outputs": [], "source": [ "def translate_pair(tens_digit, ones_digit):\n", " \"\"\"\n", " Return the translation of a pair digits:\n", " a tens digit and a ones digit.\n", " \"\"\"\n", " if (tens_digit == 0) and (ones_digit > 0):\n", " str = translate_ones_digit(ones_digit)\n", " elif tens_digit == 1:\n", " str = translate_teens(ones_digit)\n", " else:\n", " str = translate_tens_digit(tens_digit)\n", " \n", " # Append a hyphen and the ones word.\n", " if ones_digit > 0:\n", " str += '-' + translate_ones_digit(ones_digit)\n", " \n", " return str" ] }, { "cell_type": "code", "execution_count": 9, "id": "06a622bd-1da5-40db-854e-c3792ca82426", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'twenty-two'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "translate_pair(2, 2)" ] }, { "cell_type": "code", "execution_count": 10, "id": "a1c8f5f5-c64b-4a4f-ae9c-2a9969c41f58", "metadata": {}, "outputs": [], "source": [ "def translate_triplet(triplet):\n", " \"\"\"\n", " Return the translation of a triplet.\n", " \"\"\"\n", " str = '';\n", " hundreds_digit, tens_digit, ones_digit = break_up_triplet(triplet)\n", " \n", " if hundreds_digit > 0:\n", " str = translate_ones_digit(hundreds_digit) + ' hundred'\n", " \n", " # Append a space only if there is something else.\n", " if (tens_digit > 0) or (ones_digit > 0):\n", " str += ' '\n", " \n", " # If there is a pair, append their translation.\n", " if (tens_digit != 0) or (ones_digit != 0):\n", " str += translate_pair(tens_digit, ones_digit)\n", " \n", " return str" ] }, { "cell_type": "code", "execution_count": 11, "id": "679865a1-a2a9-4c3f-851d-ad12b293fcde", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one hundred eleven'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "translate_triplet(111)" ] }, { "cell_type": "code", "execution_count": 12, "id": "e59bd9d9-9028-49f0-a422-4d63ed661306", "metadata": {}, "outputs": [], "source": [ "def translate(n):\n", " \"\"\"b\n", " Return the translation of a number.\n", " \"\"\"\n", " str = '';\n", " millions, thousands, hundreds = break_up_number(n)\n", " \n", " if millions > 0:\n", " str += translate_triplet(millions) + ' million'\n", " if thousands > 0:\n", " if millions > 0:\n", " str += ' '\n", " str += translate_triplet(thousands) + ' thousand'\n", " if hundreds > 0:\n", " if (millions > 0) or (thousands > 0):\n", " str += ' '\n", " str += translate_triplet(hundreds)\n", " \n", " return str;" ] }, { "cell_type": "code", "execution_count": 13, "id": "82c2ecca-f3d8-4884-96ff-eed4606ec46c", "metadata": {}, "outputs": [], "source": [ "def prompt_for_number():\n", " \"\"\"\n", " Prompt the user for a number and return its value.\n", " \"\"\"\n", " good_number = False\n", " \n", " while not good_number:\n", " number = int(input('Number to translate? '))\n", " \n", " good_number = (0 <= number <= 999_999_999)\n", " if not good_number:\n", " print('Number must be between 1 and 999,999,999 inclusive. 0 to stop.')\n", " \n", " return number" ] }, { "cell_type": "code", "execution_count": 14, "id": "e18b4953-6ba4-4c22-8b14-d43a8c0aadd0", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Number to translate? 10\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "ten\n", "\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "Number to translate? 0\n" ] } ], "source": [ "n = prompt_for_number()\n", "\n", "# Loop to prompt for and translate numbers\n", "# until the user enters 0.\n", "while n > 0:\n", " print(translate(n))\n", " print()\n", " \n", " n = prompt_for_number()" ] }, { "cell_type": "code", "execution_count": null, "id": "c0df876a-2e96-49a2-a981-f50c02f50475", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }