{
 "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": [
    "### <center>San Jose State University<br>Dep|artment of Applied Data Science<br><br>**DATA 200<br>Computational Programming for Data Analytics**<br><br>Spring 2024<br>Instructor: Ron Mak</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a032b427-fc88-4e31-a531-e678fa24d61e",
   "metadata": {},
   "source": [
    "# Number Translator, version 4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "172ddfe7-9928-449c-a891-e51aed256876",
   "metadata": {},
   "source": [
    "#### We now begin to translate a pair. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a54ce9b2-4e8d-4f39-94cd-949d5feb67a9",
   "metadata": {},
   "source": [
    "## Example: Break apart a pair `pair = 23`"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8f9594ae-af77-4892-a77f-bd3d0a03eaa1",
   "metadata": {},
   "source": [
    "#### First, extract the first tens digit:\n",
    "- #### 23 // 10 ==> 2\n",
    "- #### Therefore, `tens_digit = pair // 10`\n",
    "#### Then the remainin ones digit:\n",
    "- #### 23 % 10 ==> 3 (remainder)\n",
    "- #### Therefore, `ones_digit = pair % 10`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2670f06c-7cd7-478d-83de-16e71d174e79",
   "metadata": {},
   "source": [
    "### Then:\n",
    "- #### If the tens digit is 0 and the ones digit is nonzero, calls function `translate_digit()` to translate the ones digit.\n",
    "- #### Else if the tens digit is a 1, then call new function `translate_teens()` that returns the corrent word \"ten\", \"eleven\", \"twelve\", ..., \"nineteen\".\n",
    "- #### Else call new function `translate_tens_digit()` that returns the correct word \"twenty\", \"thirty\", ..., \"ninety\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fc56b99d-716c-45c1-a24e-bed79d9541f5",
   "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": 2,
   "id": "0a9b0dd7-6964-49f1-adff-e9f65f27ac01",
   "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": 3,
   "id": "44d70924-5023-49d9-a2b4-00e860982558",
   "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",
    "    elif digit == 9:\n",
    "        return 'ninety' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5100eb3b-9d04-4b36-b4b0-6b46db169c1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def translate_pair(pair):\n",
    "    \"\"\"\n",
    "    Return the translation of a pair digits\n",
    "    \"\"\"\n",
    "    tens_digit = pair // 10\n",
    "    ones_digit = pair % 10\n",
    "\n",
    "    if (tens_digit == 0) and (ones_digit > 0):\n",
    "        str = translate_digit(ones_digit)\n",
    "    elif tens_digit == 1:\n",
    "        str = translate_teens(ones_digit)\n",
    "    elif tens_digit != 0:\n",
    "        str = translate_tens_digit(tens_digit)\n",
    "        \n",
    "        # Append a hyphen and the ones word.\n",
    "        if ones_digit > 0:\n",
    "            str += '-' + translate_digit(ones_digit)\n",
    "\n",
    "    else:\n",
    "        str = ''\n",
    "            \n",
    "    return str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "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": 6,
   "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": 7,
   "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": 8,
   "id": "bfa9ba15-716d-4eab-aaaa-2e68528efc5e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Number to translate?  123\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "one hundred twenty-three\n",
      "\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Number to translate?  123456789\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine\n",
      "\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Number to translate?  100_000_001\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "one hundred million  one\n",
      "\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Number to translate?  0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Done!\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()\n",
    "\n",
    "print()\n",
    "print('Done!')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30dd7c8f-8d2f-4a4b-9f4d-62279951c807",
   "metadata": {},
   "source": [
    "## How do we get rid of the extra blanks?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "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
}
