{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "67b11c97-d272-4ca6-9e37-6c14c48ec9f9",
   "metadata": {},
   "source": [
    "# Notebook With Solutions For Lec 12 Worksheet"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d536695-e8b2-4bf1-8959-7667594e2add",
   "metadata": {},
   "source": [
    "## Battleship\n",
    "(Original Function Code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ff136f77-0aaa-4609-b045-866b056b5ddf",
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw(x, y, M = 8, N = 6):\n",
    "    i = 0\n",
    "    while i < M:\n",
    "        j = 0\n",
    "        while j < N:\n",
    "            if y == i and x == j:\n",
    "                print(\"X\", end = \"\")\n",
    "            else:\n",
    "                print(\".\", end = \"\")\n",
    "            j += 1\n",
    "        print() # just a newline because end=\"\\n\" by default\n",
    "        i += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac1663ee-6e1b-4b28-95c2-83b252a42b7c",
   "metadata": {},
   "source": [
    "### 1. Assume the user types 3 and 4.0 as input; what will the types of the values in global variables x and y be, respectively?\n",
    "Solution: D. str, str\n",
    "\n",
    "**Explanation**: The `input()` function always returns strings."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18e53309-1f12-41b5-b4f7-702b01a4d039",
   "metadata": {},
   "source": [
    "### 2. What call would print a map with an X in the top-left corner?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a3d8be8d-8c6d-4d55-a99d-f54e349085c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X.....\n",
      "......\n",
      "......\n",
      "......\n",
      "......\n",
      "......\n",
      "......\n",
      "......\n"
     ]
    }
   ],
   "source": [
    "draw(0,0) ## A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6228398f-86fa-4b1f-b5ba-54f9634cea4b",
   "metadata": {},
   "source": [
    "### 3. Calling `draw(-1, -1)` prints the equvalent of what?\n",
    "Solution: B `print((\".\"*6 + \"\\n\") * 8)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9ccdbc6b-2a65-4b29-813f-425d8da7bb10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "......\n",
      "......\n",
      "......\n",
      "......\n",
      "......\n",
      "......\n",
      "......\n",
      "......\n"
     ]
    }
   ],
   "source": [
    "draw(-1,-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9fd140cf-20b3-4606-9990-da748e5a5a27",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "......\n",
      "......\n",
      "......\n",
      "......\n",
      "......\n",
      "......\n",
      "......\n",
      "......\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print((\".\"*6 + \"\\n\") * 8)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dfab10f-395c-4d8f-bf2e-6da73ceaa2bf",
   "metadata": {},
   "source": [
    "### 4. Which parameter to `draw` represents the width of the map?\n",
    "\n",
    "Solution: D. N\n",
    "\n",
    "Explanation: The parameter N controls the inner while loop execution, which prints the contents of a single line."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0400c1cc-921b-43dd-ab20-9e2687c0ee8b",
   "metadata": {},
   "source": [
    "## Tic-Tac-Toe\n",
    "\n",
    "The following code attempts to draw a tic-tac-toe board.\n",
    "```\n",
    "X| |\n",
    "-+-+-\n",
    " | |\n",
    "-+-+-\n",
    " | |\n",
    "```\n",
    "\n",
    "(original function code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1d66062e-b421-4f25-9420-573d721b124c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw(x = 0, y = 0, move = \"X\"):\n",
    "    i = 1\n",
    "    while(i < 6):\n",
    "        if i%2 == 0:\n",
    "            print(\"-+-+-\", end = \"\")\n",
    "        else:\n",
    "            j = 0\n",
    "            while j < 5:\n",
    "                if j % 2 != 0:\n",
    "                    print(\"|\", end = \"\")\n",
    "                elif i == 2*x + 1 and j == 2*y:\n",
    "                    print(move, end=\"\")\n",
    "                else:\n",
    "                    print(\" \", end = \"\")\n",
    "                j += 1\n",
    "        print()\n",
    "        i += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "341cce28-5b33-431a-96aa-0610113fe437",
   "metadata": {},
   "source": [
    "### 5. Assume the user provides 2, 1.0 and X as inputs to x, y, and move respectively. What will be the types of the values in global variables x, y, and move?\n",
    "\n",
    "Solution: C str, str, str\n",
    "\n",
    "Explanation: The return type from the input() function is always string."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be862b06-547b-4195-a8dd-d4f882a17c00",
   "metadata": {},
   "source": [
    "### 6. What would the following function call evaluate to?\n",
    "`draw(x = 2, move = \"O\")`\n",
    "\n",
    "Solution: B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "dd69679c-5fa6-495d-b848-b7200195699c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " | | \n",
      "-+-+-\n",
      " | | \n",
      "-+-+-\n",
      "O| | \n"
     ]
    }
   ],
   "source": [
    "draw(x=2,move=\"O\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7c0e931-8f2c-4e43-8600-ac72746f58de",
   "metadata": {},
   "source": [
    "### 7. Which of the following function calls would place an \"O\" at the bottom-right corner of the board?\n",
    "\n",
    "Solution: D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "88839d15-ed68-4222-841d-07bf41ba5b12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " | | \n",
      "-+-+-\n",
      " | | \n",
      "-+-+-\n",
      " | |O\n"
     ]
    }
   ],
   "source": [
    "draw(2, 2, \"O\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7b24675-f64f-48c5-b2ee-67c87964a285",
   "metadata": {},
   "source": [
    "### 8. What does the following function call evaluate to?\n",
    "`draw(-1, -1)`\n",
    "\n",
    "Solution: A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5bfb5b81-8397-44bc-ad30-de71f62c4403",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " | | \n",
      "-+-+-\n",
      " | | \n",
      "-+-+-\n",
      " | | \n"
     ]
    }
   ],
   "source": [
    "draw(-1,-1)"
   ]
  }
 ],
 "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.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
