Reference no: EM132389010
GRA 41421 Data Management and Python Programming
Question 1
Problem 1.
Write a program that asks the user to enter three numbers (use three separate input statements). Create variables called total and average that hold the sum and average of the three numbers and print out the values of total and average.
Problem 2.
Write a program that generates a list of 50 random integers such that the first integer number is between 1 and 2 (including 2), the second is between 1 and 3, the third is between 1 and 4, . . . , and the last is between 1 and 51.
Problem 3
Write a function that asks the user to enter a positive integer , and then computes (1+(1/2)+(1/3)+.....+(1/n))-ln(n). Plot the the value of your function for different values of n=10,100,500,1000,10000.
Problem 4.
Write a function that prints out whether a certain word contains any vowels and count the number of vowels. For example, you can test your function with the following words "Antidisestablishmentarianism" and "Menneskerettighetsorganisasjonene".
Problem 5
At a certain school, student's email addresses end with @student.college.edu, while professor's email addresses end with @prof.college.edu. Write a program that first asks the user how many email addresses she/he will be entering, and then asks the user to enter those addresses. After all the email addresses are entered, the program should print out a message indicating how many students and professor's have been entered.
Problem 6
Companies often try to personalize their offers to make them more attractive. One simple way to do this is just to insert the person’s name at various places in the offer. Of course, companies don’t manually type in every person’s name; everything is computer-generated. Write a program that asks the user for a name and then generates an offer like the one below.
Enter name: Steve Jobs
Dear Steve Jobs,
I am pleased to offer you our new Platinum Plus Rewards card at a special introductory APR of 47.99 percent. Steve, an offer like this does not come along every day, so I urge you to call now toll-free at 1-800-314-1592. We cannot offer such a low rate for long, Steve, so call right away.
Problem 7
Rewrite the following nested loop as a list comprehension
ans = []
for i in range(10):
for j in range(40):
if (i%3==0) and (j%2==0) and (i>j):
ans.append((i, j))
print(ans)
Problem 8
Suppose we want to create an output dictionary which contains only the odd numbers that are present in the input list (see below) as keys and their cubes as values. Show how to do this using for loops and dictionary comprehension.
To test your function, consider the following list :
np.random.seed(100)
list_integer = np.random.randint(0, 1000,size= 200)
Question 2
1. Create a function that inputs a word (string) and returns True (boolean) when there is no repeated letter in the word. For example, the word "troublemakings" should return a True value, and the word "sheet" should return a False.
2. Read the text file ShakespeareSonets.txt which contains an extract of THE SONNETS by William Shakespeare in text format. Remove the punctuation marks (such as ",", ".", ":", "'", "?", etc.), and also remove the numbers from the text (such as "1", "2", etc).
3. Select from the text those words which do not contain a repeated letter. Compute the length of each word and plot a histogram with the frequency of these words' length in the text.
Question 3
Read the data data_ECommerce_sample.csv . The data contains information about customers shopping using a web platform.
1. Show the type and number of observations for each variable.
2. Drop observations for which CustomerID is missing. Name the dataframe as ec
3. Change the column 'InvoiceData' into a datetime . Create a month variable and compute the total monthly quantity sold by the platform.
4. Compute the revenue per observation (call it Revenue ) as the product of Quantity and UnitPrice . Display the ten highest invoice orders in terms of revenue.
5. Compute the highest five invoice orders in terms of revenue for the UK, Germany, and France.
Question 4
We are going to use three datasets for this question. The description of the variables is as follows:
The dataset national_exam_5th_year_level_grade.csv contains the grade of the 5th-year National Exam in Norway.
Variable Description
Unnamed: 0 School code
2015_English_Boy English grade for boys in year 2015
2015_English_girl English grade for girls in year 2015
... ...
The dataset national_exam_5th_year_level_school_info.csv contains information on schools' location in Norway.
Variable Description
county code County code
municipal code Municipality code
county County name
municipal Municipality name
public True if the school is public and False if the school is private
school School name
school code School code
The dataset income_nor.csv contains average household income for each municipality in Norway.
Variable Description
municipal code Municipality code
2017 Average household income of each municipality
1. Load the data national_exam_5th_year_level_grade.csv as a dataFrame df . Rename the first column as school_code and set it as the row index.
2. Create a hierarchical index for the columns. The index levels should be year, subject, and gender.
3. Show a table with average grades for the whole country by gender and subject in 2017.
4. Select Math and English grades for boys and girls for the year 2017, and call it dfs . Plot an histogram for each grade in a two-by-two figure (Please label the x- and y-axis and show the title for each subplot).
5. Load national_exam_5th_year_level_school_info.csv as a dataFrame dfk . You may notice that there are some strange strings in the column of county, municipal and school. Please replace those values ["¿","\x8c","¾","\x9f","¯","\x81","\x87","\x01"] with ["o", "aa","ae","u","o","aa","a","s"], respectively.
6. Merge DataFrames dfk with dfs on school_code . Call the merged dataset dfall .
7. Remove the schools with missing grade values and the municipalities with less than five schools.
8. Compute the mean of test grades for each municipality by subject and gender. Call teh dataframe mean_grade . Plot a bar chart for the ten municipalities with the highest Math scores for boys.
9. Load income_nor.csv as a dataFrame df_income . Keep the observations if the year is 2017. Set 'municipal code' as the index.
10. Merge the DataFrame income with mean_grade .
11. Plot the municipality's income against the average grade of boys and girls in Math using a scatter plot. Please show the legend and label the axis and title.
12. Run two regressions for boys and girls' Math grade on income. where grade is the average math for municipality , and is the average household income
gradei = α0 + α1 × incomei
where grade is the average math for municipality i, and is the average household income.