{ "cells": [ { "cell_type": "markdown", "id": "90ffdccd", "metadata": {}, "source": [ "*Interval Request Scheduling* -- Greedy Algorithm" ] }, { "cell_type": "code", "execution_count": 35, "id": "66ae47a2", "metadata": {}, "outputs": [], "source": [ "import random\n", "\n", "# random pair (s, f) where s and f are integers between\n", "# 0 and 99, inclusive\n", "def random_request():\n", " return tuple(sorted(random.sample(range(0, 100), 2)))\n", "\n", "# make n random requests and return them all\n", "def make_requests(n):\n", " #requests = []\n", " #for i in range(n):\n", " # requests.append(random_request())\n", " #return requests\n", " return [random_request() for i in range(n)]" ] }, { "cell_type": "code", "execution_count": 36, "id": "8bf45251", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(32, 98), (3, 27), (36, 57), (41, 62), (74, 92)]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_requests(5)" ] }, { "cell_type": "markdown", "id": "630b680c", "metadata": {}, "source": [ "random_request()" ] }, { "cell_type": "raw", "id": "7416d993", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 29, "id": "5636e205", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "Sample larger than population or is negative", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [29]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mrandom\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msample\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/usr/local/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/random.py:449\u001b[0m, in \u001b[0;36mRandom.sample\u001b[0;34m(self, population, k, counts)\u001b[0m\n\u001b[1;32m 447\u001b[0m randbelow \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_randbelow\n\u001b[1;32m 448\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;241m0\u001b[39m \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m k \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m n:\n\u001b[0;32m--> 449\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSample larger than population or is negative\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 450\u001b[0m result \u001b[38;5;241m=\u001b[39m [\u001b[38;5;28;01mNone\u001b[39;00m] \u001b[38;5;241m*\u001b[39m k\n\u001b[1;32m 451\u001b[0m setsize \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m21\u001b[39m \u001b[38;5;66;03m# size of a small set minus size of an empty list\u001b[39;00m\n", "\u001b[0;31mValueError\u001b[0m: Sample larger than population or is negative" ] } ], "source": [ "random.sample([1,2,3],4)" ] }, { "cell_type": "code", "execution_count": null, "id": "014186ce", "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.9.10" } }, "nbformat": 4, "nbformat_minor": 5 }