From ef90a385118bfe2476655992a14259d832c438cc Mon Sep 17 00:00:00 2001
From: ming <98682414+mic000@users.noreply.github.com>
Date: Tue, 22 Jul 2025 09:44:14 -0700
Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20Colab=20=E5=88=9B=E5=BB=BA?=
=?UTF-8?q?=E8=80=8C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Class_2.ipynb | 478 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 478 insertions(+)
create mode 100644 Class_2.ipynb
diff --git a/Class_2.ipynb b/Class_2.ipynb
new file mode 100644
index 0000000000000..a50766c2dfa81
--- /dev/null
+++ b/Class_2.ipynb
@@ -0,0 +1,478 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "AfJ2xWo9euad",
+ "outputId": "0136d532-fb13-4186-de77-119a2f4b74d8"
+ },
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[0, 1, 2, 3, 4, 5, 7, 8, 9]\n"
+ ]
+ }
+ ],
+ "source": [
+ "import heapq\n",
+ "a = [1, 4, 7]\n",
+ "b = [2, 3, 8]\n",
+ "c = [0, 5, 9]\n",
+ "result = heapq.merge(a, b, c)\n",
+ "print(list(result)) # [0, 1, 2, 3, 4, 5, 7, 8, 9]\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "data = [3, 8, 2, 5, 10, 1, 7]\n",
+ "print(heapq.nlargest(3, data))\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "Bs67p1MHs-YC",
+ "outputId": "9403908a-5740-422d-f16c-b418c74d01f3"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[10, 8, 7]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "print(heapq.nsmallest(2, data))"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "AZIon0lPtCak",
+ "outputId": "8f18f589-d040-4663-8d46-750f8106ad83"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[1, 2]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "words = ['cat', 'elephant', 'dog', 'bird']\n",
+ "print(heapq.nlargest(2, words, key=len))\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "Wvg1bnVetPqB",
+ "outputId": "4ed8cf5d-04b4-4330-f989-6fde0dcec937"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "['elephant', 'bird']\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "import heapq\n",
+ "data = [20, 1, 45, 12, 6]\n",
+ "heapq.heapify(data)\n",
+ "print(\"Original heap: \", data)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "ZVSq-baMtPiQ",
+ "outputId": "2430650e-0155-4d12-b323-5b4c12da112e"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Original heap: [1, 6, 45, 12, 20]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "heapq.heappush(data, 3)\n",
+ "print(\"Pushed heap: \", data)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "Qx0QUYKJuP8s",
+ "outputId": "5503d975-00ab-45c7-f720-23c7f64db4c1"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Pushed heap: [1, 6, 3, 12, 20, 45]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "min_val = heapq.heappop(data)\n",
+ "print(\"Popped heap: \", data)\n",
+ "print(\"Popped value: \", min_val)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "PGeCe1ZNugWe",
+ "outputId": "af9b6458-7a4c-4727-a4bc-71bddf998952"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Popped heap: [3, 6, 45, 12, 20]\n",
+ "Popped value: 1\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "h = []\n",
+ "for x in [5, 2, 8]:\n",
+ " heapq.heappush(h, x) # [2, 5, 8]\n",
+ "\n",
+ "res = heapq.heappushpop(h, 1)\n",
+ "print(res, h)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "YRLqmc5DvSZT",
+ "outputId": "eedfac6c-6615-4385-b681-8bd5c4e2196f"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "1 [2, 5, 8]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "res2 = heapq.heapreplace(h, 1)\n",
+ "print(res2, h)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "GwVudeTOwDN4",
+ "outputId": "fd04ec00-a61a-4a1a-be9d-133c1389b8a4"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "2 [1, 5, 8]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "test = [5, 1, 7]\n",
+ "val = heapq.heappop(test)\n",
+ "print(val, test)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "S2H3gVWayiUL",
+ "outputId": "929d876e-4823-459c-a14a-2d92b6e121ae"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "5 [1, 7]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "h = [1, 3, 5]\n",
+ "heapq.heapify(h)\n",
+ "h.append(0)\n",
+ "print(\"Illegal heap: \", h)\n",
+ "min_val = heapq.heappop(h)\n",
+ "print(\"Popped value: \", min_val, \"; New heap: \", h)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "5uU8_1_Wy2cb",
+ "outputId": "afa288df-8beb-40f0-9aa1-5b7e77f4d6ef"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Illegal heap: [1, 3, 5, 0]\n",
+ "Popped value: 1 ; New heap: [0, 3, 5]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "queue.PriorityQueue #thread safe"
+ ],
+ "metadata": {
+ "id": "V-bb47Epwmkw"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "max_heap = []\n",
+ "for x in [5, 1, 8, 3]:\n",
+ " heapq.heappush(max_heap, -x)\n",
+ "print(\"Negative heap: \", max_heap)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "JpI-sVWZ0BMS",
+ "outputId": "20e9cf5f-4893-4c32-adc0-f9cbc9490515"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Negative heap: [-8, -3, -5, -1]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "max_val = -heapq.heappop(max_heap)\n",
+ "print(\"Popped value: \", max_val, \"; New heap: \", max_heap)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "cDNdzQ-P0R6J",
+ "outputId": "55878f46-2333-40ae-af30-eafc8f67932f"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Popped value: 8 ; New heap: [-5, -3, -1]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class MaxHeapObj:\n",
+ " def __init__(self, value):\n",
+ " self.value = value\n",
+ " def __lt__(self, other):\n",
+ " return self.value > other.value # Reverse order\n",
+ " def __repr__(self):\n",
+ " return str(self.value)\n",
+ "\n",
+ "\n",
+ "h = []\n",
+ "for x in [5, 1, 8, 3]:\n",
+ " heapq.heappush(h, MaxHeapObj(x))\n",
+ "print(\"Original heap: \", h)\n",
+ "max_obj = heapq.heappop(h)\n",
+ "print(\"Popped value: \", max_obj.value, \"; New heap: \", h)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "f3I2-0vJ0lb6",
+ "outputId": "2434b3a0-1332-4ddc-b2c4-764d209b281f"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Original heap: [8, 3, 5, 1]\n",
+ "Popped value: 8 ; New heap: [5, 3, 1]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from functools import cmp_to_key\n",
+ "def reverse_compare(x, y):\n",
+ " return -1 if x > y else (1 if x < y else 0)\n",
+ "\n",
+ "KeyObj = cmp_to_key(reverse_compare)\n",
+ "h = []\n",
+ "for x in [5, 1, 8, 3]:\n",
+ " heapq.heappush(h, KeyObj(x))\n",
+ "print([obj.obj for obj in h])\n",
+ "max_obj = heapq.heappop(h)\n",
+ "print(\"Popped value: \", max_obj.obj, \"; New heap: \", h)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "13rXzHJl2T9C",
+ "outputId": "fb5c1e78-e385-46e3-e4b0-ff931d8636a1"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[8, 3, 5, 1]\n",
+ "Popped value: 8 ; New heap: [, , ]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "import heapq\n",
+ "\n",
+ "def top_k_largest(nums, k):\n",
+ " if k <= 0:\n",
+ " return []\n",
+ " heap = nums[:k]\n",
+ " heapq.heapify(heap)\n",
+ " for x in nums[k:]:\n",
+ " if x > heap[0]:\n",
+ " heapq.heapreplace(heap, x)\n",
+ " return sorted(heap, reverse=True)\n",
+ "\n",
+ "nums = [5, 1, 8, 3, 2, 10]\n",
+ "k = 3\n",
+ "print(top_k_largest(nums, k))"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "jTHdRUwB6dM3",
+ "outputId": "fdae2c64-71ef-43b5-9b0e-3c8f8169635d"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[10, 8, 5]\n"
+ ]
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file