|
@@ -0,0 +1,432 @@
|
|
|
+{
|
|
|
+ "cells": [
|
|
|
+ {
|
|
|
+ "cell_type": "markdown",
|
|
|
+ "metadata": {},
|
|
|
+ "source": [
|
|
|
+ "# Inventory Class"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 22,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [],
|
|
|
+ "source": [
|
|
|
+ "import csv # step 1\n",
|
|
|
+ "\n",
|
|
|
+ "class Inventory(): # step 2\n",
|
|
|
+ " \n",
|
|
|
+ " def __init__(self, csv_filename): # step 3\n",
|
|
|
+ " with open(csv_filename) as f: # step 4\n",
|
|
|
+ " reader = csv.reader(f)\n",
|
|
|
+ " rows = list(reader)\n",
|
|
|
+ " self.header = rows[0] # step 5\n",
|
|
|
+ " self.rows = rows[1:]\n",
|
|
|
+ " for row in self.rows: # step 6\n",
|
|
|
+ " row[-1] = int(row[-1])"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 23,
|
|
|
+ "metadata": {
|
|
|
+ "scrolled": true
|
|
|
+ },
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "name": "stdout",
|
|
|
+ "output_type": "stream",
|
|
|
+ "text": [
|
|
|
+ "['Id', 'Company', 'Product', 'TypeName', 'Inches', 'ScreenResolution', 'Cpu', 'Ram', 'Memory', 'Gpu', 'OpSys', 'Weight', 'Price_euros']\n",
|
|
|
+ "1303\n"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "source": [
|
|
|
+ "inventory = Inventory('laptops.csv') # step 7\n",
|
|
|
+ "print(inventory.header) # step 8\n",
|
|
|
+ "print(len(inventory.rows)) # step 9"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "markdown",
|
|
|
+ "metadata": {},
|
|
|
+ "source": [
|
|
|
+ "# Finding a Laptop From the Id"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 21,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [],
|
|
|
+ "source": [
|
|
|
+ "import csv \n",
|
|
|
+ "\n",
|
|
|
+ "class Inventory(): \n",
|
|
|
+ " \n",
|
|
|
+ " def __init__(self, csv_filename):\n",
|
|
|
+ " with open(csv_filename) as f: \n",
|
|
|
+ " reader = csv.reader(f)\n",
|
|
|
+ " rows = list(reader)\n",
|
|
|
+ " self.header = rows[0] \n",
|
|
|
+ " self.rows = rows[1:]\n",
|
|
|
+ " for row in self.rows: \n",
|
|
|
+ " row[-1] = int(row[-1])\n",
|
|
|
+ " \n",
|
|
|
+ " def get_laptop_from_id(self, laptop_id): # step 1\n",
|
|
|
+ " for row in self.rows: # step 2\n",
|
|
|
+ " if row[0] == laptop_id:\n",
|
|
|
+ " return row\n",
|
|
|
+ " return None # step 3"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 20,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "name": "stdout",
|
|
|
+ "output_type": "stream",
|
|
|
+ "text": [
|
|
|
+ "['3362737', 'HP', '250 G6', 'Notebook', '15.6', 'Full HD 1920x1080', 'Intel Core i5 7200U 2.5GHz', '8GB', '256GB SSD', 'Intel HD Graphics 620', 'No OS', '1.86kg', 575]\n",
|
|
|
+ "None\n"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "source": [
|
|
|
+ "inventory = Inventory('laptops.csv') # step 4\n",
|
|
|
+ "print(inventory.get_laptop_from_id('3362737')) # step 5\n",
|
|
|
+ "print(inventory.get_laptop_from_id('3362736')) # step 6"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "markdown",
|
|
|
+ "metadata": {},
|
|
|
+ "source": [
|
|
|
+ "# Improving Id Lookups"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 33,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [],
|
|
|
+ "source": [
|
|
|
+ "import csv \n",
|
|
|
+ "\n",
|
|
|
+ "class Inventory(): \n",
|
|
|
+ " \n",
|
|
|
+ " def __init__(self, csv_filename):\n",
|
|
|
+ " with open(csv_filename) as f: \n",
|
|
|
+ " reader = csv.reader(f)\n",
|
|
|
+ " rows = list(reader)\n",
|
|
|
+ " self.header = rows[0] \n",
|
|
|
+ " self.rows = rows[1:]\n",
|
|
|
+ " for row in self.rows: \n",
|
|
|
+ " row[-1] = int(row[-1])\n",
|
|
|
+ " self.id_to_row = {} # step 1\n",
|
|
|
+ " for row in self.rows: # step 2\n",
|
|
|
+ " self.id_to_row[row[0]] = row \n",
|
|
|
+ " \n",
|
|
|
+ " def get_laptop_from_id(self, laptop_id):\n",
|
|
|
+ " for row in self.rows: \n",
|
|
|
+ " if row[0] == laptop_id:\n",
|
|
|
+ " return row\n",
|
|
|
+ " return None \n",
|
|
|
+ " \n",
|
|
|
+ " def get_laptop_from_id_fast(self, laptop_id): # step 3\n",
|
|
|
+ " if laptop_id in self.id_to_row: # step 4\n",
|
|
|
+ " return self.id_to_row[laptop_id]\n",
|
|
|
+ " return None"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 27,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "name": "stdout",
|
|
|
+ "output_type": "stream",
|
|
|
+ "text": [
|
|
|
+ "['3362737', 'HP', '250 G6', 'Notebook', '15.6', 'Full HD 1920x1080', 'Intel Core i5 7200U 2.5GHz', '8GB', '256GB SSD', 'Intel HD Graphics 620', 'No OS', '1.86kg', 575]\n",
|
|
|
+ "None\n"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "source": [
|
|
|
+ "inventory = Inventory('laptops.csv') # step 5\n",
|
|
|
+ "print(inventory.get_laptop_from_id_fast('3362737')) # step 6\n",
|
|
|
+ "print(inventory.get_laptop_from_id_fast('3362736')) # step 7"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "markdown",
|
|
|
+ "metadata": {},
|
|
|
+ "source": [
|
|
|
+ "# Comparing Performance"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 44,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "name": "stdout",
|
|
|
+ "output_type": "stream",
|
|
|
+ "text": [
|
|
|
+ "0.5769174098968506\n",
|
|
|
+ "0.0021619796752929688\n"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "source": [
|
|
|
+ "import time # step 1\n",
|
|
|
+ "import random # step 2\n",
|
|
|
+ "\n",
|
|
|
+ "ids = [random.randint(1000000, 9999999) for _ in range(10000)] # step 3\n",
|
|
|
+ "\n",
|
|
|
+ "inventory = Inventory('laptops.csv') # step 4\n",
|
|
|
+ "\n",
|
|
|
+ "total_time_no_dict = 0 # step 5\n",
|
|
|
+ "for id in ids: # step 6\n",
|
|
|
+ " start = time.time() # step 6.1\n",
|
|
|
+ " inventory.get_laptop_from_id(id) # step 6.2\n",
|
|
|
+ " end = time.time() # step 6.3\n",
|
|
|
+ " total_time_no_dict += end - start # step 6.4\n",
|
|
|
+ " \n",
|
|
|
+ "total_time_dict = 0 # step 7\n",
|
|
|
+ "for id in ids: # step 8\n",
|
|
|
+ " start = time.time() # step 8.1\n",
|
|
|
+ " inventory.get_laptop_from_id_fast(id) # step 8.2\n",
|
|
|
+ " end = time.time() # step 8.3\n",
|
|
|
+ " total_time_dict += end - start # step 8.4\n",
|
|
|
+ " \n",
|
|
|
+ "print(total_time_no_dict) # step 9\n",
|
|
|
+ "print(total_time_dict)"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "markdown",
|
|
|
+ "metadata": {},
|
|
|
+ "source": [
|
|
|
+ "# Two Laptop Promotion"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 3,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [],
|
|
|
+ "source": [
|
|
|
+ "import csv \n",
|
|
|
+ "\n",
|
|
|
+ "class Inventory(): \n",
|
|
|
+ " \n",
|
|
|
+ " def __init__(self, csv_filename):\n",
|
|
|
+ " with open(csv_filename) as f: \n",
|
|
|
+ " reader = csv.reader(f)\n",
|
|
|
+ " rows = list(reader)\n",
|
|
|
+ " self.header = rows[0] \n",
|
|
|
+ " self.rows = rows[1:]\n",
|
|
|
+ " for row in self.rows: \n",
|
|
|
+ " row[-1] = int(row[-1])\n",
|
|
|
+ " self.id_to_row = {} \n",
|
|
|
+ " for row in self.rows: \n",
|
|
|
+ " self.id_to_row[row[0]] = row \n",
|
|
|
+ " \n",
|
|
|
+ " def get_laptop_from_id(self, laptop_id):\n",
|
|
|
+ " for row in self.rows: \n",
|
|
|
+ " if row[0] == laptop_id:\n",
|
|
|
+ " return row\n",
|
|
|
+ " return None \n",
|
|
|
+ " \n",
|
|
|
+ " def get_laptop_from_id_fast(self, laptop_id): \n",
|
|
|
+ " if laptop_id in self.id_to_row: \n",
|
|
|
+ " return self.id_to_row[laptop_id]\n",
|
|
|
+ " return None\n",
|
|
|
+ "\n",
|
|
|
+ " def check_promotion_dollars(self, dollars): # step 1\n",
|
|
|
+ " for row in self.rows: # step 2\n",
|
|
|
+ " if row[-1] == dollars:\n",
|
|
|
+ " return True\n",
|
|
|
+ " for row1 in self.rows: # step 3\n",
|
|
|
+ " for row2 in self.rows:\n",
|
|
|
+ " if row1[-1] + row2[-1] == dollars:\n",
|
|
|
+ " return True\n",
|
|
|
+ " return False # step 4"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 4,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "name": "stdout",
|
|
|
+ "output_type": "stream",
|
|
|
+ "text": [
|
|
|
+ "True\n",
|
|
|
+ "False\n"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "source": [
|
|
|
+ "inventory = Inventory('laptops.csv') # step 5\n",
|
|
|
+ "print(inventory.check_promotion_dollars(1000)) # step 6\n",
|
|
|
+ "print(inventory.check_promotion_dollars(442)) # step 7"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "markdown",
|
|
|
+ "metadata": {},
|
|
|
+ "source": [
|
|
|
+ "# Optimizing Laptop Promotion"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 5,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [],
|
|
|
+ "source": [
|
|
|
+ "import csv \n",
|
|
|
+ "\n",
|
|
|
+ "class Inventory(): \n",
|
|
|
+ " \n",
|
|
|
+ " def __init__(self, csv_filename):\n",
|
|
|
+ " with open(csv_filename) as f: \n",
|
|
|
+ " reader = csv.reader(f)\n",
|
|
|
+ " rows = list(reader)\n",
|
|
|
+ " self.header = rows[0] \n",
|
|
|
+ " self.rows = rows[1:]\n",
|
|
|
+ " for row in self.rows: \n",
|
|
|
+ " row[-1] = int(row[-1])\n",
|
|
|
+ " self.id_to_row = {} \n",
|
|
|
+ " for row in self.rows: \n",
|
|
|
+ " self.id_to_row[row[0]] = row\n",
|
|
|
+ " self.prices = set() # step 1\n",
|
|
|
+ " for row in self.rows: # step 2\n",
|
|
|
+ " self.prices.add(row[-1])\n",
|
|
|
+ " \n",
|
|
|
+ " def get_laptop_from_id(self, laptop_id):\n",
|
|
|
+ " for row in self.rows: \n",
|
|
|
+ " if row[0] == laptop_id:\n",
|
|
|
+ " return row\n",
|
|
|
+ " return None \n",
|
|
|
+ " \n",
|
|
|
+ " def get_laptop_from_id_fast(self, laptop_id): \n",
|
|
|
+ " if laptop_id in self.id_to_row: \n",
|
|
|
+ " return self.id_to_row[laptop_id]\n",
|
|
|
+ " return None\n",
|
|
|
+ "\n",
|
|
|
+ " def check_promotion_dollars(self, dollars): \n",
|
|
|
+ " for row in self.rows: \n",
|
|
|
+ " if row[-1] == dollars:\n",
|
|
|
+ " return True\n",
|
|
|
+ " for row1 in self.rows: \n",
|
|
|
+ " for row2 in self.rows:\n",
|
|
|
+ " if row1[-1] + row2[-1] == dollars:\n",
|
|
|
+ " return True\n",
|
|
|
+ " return False \n",
|
|
|
+ " \n",
|
|
|
+ " def check_promotion_dollars_fast(self, dollars): # step 3\n",
|
|
|
+ " if dollars in self.prices: # step 4\n",
|
|
|
+ " return True\n",
|
|
|
+ " for price in self.prices: # step 5\n",
|
|
|
+ " if dollars - price in self.prices:\n",
|
|
|
+ " return True\n",
|
|
|
+ " return False # step 6"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 6,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "name": "stdout",
|
|
|
+ "output_type": "stream",
|
|
|
+ "text": [
|
|
|
+ "True\n",
|
|
|
+ "False\n"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "source": [
|
|
|
+ "inventory = Inventory('laptops.csv') # step 7\n",
|
|
|
+ "print(inventory.check_promotion_dollars_fast(1000)) # step 8\n",
|
|
|
+ "print(inventory.check_promotion_dollars_fast(442)) # step 9"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "markdown",
|
|
|
+ "metadata": {},
|
|
|
+ "source": [
|
|
|
+ "# Comparing Performance"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": 12,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "name": "stdout",
|
|
|
+ "output_type": "stream",
|
|
|
+ "text": [
|
|
|
+ "0.7781209945678711\n",
|
|
|
+ "0.0003719329833984375\n"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "source": [
|
|
|
+ "prices = [random.randint(100, 5000) for _ in range(100)] # step 1\n",
|
|
|
+ "\n",
|
|
|
+ "inventory = Inventory('laptops.csv') # step 2\n",
|
|
|
+ "\n",
|
|
|
+ "total_time_no_dict = 0 # step 3\n",
|
|
|
+ "for price in prices: # step 4\n",
|
|
|
+ " start = time.time() # step 4.1\n",
|
|
|
+ " inventory.check_promotion_dollars(price) # step 4.2\n",
|
|
|
+ " end = time.time() # step 4.3\n",
|
|
|
+ " total_time_no_dict += end - start # step 4.4\n",
|
|
|
+ " \n",
|
|
|
+ "total_time_dict = 0 # step 5\n",
|
|
|
+ "for price in prices: # step 6\n",
|
|
|
+ " start = time.time() # step 6.1\n",
|
|
|
+ " inventory.check_promotion_dollars_fast(price) # step 6.2\n",
|
|
|
+ " end = time.time() # step 6.3\n",
|
|
|
+ " total_time_dict += end - start # step 6.4\n",
|
|
|
+ " \n",
|
|
|
+ "print(total_time_no_dict) # step 7\n",
|
|
|
+ "print(total_time_dict)"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "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.7.4"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "nbformat": 4,
|
|
|
+ "nbformat_minor": 2
|
|
|
+}
|