{ "cells": [ { "cell_type": "markdown", "id": "56ea5c9e-aa01-4aec-90e4-59ebf71f5bd6", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2023
Instructor: Ron Mak

**Assignment #5.1: Dictionaries**

SAMPLE SOLUTIONS
" ] }, { "cell_type": "markdown", "id": "af818062-9c87-48d6-b943-8f399d2fd007", "metadata": {}, "source": [ "#### **PROBLEM 5.1. a.:** [80 points] Rewrite the number translator program in the Feb. 15 notebook `NumberTranslator.ipynb` using a dictionary containing the number words. For example,\n", "``` python\n", "words = { 1: 'one', 2: 'two' }\n", "```\n", "#### You should be able to replace the `if elif else` statements." ] }, { "cell_type": "code", "execution_count": 1, "id": "84b5622b-c654-4500-94eb-84bf6cacab7f", "metadata": {}, "outputs": [], "source": [ "# Global constant\n", "WORDS = { 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five',\n", " 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine',\n", " 11: 'eleven', 12: 'twelve', 13: 'thirteen',\n", " 14: 'fourteen', 15: 'fifteen', 16: 'sixteen',\n", " 17: 'seventeen', 18: 'eighteen', 19: 'nineteen',\n", " 10: 'ten', 20: 'twenty', 30: 'thirty', 40: 'forty',\n", " 50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty',\n", " 90: 'ninety', 100: 'hundred', 1000: 'thousand',\n", " 1_000_000: 'million'\n", " }" ] }, { "cell_type": "code", "execution_count": 2, "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": 3, "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": 4, "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": 5, "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": 6, "id": "9c50b730-d081-485e-963f-73567aaba4e7", "metadata": {}, "outputs": [], "source": [ "def translate_ones_digit(digit):\n", " \"\"\"\n", " Return the translated ones digit.\n", " \"\"\"\n", " return WORDS[digit]" ] }, { "cell_type": "code", "execution_count": 7, "id": "cc8e1411-28ff-45a1-a98c-5fb50949ab2d", "metadata": {}, "outputs": [], "source": [ "def translate_teens(n):\n", " \"\"\"\n", " Return the translated teens value n.\n", " \"\"\"\n", " return WORDS[n]" ] }, { "cell_type": "code", "execution_count": 8, "id": "2a9dd8a7-eb93-4d8f-8776-43322808e06d", "metadata": {}, "outputs": [], "source": [ "def translate_tens_digit(n):\n", " \"\"\"\n", " Return the translated tens value n.\n", " \"\"\"\n", " return WORDS[n] " ] }, { "cell_type": "code", "execution_count": 9, "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(10 + ones_digit)\n", " else:\n", " str = translate_tens_digit(10*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": 10, "id": "06a622bd-1da5-40db-854e-c3792ca82426", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'twenty-two'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "translate_pair(2, 2)" ] }, { "cell_type": "code", "execution_count": 11, "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) + ' ' + WORDS[100]\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": 12, "id": "679865a1-a2a9-4c3f-851d-ad12b293fcde", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one hundred eleven'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "translate_triplet(111)" ] }, { "cell_type": "code", "execution_count": 13, "id": "e59bd9d9-9028-49f0-a422-4d63ed661306", "metadata": {}, "outputs": [], "source": [ "def translate(n):\n", " \"\"\"\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) + ' ' + WORDS[1_000_000]\n", " if thousands > 0:\n", " if millions > 0:\n", " str += ' '\n", " str += translate_triplet(thousands) + ' ' + WORDS[1000]\n", " if hundreds > 0:\n", " if (millions > 0) or (thousands > 0):\n", " str += ' '\n", " str += translate_triplet(hundreds)\n", " \n", " return str;" ] }, { "cell_type": "markdown", "id": "ab9db272-91a5-4aa6-99ee-7c3943e8e605", "metadata": {}, "source": [ "#### **PROBLEM 5.1. b.:** [20 points] Test your rewritten number translator with 10 randomly generated integer values from 1 through 999,999,999 inclusive." ] }, { "cell_type": "code", "execution_count": 14, "id": "82c2ecca-f3d8-4884-96ff-eed4606ec46c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "n = 532,256,967\n", "five hundred thirty-two million two hundred fifty-six thousand nine hundred sixty-seven\n", "\n", "n = 321,770,321\n", "three hundred twenty-one million seven hundred seventy thousand three hundred twenty-one\n", "\n", "n = 31,117,647\n", "thirty-one million one hundred seventeen thousand six hundred forty-seven\n", "\n", "n = 6,223,154\n", "six million two hundred twenty-three thousand one hundred fifty-four\n", "\n", "n = 853,456,091\n", "eight hundred fifty-three million four hundred fifty-six thousand ninety-one\n", "\n", "n = 754,695,560\n", "seven hundred fifty-four million six hundred ninety-five thousand five hundred sixty\n", "\n", "n = 652,342,142\n", "six hundred fifty-two million three hundred forty-two thousand one hundred forty-two\n", "\n", "n = 126,345,322\n", "one hundred twenty-six million three hundred forty-five thousand three hundred twenty-two\n", "\n", "n = 153,113,587\n", "one hundred fifty-three million one hundred thirteen thousand five hundred eighty-seven\n", "\n", "n = 120,276,388\n", "one hundred twenty million two hundred seventy-six thousand three hundred eighty-eight\n" ] } ], "source": [ "import random\n", "\n", "for _ in range(10):\n", " n = random.randrange(1, 1_000_000_000)\n", " \n", " print()\n", " print(f'{n = :,d}')\n", " print(translate(n))" ] } ], "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 }