{ "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 2" ] }, { "cell_type": "markdown", "id": "172ddfe7-9928-449c-a891-e51aed256876", "metadata": {}, "source": [ "#### Our initial insight: We only have to translate 3-digit numbers. We simply append the words \"million\" or \"thousand\" or nothing at all, as necessary. So let's modify our function `translate()` to break apart a number into 3-digit triplet values." ] }, { "cell_type": "markdown", "id": "a54ce9b2-4e8d-4f39-94cd-949d5feb67a9", "metadata": {}, "source": [ "## Example: Break apart a large number `n = 123_456_789`" ] }, { "attachments": {}, "cell_type": "markdown", "id": "8f9594ae-af77-4892-a77f-bd3d0a03eaa1", "metadata": {}, "source": [ "#### First, extract the first triplet 123 representing millions:\n", "- #### 123_456_789 // 1_000_000 ==> 123\n", "- #### Therefore, `millions = n//1_000_000`\n", "#### Next, extract the second triplet 456 representing thousands:\n", "- #### 123_456_789 % 1_000_000 ==> 456_789 (remainder)\n", "- #### 456_789 // 1000 ==> 456\n", "- #### Therefore, `remainder = n % 1_000_000` and `thousands` = remainder // 1000`\n", "#### Finally, extract the third triplet 789 representing hundreds:\n", "- #### 456_789 % 100 ==> 789\n", "- #### Therefore, `hundreds = remainder % 100`" ] }, { "cell_type": "markdown", "id": "2670f06c-7cd7-478d-83de-16e71d174e79", "metadata": {}, "source": [ "### We don't yet know how to translate a triplet, so for now we'll write a placeholder function `translate_triplet()` that simply returns the triplet value as a string." ] }, { "cell_type": "code", "execution_count": null, "id": "450864a7-1c17-486d-bfbd-fdc6b7633515", "metadata": {}, "outputs": [], "source": [ "def translate_triplet(triplet):\n", " \"\"\"\n", " Placeholder: Just return the triplet value.\n", " \"\"\"\n", " return str(triplet)" ] }, { "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 }