Problem 1: The Weird Widget Company of New York has several stores in New York. Each evening, the corporate office in midtown Manhattan receives sales figures from each store in a list of tuples. Each tuple contains two elements, (store, sales).
Write a program that prints the total sales and the average sales for the day. Use a for loop to calculate the total sales and the average sales (try not to use any external libraries!).
Example:
daily_sales = [(‘A',150234.22),('B',73232.90),('C',110493.29),('D',
231965.64),('E',66398.58)] Your program should print:
Total sales today: $632324.63
Average sales today: $126464.93
What you need:
1. set total_sales to 0
2. for: use a for loop to iterate through the daily_sales list adding the second value of each tuple to total_sales at each iteration
3. len: after the end of the loop, calculate the average. Use the len function to figure out the number of data items (for the average)
4. print formatting: print the results. You’ll need to format the average to two decimal places and add the $ sign in the front of the numbers.
Problem 2: The corporate office of the Weird Widget Company of New York also has data on budgeted sales for each store. Budgeted Sales data is stored in a dict with store identifiers as the key. Write a program that prints out the store identifiers of stores that have underperformed or over performed their budget by more than x%.
Example:
daily_sales = [(‘A’,150234.22),('B',73232.90),('C',110493.29),('D',
231965.64),('E',66398.58)]
budgeted_sales = {‘A’:140296.00,’B’:103981.00,’C’:80452.00,’D':
200900.00,'E':90000.00}
performance_threshold = 20
Your program should print:
Store
B
over
or
underperformed
its
budget
Store
C
over
or
underperformed
its
budget
Store
E
over
or
underperformed
its
budget
What you need:
1. for: a for loop to iterate over the daily_sales list
2. len: for the number of tuples in daily_sales
3. if: to compare percent over or under budget for each store
4. abs: since we’re interested in the magnitude of performance difference and are ignoring the sign
Problem 3: An equity hedge fund stores its portfolio of equities in a list of tuples. Each tuple contains four elements: the ticker, number of shares, cost basis, and the current price. You are hired to help
the fund analyze its portfolio and you decide to write a function that returns useful information. You need to do the following:
1. Write a function under_water that takes a tuple (ticker,shares,cost,price) as an argument and returns True if the position is losing money and False otherwise.
2. Write a function above_water that takes a tuple (ticker,shares,cost,price) as an argument and returns False if the position is under water and True otherwise.
3. Write a function get_data that takes two arguments, a portfolio x and a function y. The function returns two values: the number of items in x for which the function y returns True and the total unrealized profit on the portfolio
4. A position is a losing position if:
shares * (price - cost) < 0
Example:
portfolio = [(‘AAPL',-100,110.33,93.79),('IONS',700,11.22,33.33), ('GS',400,189.72,150.70),('SBUX',300,44.73,53.72)]
under_water((‘AAPL’,-100,110.33,93.79)) should return False under_water((‘GS’,400,189.72,150.70)) should return True
above_water((‘AAPL’,-100,110.33,93.79)) should return True above_water((‘GS’,400,189.72,150.70)) should return False
get_data(portfolio,under_water) should return (1, 4219.999999999996)
get_data(portfolio,above_water) should return (3, 4219.999999999996) What you need:
1. def: You need to write two functions. Make sure you use return and you don’t need to print anything.
2. pass a function as an argument: Look at the example we did in class on how to pass functions as arguments and then how to use the function once you’ve passed it.
3. return x,y: A function can return more than one value. Just separate the values using commas.