Created
May 29, 2018 02:30
-
-
Save jrjames83/ad5a29f8e9afb16543733a5e38135775 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# You have some inventory figures (stock, units in stock)\n", | |
| "# You also have cost data for each product, eg a shirt costs you $8.75\n", | |
| "# You also have the price you sell it for, eg socks are $3.25\n", | |
| "\n", | |
| "stock = {\"shirts\": 100, \"pants\": 50, \"socks\": 275, \"belts\": 450}\n", | |
| "cost = {\"shirts\": 8.75, \"pants\": 14.50, \"socks\": 2.65, \"belts\": 4.50}\n", | |
| "retail = {\"shirts\": 18.75, \"pants\": 39.50, \"socks\": 3.25, \"belts\": 6.90}" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "#A Find the cheapest product based on your cost\n", | |
| "#B Find the most expensive product based on your cost\n", | |
| "#C Find the product with the highest gross margin (retail divided by cost)\n", | |
| "#D Find the revenue for each product if you sell out\n", | |
| "#E If you could only sell out to products, which products would you sell to maximize gross profit?\n", | |
| "#F Create a new dict for stock, based on whether the prod is an accessory (belt, stock)\n", | |
| " # or it's a shirt/pant" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "('socks', 2.65)" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# A \n", | |
| "min(cost.items(), key=lambda x: x[1]) " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "('pants', 14.5)" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# B\n", | |
| "max(cost.items(), key=lambda x: x[1]) # :) " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[('shirts', 2.142857142857143),\n", | |
| " ('pants', 2.7241379310344827),\n", | |
| " ('socks', 1.2264150943396226),\n", | |
| " ('belts', 1.5333333333333334)]" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# C - step 1 (read up on dict.items() if you need to)\n", | |
| "[ (k, retail[k] / cost[k] ) for k,v in stock.items() ]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "('pants', 2.7241379310344827)" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# C - step 2 (read up on dict.items() if you need to)\n", | |
| "max([ (k, retail[k] / cost[k] ) for k,v in stock.items() ], key=lambda x: x[1])\n", | |
| "# So pants have the largest mark-up" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "pants 2.7241379310344827\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# C - perhaps a more intuitive approach, but the above is what you'll see in the wilds\n", | |
| "# of stackoverflow / reality\n", | |
| "\n", | |
| "best_margin = 0\n", | |
| "best_prod = \"\"\n", | |
| "\n", | |
| "# Check out all our products, grab their retail, cost, make margin \n", | |
| "# set a value called current best margin, and always update if \n", | |
| "# a value is found that exceeds the current best\n", | |
| "\n", | |
| "for item, _ in stock.items():\n", | |
| " retail_ = retail[item]\n", | |
| " cost_ = cost[item]\n", | |
| " current_best_margin = retail_ / cost_\n", | |
| " # Add print statements if you're not sure how the variables are working here!\n", | |
| " if current_best_margin > best_margin:\n", | |
| " best_margin = current_best_margin\n", | |
| " best_prod = item\n", | |
| "\n", | |
| "print(best_prod, best_margin)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'belts': 3105.0, 'pants': 1975.0, 'shirts': 1875.0, 'socks': 893.75}" | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# D - revenue from each product if you sell out.....\n", | |
| "\n", | |
| "# use a list comp to get a list of quantity * retail price\n", | |
| "{k: (retail[k] * stock[k]) for k,v in stock.items() }" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[('pants', 1250.0), ('belts', 1080.0000000000002)]" | |
| ] | |
| }, | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\n", | |
| "# 2 products to maximize gross profit - just gonna copy, paste above and tweak\n", | |
| "# Sneaky! https://stackoverflow.com/questions/7197315/5-maximum-values-in-a-python-dictionary\n", | |
| "from collections import Counter\n", | |
| "\n", | |
| "Counter({k: ( (retail[k] - cost[k]) * stock[k]) for k,v in stock.items() }).most_common(2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'accessories': 725, 'regular': 150}" | |
| ] | |
| }, | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# F - Even if you take an hour to review the below, it'll be well worth your time\n", | |
| "# Maybe we'll work this out in class too!\n", | |
| "\n", | |
| "new_stock_acc = {k:v for k,v in stock.items() if k in [\"belts\", 'socks']}\n", | |
| "new_stock_acc = {\"accessories\": sum(list(new_stock_acc.values()))}\n", | |
| "\n", | |
| "new_stock_regular = {k:v for k,v in stock.items() if k not in [\"belts\", 'socks']}\n", | |
| "new_stock_regular = {\"regular\": sum(list(new_stock_regular.values()))}\n", | |
| "\n", | |
| "# New dict merging syntax, python 3.5+ \n", | |
| "merged = {**new_stock_acc, **new_stock_regular}\n", | |
| "merged" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "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.6.1" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment