Skip to content

Instantly share code, notes, and snippets.

@johnmcc
Created April 21, 2018 12:31
Show Gist options
  • Save johnmcc/d4a0cc321eb6d841bdc803159853b7d1 to your computer and use it in GitHub Desktop.
Save johnmcc/d4a0cc321eb6d841bdc803159853b7d1 to your computer and use it in GitHub Desktop.

OOP Lab

Acme, Inc. has provided you with a CSV file containing the following fields for a number of orders:

  • region
  • country
  • item_type
  • sales_channel
  • order_id
  • units_sold (int)
  • unit_price (float)
  • unit_cost (float)
  • total_revenue (float)
  • total_cost (float)
  • total_profit (float)

They would like you to process this data to provide them with a number of insights.

Use OOP to find the result of:

  1. The total number of units sold across all orders
  2. The total amount of profit across all orders
  3. The number of online orders that were made in Hungary
  4. The order_id of the order that generated the highest profit (Harder!)

Extension

In addition, they would like you to generate a dictionary where each key is a region ("Europe", "Australia and Oceania" etc.) and the value is the total profit for that region.

The output should look like this:

{'Central America and the Caribbean': 40988504.32, 'Europe': 85933409.37, 'Asia': 64738894.81, 'Middle East and North Africa': 49885092.50, 'Australia and Oceania': 36740921.27, 'Sub-Saharan Africa': 102699264.75, 'North America': 9457684.50}

Hints

1 & 2) You may use a list comprehension or a loop to generate the result.

3 ) You may use a list comprehension or a loop + conditional here.

4 ) You might be able to use a comprehension here, but it will be much easier to use a loop. Think about how you will track the order with the highest profit throughout the loop.

Extension: You can check if a key exists within a dictionary by using the in keyword. For example:

profits = {}

if "Europe" in profits:
	# This code will execute if profits["Europe"] exists already
else:
	# This code will execute if profits doesn't have a key of Europe yet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment