{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "fd42952f-f89e-4660-86b9-7c753c1aa6b8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "632b1bb8-4d5a-48ed-b606-798563f7288f", "metadata": {}, "source": [ "###
San Jose State University
Dep|artment of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "id": "a032b427-fc88-4e31-a531-e678fa24d61e", "metadata": {}, "source": [ "# Number Translator, version 3" ] }, { "cell_type": "markdown", "id": "172ddfe7-9928-449c-a891-e51aed256876", "metadata": {}, "source": [ "#### We now begin to translate a triplet like 123. If the hundreds digit is nonzero, we can translate it into one of the words \"one\", \"two\", ... \"nine\" with a new function `translate_digit()` that returns the correct word, to which we append the word \"hundred\"." ] }, { "cell_type": "markdown", "id": "a54ce9b2-4e8d-4f39-94cd-949d5feb67a9", "metadata": {}, "source": [ "## Example: Break apart a triplet `triplet = 123`" ] }, { "attachments": {}, "cell_type": "markdown", "id": "8f9594ae-af77-4892-a77f-bd3d0a03eaa1", "metadata": {}, "source": [ "#### First, extract the first hundreds digit:\n", "- #### 123 // 100 ==> 1\n", "- #### Therefore, `hundreds_digit = triplet // 100`\n", "#### Then the remaining pair:\n", "- #### 123 % 100 ==> 23 (remainder)\n", "- #### Therefore, `pair = triplet % 100`" ] }, { "cell_type": "markdown", "id": "2670f06c-7cd7-478d-83de-16e71d174e79", "metadata": {}, "source": [ "### We don't yet know how to translate a pair, so for now we'll write a placeholder function `translate_pair()` that simply returns the ppair value as a string." ] }, { "cell_type": "code", "execution_count": null, "id": "5100eb3b-9d04-4b36-b4b0-6b46db169c1d", "metadata": {}, "outputs": [], "source": [ "def translate_pair(pair):\n", " \"\"\"\n", " Placeholder: Just return the pair value.\n", " \"\"\"\n", " return str(pair)" ] }, { "cell_type": "code", "execution_count": null, "id": "f7474405-0877-45f4-b2fe-e8cf28d33341", "metadata": {}, "outputs": [], "source": [ "def translate_digit(digit):\n", " \"\"\"\n", " Return the translated 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": null, "id": "450864a7-1c17-486d-bfbd-fdc6b7633515", "metadata": {}, "outputs": [], "source": [ "def translate_triplet(triplet):\n", " \"\"\"\n", " Return the translation of a triplet.\n", " \"\"\"\n", " str = '';\n", "\n", " # Break apart triplet into a hundreds digit\n", " # and the remaining pair of digits.\n", " hundreds_digit = triplet//100\n", " pair = triplet % 100\n", " \n", " if hundreds_digit > 0:\n", " str = translate_digit(hundreds_digit) + ' hundred'\n", "\n", " if pair > 0:\n", " str += ' ' + translate_pair(pair)\n", " \n", " return str" ] }, { "cell_type": "code", "execution_count": null, "id": "b47845d5-21b0-4d8d-9cae-bb75fae539d8", "metadata": {}, "outputs": [], "source": [ "def translate(n):\n", " \"\"\"\n", " Return the translation of a number.\n", " \"\"\"\n", " str = '';\n", "\n", " # Break apart n into three triplets.\n", " millions = n//1_000_000\n", " remainder = n%1_000_000\n", " \n", " thousands = remainder//1000\n", " hundreds = remainder%1000\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": null, "id": "a30a991b-0a4d-41d2-ac18-1ed6e700c6bd", "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", " print()\n", " \n", " return number" ] }, { "cell_type": "code", "execution_count": null, "id": "bfa9ba15-716d-4eab-aaaa-2e68528efc5e", "metadata": {}, "outputs": [], "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()\n", "\n", "print()\n", "print('Done!')" ] }, { "cell_type": "code", "execution_count": null, "id": "232826c5-8d25-4d28-84b7-0afc94fb95ae", "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2024 by Ronald Mak" ] }, { "cell_type": "code", "execution_count": null, "id": "cb7a6946-1b44-4aef-b0d5-8df5f9406eec", "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.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }