{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ch01-lab-title",
   "metadata": {},
   "source": [
    "# Lab\n",
    "\n",
    "- Use this assignment to practice the chapter's concepts and coding skills in runnable Python code.\n",
    "- Sign in from the account menu before you start working on the assignment.\n",
    "- Write and run your code directly in the coding cells.\n",
    "- Multiple submissions are allowed. The highest score is kept.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ch01-lab-q1",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Question 1: CRISP-DM phases\n",
    "# A retail manager wants to predict which customers are likely to stop buying.\n",
    "# Store the first three CRISP-DM phases in variables named phase_1, phase_2, and phase_3.\n",
    "# Store the last three phases in variables named phase_4, phase_5, and phase_6.\n",
    "# Then print the first phase and the last phase.\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ch01-lab-a1",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First phase: Business Understanding\n",
      "Last phase: Deployment\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "phase_1 = \"Business Understanding\"\n",
    "phase_2 = \"Data Understanding\"\n",
    "phase_3 = \"Data Preparation\"\n",
    "phase_4 = \"Modeling\"\n",
    "phase_5 = \"Evaluation\"\n",
    "phase_6 = \"Deployment\"\n",
    "\n",
    "print(\"First phase:\", phase_1)\n",
    "print(\"Last phase:\", phase_6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ch01-lab-q2",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Question 2: Data science tool categories\n",
    "# Create four variables named data_management_tool, data_manipulation_tool,\n",
    "# data_analysis_tool, and data_visualization_tool.\n",
    "# Give each variable one tool example from Section 1.1.\n",
    "# Then print the visualization tool.\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ch01-lab-a2",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Visualization tool: Matplotlib\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "data_management_tool = \"SQL database\"\n",
    "data_manipulation_tool = \"pandas\"\n",
    "data_analysis_tool = \"scikit-learn\"\n",
    "data_visualization_tool = \"Matplotlib\"\n",
    "\n",
    "print(\"Visualization tool:\", data_visualization_tool)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ch01-lab-q3",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Question 3: Decimal to binary conversion\n",
    "# Convert the decimal number 13 to binary using base-2 place values.\n",
    "# Store each binary digit in a variable named bit_3, bit_2, bit_1, and bit_0.\n",
    "# Store the full binary answer in a variable named manual_binary.\n",
    "# Then print manual_binary.\n",
    "# Check your answer by printing bin(13).\n",
    "decimal_number = 13\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ch01-lab-a3",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Manual conversion: 0b1101\n",
      "Python check: 0b1101\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "decimal_number = 13\n",
    "bit_3 = 1  # 8s place\n",
    "bit_2 = 1  # 4s place\n",
    "bit_1 = 0  # 2s place\n",
    "bit_0 = 1  # 1s place\n",
    "\n",
    "manual_binary = \"0b1101\"\n",
    "\n",
    "print(\"Manual conversion:\", manual_binary)\n",
    "print(\"Python check:\", bin(decimal_number))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ch01-lab-q4",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Question 4: Expressions and statements\n",
    "# The values below describe one customer order.\n",
    "# Write assignment statements that calculate subtotal, tax, and total.\n",
    "# Then print all three values rounded to 2 decimal places.\n",
    "# Use round(value, 2) to round a number to 2 decimal places.\n",
    "unit_price = 18.75\n",
    "quantity = 4\n",
    "tax_rate = 0.0825\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ch01-lab-a4",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Subtotal: 75.0\n",
      "Tax: 6.19\n",
      "Total: 81.19\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "unit_price = 18.75\n",
    "quantity = 4\n",
    "tax_rate = 0.0825\n",
    "\n",
    "subtotal = unit_price * quantity\n",
    "tax = subtotal * tax_rate\n",
    "total = subtotal + tax\n",
    "\n",
    "print(\"Subtotal:\", round(subtotal, 2))\n",
    "print(\"Tax:\", round(tax, 2))\n",
    "print(\"Total:\", round(total, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ch01-lab-q5",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Question 5: Number systems and character encoding\n",
    "# A simple inventory system uses product code \"C\" and item number 64.\n",
    "# Save the decimal code for \"C\" in c_decimal using ord().\n",
    "# Save the binary representation of c_decimal in c_binary using bin().\n",
    "# Save the hexadecimal representation of item_number in item_hex using hex().\n",
    "# Print c_decimal, c_binary, and item_hex.\n",
    "item_number = 64\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ch01-lab-a5",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C decimal: 67\n",
      "C binary: 0b1000011\n",
      "Item hex: 0x40\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "item_number = 64\n",
    "c_decimal = ord(\"C\")\n",
    "c_binary = bin(c_decimal)\n",
    "item_hex = hex(item_number)\n",
    "\n",
    "print(\"C decimal:\", c_decimal)\n",
    "print(\"C binary:\", c_binary)\n",
    "print(\"Item hex:\", item_hex)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch01-lab-submit",
   "metadata": {},
   "source": [
    "<style>\n",
    ".ch01-lab-grader { max-width: 900px; margin-top: 1rem; }\n",
    ".ch01-lab-feedback-list { display: grid; gap: 0.45rem; margin: 0 0 1rem; }\n",
    ".ch01-lab-feedback-list[hidden] { display: none; }\n",
    ".ch01-lab-feedback-row { border: 1px solid #d8dee4; border-radius: 6px; padding: 0.55rem 0.65rem; }\n",
    ".ch01-lab-feedback-row strong { margin-right: 0.35rem; }\n",
    ".ch01-lab-feedback-row.correct { border-color: #aceebb; background: #dafbe1; color: #116329; }\n",
    ".ch01-lab-feedback-row.incorrect { border-color: #ffcecb; background: #ffebe9; color: #cf222e; }\n",
    ".ch01-lab-actions { display: flex; gap: 0.75rem; align-items: center; margin: 1rem 0 0; flex-wrap: wrap; }\n",
    ".ch01-lab-actions button { border: 1px solid #176de8; border-radius: 6px; background: #176de8; color: white; padding: 0.55rem 0.9rem; cursor: pointer; font-weight: 600; }\n",
    ".ch01-lab-actions button.secondary { background: white; color: #176de8; }\n",
    ".ch01-lab-actions.is-locked { opacity: 0.55; }\n",
    ".ch01-lab-actions button:disabled { cursor: not-allowed; opacity: 0.65; }\n",
    ".ch01-lab-result { font-weight: 700; }\n",
    "</style>\n",
    "\n",
    "<div class='ch01-lab-grader' id='ch01-lab-grader'>\n",
    "  <div class='ch01-lab-feedback-list' aria-live='polite' hidden>\n",
    "    <div class='ch01-lab-feedback-row' data-question='q1'><strong>Question 1</strong><span></span></div>\n",
    "    <div class='ch01-lab-feedback-row' data-question='q2'><strong>Question 2</strong><span></span></div>\n",
    "    <div class='ch01-lab-feedback-row' data-question='q3'><strong>Question 3</strong><span></span></div>\n",
    "    <div class='ch01-lab-feedback-row' data-question='q4'><strong>Question 4</strong><span></span></div>\n",
    "    <div class='ch01-lab-feedback-row' data-question='q5'><strong>Question 5</strong><span></span></div>\n",
    "  </div>\n",
    "  <div class='ch01-lab-actions'>\n",
    "    <button type='button' id='ch01-lab-submit' disabled>Submit</button>\n",
    "    <button type='button' class='secondary' id='ch01-lab-reset'>Clear Results</button>\n",
    "    <span class='ch01-lab-result' id='ch01-lab-result' aria-live='polite'></span>\n",
    "  </div>\n",
    "</div>\n",
    "\n",
    "<script>\n",
    "(function () {\n",
    "  var labId = 'ch01-lab';\n",
    "\n",
    "  function getParams() {\n",
    "    var params = new URLSearchParams(window.location.search);\n",
    "    return {\n",
    "      canvas_course_id: params.get('canvas_course_id') || params.get('course_id') || '',\n",
    "      canvas_assignment_id: params.get('canvas_assignment_id') || params.get('assignment_id') || '',\n",
    "      canvas_user_id: params.get('canvas_user_id') || params.get('user_id') || ''\n",
    "    };\n",
    "  }\n",
    "\n",
    "  function loadSessionIdentity(identity, actions, submit, result) {\n",
    "    submit.disabled = true;\n",
    "    actions.classList.add('is-locked');\n",
    "    result.textContent = 'Sign in from the account menu to submit this assignment.';\n",
    "\n",
    "    return fetch('/api/v1/session.php', { credentials: 'same-origin' })\n",
    "      .then(function (response) { return response.ok ? response.json() : null; })\n",
    "      .then(function (payload) {\n",
    "        if (!payload || !payload.authenticated || !payload.identity) return identity;\n",
    "        identity.canvas_user_id = payload.identity.canvas_user_id || identity.canvas_user_id || '';\n",
    "        identity.student_identifier = payload.identity.student_identifier || '';\n",
    "        submit.disabled = false;\n",
    "        actions.classList.remove('is-locked');\n",
    "        result.textContent = payload.identity.display_name ? 'Signed in as ' + payload.identity.display_name + '.' : 'Signed in.';\n",
    "        return identity;\n",
    "      })\n",
    "      .catch(function () { return identity; });\n",
    "  }\n",
    "\n",
    "  function codeFromCell(cell) {\n",
    "    var cm = cell.querySelector('.CodeMirror');\n",
    "    if (cm && cm.CodeMirror && typeof cm.CodeMirror.getValue === 'function') return cm.CodeMirror.getValue();\n",
    "    var cmContent = cell.querySelector('.cm-content');\n",
    "    if (cmContent && cmContent.cmView && cmContent.cmView.view) return cmContent.cmView.view.state.doc.toString();\n",
    "    var textarea = cell.querySelector('textarea');\n",
    "    if (textarea) return textarea.value;\n",
    "    var pre = cell.querySelector('.cell_input pre');\n",
    "    return pre ? pre.textContent : '';\n",
    "  }\n",
    "\n",
    "  function collectCodeCells() {\n",
    "    var cells = Array.prototype.slice.call(document.querySelectorAll('div.cell.tag_thebe-interactive')).slice(0, 5);\n",
    "    var code = {};\n",
    "    cells.forEach(function (cell, index) {\n",
    "      code['q' + (index + 1)] = codeFromCell(cell);\n",
    "    });\n",
    "    return code;\n",
    "  }\n",
    "\n",
    "  function applyFeedback(grader, feedback) {\n",
    "    var list = grader.querySelector('.ch01-lab-feedback-list');\n",
    "    list.hidden = false;\n",
    "    ['q1', 'q2', 'q3', 'q4', 'q5'].forEach(function (question) {\n",
    "      var row = grader.querySelector('[data-question=' + question + ']');\n",
    "      var message = row.querySelector('span');\n",
    "      var item = feedback[question];\n",
    "      row.className = 'ch01-lab-feedback-row';\n",
    "      if (!item) return;\n",
    "      message.textContent = item.message + ' (' + item.score + ' / ' + item.max_score + ')';\n",
    "      row.classList.add(item.correct ? 'correct' : 'incorrect');\n",
    "    });\n",
    "  }\n",
    "\n",
    "  function clearFeedback(grader) {\n",
    "    var list = grader.querySelector('.ch01-lab-feedback-list');\n",
    "    list.hidden = true;\n",
    "    grader.querySelectorAll('.ch01-lab-feedback-row').forEach(function (row) {\n",
    "      row.className = 'ch01-lab-feedback-row';\n",
    "      var message = row.querySelector('span');\n",
    "      if (message) message.textContent = '';\n",
    "    });\n",
    "  }\n",
    "\n",
    "  function saveAttempt(payload) {\n",
    "    return fetch('/api/v1/lab-attempts.php', {\n",
    "      method: 'POST',\n",
    "      headers: { 'Content-Type': 'application/json' },\n",
    "      credentials: 'same-origin',\n",
    "      body: JSON.stringify(payload)\n",
    "    }).then(function (response) {\n",
    "      return response.text().then(function (text) {\n",
    "        var payload = text ? JSON.parse(text) : null;\n",
    "        if (!payload || !response.ok || !payload.ok) {\n",
    "          throw new Error((payload && (payload.message || payload.error)) || 'Submission API is unavailable.');\n",
    "        }\n",
    "        return payload;\n",
    "      });\n",
    "    });\n",
    "  }\n",
    "\n",
    "  function initLabGrader() {\n",
    "    var grader = document.getElementById('ch01-lab-grader');\n",
    "    if (!grader || grader.dataset.ready === 'true') return;\n",
    "    grader.dataset.ready = 'true';\n",
    "\n",
    "    var identity = getParams();\n",
    "    var actions = grader.querySelector('.ch01-lab-actions');\n",
    "    var result = document.getElementById('ch01-lab-result');\n",
    "    var submit = document.getElementById('ch01-lab-submit');\n",
    "    var reset = document.getElementById('ch01-lab-reset');\n",
    "\n",
    "    loadSessionIdentity(identity, actions, submit, result);\n",
    "\n",
    "    submit.addEventListener('click', function () {\n",
    "      clearFeedback(grader);\n",
    "      submit.disabled = true;\n",
    "      result.textContent = 'Submitting code.';\n",
    "\n",
    "      saveAttempt({\n",
    "        lab_id: labId,\n",
    "        code: collectCodeCells(),\n",
    "        canvas_course_id: identity.canvas_course_id,\n",
    "        canvas_assignment_id: identity.canvas_assignment_id,\n",
    "        canvas_user_id: identity.canvas_user_id\n",
    "      })\n",
    "        .then(function (payload) {\n",
    "          var best = (payload.best_score === null || typeof payload.best_score === 'undefined') ? payload.score : payload.best_score;\n",
    "          var attempt = (payload.attempt_count === null || typeof payload.attempt_count === 'undefined') ? '' : ' Attempt ' + payload.attempt_count + '.';\n",
    "          applyFeedback(grader, payload.feedback || {});\n",
    "          result.textContent = 'Submission saved.' + attempt + ' Score: ' + payload.score + ' / ' + payload.max_score + '. Best score: ' + best + ' / ' + payload.max_score + '. Canvas sync: ' + payload.canvas_sync_status + '.';\n",
    "        })\n",
    "        .catch(function (error) {\n",
    "          result.textContent = 'Could not save this attempt: ' + error.message;\n",
    "        })\n",
    "        .finally(function () {\n",
    "          submit.disabled = false;\n",
    "        });\n",
    "    });\n",
    "\n",
    "    reset.addEventListener('click', function () {\n",
    "      clearFeedback(grader);\n",
    "      result.textContent = '';\n",
    "    });\n",
    "  }\n",
    "\n",
    "  if (document.readyState === 'loading') {\n",
    "    document.addEventListener('DOMContentLoaded', initLabGrader);\n",
    "  } else {\n",
    "    initLabGrader();\n",
    "  }\n",
    "})();\n",
    "</script>\n",
    ""
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "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.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
