Homework 3 Note: Plagiarism Is Serious This Work Should Be F
Homework 3 Note Plagiarism Is Serious This Work Should Be Fini
Use code snippets given, write a function called “def top_prices(prices, n):”, which will take in 2 arguments. First argument is the list of prices, each price should be of a number type (int or float). Second argument ‘n’ indicates top n prices. For example, if n=3, this function should return a new list of the top 3 prices, sorted from low to high. If n=10, this function should return a new list of the top 10 prices, sorted from low to high.
a. This function should not change the original ‘prices’ passed in.
b. This function should return a list, it should not print anything. For debugging purposes, at first we can add printing statements inside, but before submitting please remove all prints. If things don't work at first, try PyCharm Debugger (youtube has a lot of tutorials).
2. Use code snippets given, write a function called “def first_20_stock_prices(symbols, prices)”, which will take 2 input arguments, ‘symbols’ and ‘prices’. Prices is list of prices in float. Write a ‘for’ loop, to print to screen both stock symbols and stock prices, for first 20 stocks, in user friendly way. One example line from print: “Symbols: GE, Stock Price: 16.13”
a. Each symbol and price for this symbol must be on the same line.
b. Price should only keep 2 decimal places, like 16.13.
c. This function should only print, should not return anything.
3. Use code snippets given, write a function “def top_quartile_prices(prices):”, which will take in original ‘prices’ in list of floats, and will print the top quartile prices, in other words, should print the top 25% of the prices, sorted from high to low. It should only care about prices, not symbols.
A user friendly message should be printed, example: “The top quartile prices are: 100.1, 99.2, 98.3…..”, with 1 decimal place for numbers. Note 100.1 is only an example, the real top prices may be different. This function should only print, it should not return anything.
4. Use code snippet given, write a function “def visualize(prices):”, which will do the same thing as in Jupyter Notebook. You can just copy / paste the examples, or if you want to do some tweaks to the x/y axis, or x/y ticks, or title on top, you’re welcome to do so. Exact copy of the given code will get full score for this step. The purpose of this step is mostly about making plot work on your computer.
5. Write a function called “def main()”, that takes in no argument. This ‘main’ will initiate some data, manipulate data to the format we like, then call the 4 functions described above.
a. Initiate the original ‘prices’ and ‘symbols’ in main, starting from ‘prices_str’ in the first line and ‘symbols_str’ in the first few lines in the Jupyter Notebook. Do some data manipulation until you get a list of floats, as ‘prices’ and list of strings as ‘symbols’, without space or “\n” (new line characters) in symbols.
b. In 'main', call ‘top_prices(prices, n)’ twice, using n=5 and n=20 respectively, assign the return value to a variable each time, then write a line to print user friendly message, something like ‘Top 5 stock prices are [5, 6, 7….]’. Please use str.format() formatting for n=5, and f-string formatting for n=20.
c. In 'main', call function ‘first_20_stock_prices(symbols, prices)’. The function itself will print to screen, so we don’t have to print anything for this function inside of 'main'.
d. In 'main', call function ‘top_quartile_prices(prices)’, which will print the result inside of function.
e. In 'main', call function ‘visualize(prices)’, which should draw a graph similar to the one in Jupyter Notebook.
6. Submit python file "stock_yourlastname.py" to Blackboard. Please substitute your last name in file name.