{ "cells": [ { "cell_type": "code", "execution_count": 3, "id": "economic-implementation", "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 4, "id": "abstract-resistance", "metadata": {}, "outputs": [], "source": [ "def random_request():\n", " return sorted(random.sample(range(100),2))\n", "\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": 5, "id": "falling-legislation", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[24, 51], [26, 52], [16, 45], [29, 70], [27, 62]]\n", "[[16, 45], [24, 51], [26, 52], [27, 62], [29, 70]]\n" ] } ], "source": [ "R = make_requests(5)\n", "print(R)\n", "print(sorted(R, key=lambda x : x[1]))" ] }, { "cell_type": "code", "execution_count": 6, "id": "weighted-wheel", "metadata": {}, "outputs": [], "source": [ "def greedy_solution(requests):\n", " sorted_requests = sorted(requests, key=lambda x : x[1])\n", " solution = []\n", " \n", " # pop gives you back the element at that index and\n", " # removes it from the lists\n", " solution.append(sorted_requests.pop(0))\n", " \n", " while len(sorted_requests) > 0:\n", " request = sorted_requests.pop(0)\n", " # if request can be added to solution, then add it\n", " if request[0] >= solution[-1][1]:\n", " solution.append(request)\n", " \n", " return solution" ] }, { "cell_type": "code", "execution_count": 7, "id": "elementary-bachelor", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[24, 51], [26, 52], [16, 45], [29, 70], [27, 62]]\n" ] }, { "data": { "text/plain": [ "[[16, 45]]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(R)\n", "greedy_solution(R)" ] }, { "cell_type": "code", "execution_count": 8, "id": "independent-perry", "metadata": {}, "outputs": [], "source": [ "def plot_requests(requests):\n", " for r in sorted(requests, key=lambda x:x[1]):\n", " print(\" \" * r[0] + \"-\" * (r[1]-r[0]))" ] }, { "cell_type": "code", "execution_count": null, "id": "bacterial-installation", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 9, "id": "funny-invalid", "metadata": {}, "outputs": [], "source": [ "R = make_requests(500)" ] }, { "cell_type": "code", "execution_count": 10, "id": "liberal-surrey", "metadata": {}, "outputs": [], "source": [ "#plot_requests(R)" ] }, { "cell_type": "code", "execution_count": 11, "id": "fluid-indian", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " -\n", " -\n", " ---\n", " -\n", " ----\n", " ---\n", " -\n", " --\n", " -----\n", " --\n", " ------\n", " ----\n", " --\n", " ----\n", " -\n", " -----\n", " -----\n", " -------\n", " ---\n", " -\n", " ---\n" ] } ], "source": [ "plot_requests(greedy_solution(R))" ] }, { "cell_type": "code", "execution_count": null, "id": "considerable-consolidation", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.1" } }, "nbformat": 4, "nbformat_minor": 5 }