<jupyter_start><jupyter_text>Homework 2 (solutions)  Problem 1Construct a function, which will ask the user to input several numbers separated by commasand will calculate their average. (e.g. if the user inputs 3,5,7 the function must result in 5).<jupyter_code>inputted_numbers = input("Please, input some numbers seprated by a comma: ")
def simple_mean(inputted_numbers):
    return float( sum(inputted_numbers) ) / float( len(inputted_numbers) )
print simple_mean(inputted_numbers)<jupyter_output>Please, input some numbers seprated by a comma: 10, 12.5, 15, 17.5, 20, 22.5, 25
17.5<jupyter_text>Problem 2Upgrade the function above to also ask the user the number of occurrences to calculate the average for. (e.g. if the user inputs 1,3,5,7,9 and as the second argument to function inputs 2, the function must result in (7+9)/2 = 8. If the user inputs the same numbers as a first argument, but inputs 3 as the 2nd, then the function must result in (5+7+9)/3=7)<jupyter_code>inputted_numbers = input("Please, input some numbers seprated by a comma: ")
occurancy = input("Please, input the number of occurancies to calculate the mean for: ")
def rolling_mean(inputted_numbers,occurancy):
    return float( sum(inputted_numbers[-occurancy:]) ) / float( len(inputted_numbers[-occurancy:]) )
print rolling_mean(inputted_numbers, occurancy)<jupyter_output>Please, input some numbers seprated by a comma: 10, 12.5, 15, 17.5, 20, 22.5, 25
Please, input the number of occurancies to calculate the mean for: 5
20.0<jupyter_text>Problem 3Construct a function, which will generate a random number between 1 and 100 (both included). If this number is between 50 and 100 (not included) the function returns "Win", if it is between 1 and 50 (both included) the function returns "Loss" and if it is exactly 100 then the function returns "Draw". (Hint: to generate a random number, one should first import a packagecalled random (i.e. import random) and then use the function randint from there (e.g. random.randint(x,y), where x=1 and y=100 in our case)<jupyter_code>import random
number=random.randint(1,100)
def generator(number):
    if 100 > number > 50:
        return "Win"
    elif 50 >= number >= 1:
        return "Loss"
    else:
        return "Draw"
print(number)
print generator(number)<jupyter_output>70
Win<jupyter_text>Problem 4Create a list of 3 stocks (choose whatever stocks you want, e.g. ["IBM","AAPL","MSFT"]). Create a for loop that will iterate over the list, get the data of relevant stock and print the first 7 rows of the data.<jupyter_code>import pandas_datareader.data as web
stock_list = ["IBM","AAPL","MSFT"]
for stock in stock_list:
    data = web.DataReader(stock,"google")
    print( data.head(7) )<jupyter_output>Open    High     Low   Close   Volume
Date                                               
2010-01-04  131.18  132.97  130.85  132.45  6155846
2010-01-05  131.68  131.85  130.10  130.85  6842471
2010-01-06  130.68  131.49  129.81  130.00  5605290
2010-01-07  129.87  130.25  128.91  129.55  5840569
2010-01-08  129.07  130.92  129.05  130.85  4197105
2010-01-11  131.06  131.06  128.67  129.48  5731177
2010-01-12  129.03  131.33  129.00  130.51  8083354
             Open   High    Low  Close     Volume
Date                                             
2010-01-04  30.49  30.64  30.34  30.57  123432050
2010-01-05  30.66  30.80  30.46  30.63  150476004
2010-01-06  30.63  30.75  30.11  30.14  138039594
2010-01-07  30.25  30.29  29.86  30.08  119282324
2010-01-08  30.04  30.29  29.87  30.28  111969081
2010-01-11  30.40  30.43  29.78  30.02  115557365
2010-01-12  29.88  29.97  29.49  29.67  148614774
             Open   High    Low  Close    Volume
Date                             [...]<jupyter_text>Problem 5Upgrade the for loop above. Now, instead of printing the data, the for loop should iterate over the list, get the data and plot it.<jupyter_code>import pandas_datareader.data as web
import matplotlib.pyplot as plt
stock_list = ["IBM","AAPL","MSFT"]
for stock in stock_list:
    data = web.DataReader(stock,"google")
    plt.plot(data["Open"])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09266876360996855, 'fraction_numerical': 0.14033389789499154, 'mean_word_length': 3.342436974789916, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '434544', 'license': 'Apache-2.0', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1365, 'n_words': 493, 'path': 'Week 2/Homework_2_solutions.ipynb', 'repo_name': 'HrantDavtyan/Data_Scraping', 'size': '4133'}