2. [Fall 2022] Homework-2#
2.1. Total number of points: 70#
3. Due date: September 22nd, 2022#
Before you submit this homework, make sure everything runs as expected. First, restart the kernel (in the menu, select Kernel → Restart) and then run all cells (in the menubar, select Cell → Run All). You can discuss with others regarding the homework but all work must be your own.
This homework will test your knowledge on basics of Python. The Python notebooks shared will be helpful to solve these problems.
Steps to evaluate your solutions:
Step-1: Ensure you have installed Anaconda (Windows: https://docs.anaconda.com/anaconda/install/windows/ ; Mac:https://docs.anaconda.com/anaconda/install/mac-os/ ; Linux: https://docs.anaconda.com/anaconda/install/linux/)
Step-2: Open the Jupyter Notebook by first launching the anaconda software console
Step-3: Open the .ipynb file and write your solutions at the appropriate location “# YOUR CODE HERE”
Step-4: You can restart the kernel and click run all (in the menubar, select Cell → Run All) on the center-right on the top of this window.
Step-5: Now go to “File” then click on “Download as” then click on “Notebook (.ipynb)” Please DO NOT change the file name and just keep it as .ipynb file format
Step-6: Go to lms.rpi.edu and upload your homework at the appropriate link to submit this homework.
5. Q1 [10 points]. Write a function that takes a number and checks if it is a composite number. Return True
if its a composite number or else False
.#
A number is composite when it has more than two factors – which means apart from getting divided by 1 and the number itself, it can also be divided by at least one positive integer.
def isComposite(num):
'''Write a program to check if a given number is a composite number or not.
A number is composite if there are more than two factors 1, itself, and atleast one positive integer.
The program returns -- True, if it is a composite number; otherwise False. Assume that you will be receiving only positive integers (>=0) as the input parameters.
'''
# YOUR CODE HERE
raise NotImplementedError()
#Test cell-1 [6 pts]
#DO NOT MODIFY/DELETE THIS CELL
'''case-1'''
number=1
assert isComposite(number) == True
'''case-2'''
number = 10
assert isComposite(number)== True
'''case-3'''
number = 97
assert isComposite(number) == False
#Test cell-2 [2 pts]
#DO NOT MODIFY/DELETE THIS CELL
#2 [2 pts]
#DO NOT MODIFY/DELETE THIS CELL
6. Q2 [5 points]. import the numpy package as np.#
'''Note that ONLY if you get this solution right all the below questions will be executed'''
# YOUR CODE HERE
raise NotImplementedError()
#Test cell-3 [5 pts]
#DO NOT MODIFY/DELETE THIS CELL
assert np.prod([1,2,3])==6
7. Q3 [5 points]. Create a numpy array called arr3
of zeros of size 10 where the sixth element is 1.#
# YOUR CODE HERE
raise NotImplementedError()
#Test cell-4 [3 pts]
#DO NOT MODIFY/DELETE THIS CELL
assert isinstance(arr3, np.ndarray)
assert arr3[:5].all() == 0
assert arr3[5] == 1
#Test cell-5 [2 pts]
#DO NOT MODIFY/DELETE THIS CELL
8. Q4 [5 points]. Create a numpy array arr4
with elements 1,3,5,7,9,11 (in the same order)#
9. Now create a new array arr41
whose elements are twice the value of each element in arr4
at the same index respectively.#
# YOUR CODE HERE
raise NotImplementedError()
#Test cell-6 [4 pts]
#DO NOT MODIFY/DELETE THIS CELL
assert isinstance(arr4, np.ndarray)
assert arr4[2] == 5
assert arr41[2] == 10
assert arr41[1] == 6
#Test cell-7 [1 pts]
#DO NOT MODIFY/DELETE THIS CELL
10. Q5 [10 points]. Return the sum of absolute values of elements that occur maximum number of times and minimum number of times in a numpy array.#
abs() is a function used to return absolute values of a given number. For example, abs(-3) = 3 and abs(3) = 3
if there are two (or more) elements occuring with same frequency and are occurring maximum number of times, choose the element that is smaller in value
if there are two (or more) elements occuring with same frequency and are occurring minimum number of times, choose the element that is smaller in value
def SumMinMax(arr5):
"""Returns the difference between
element that occurs maximum number of times and element that occurs minimum number of times
"""
# YOUR CODE HERE
raise NotImplementedError()
#Test cell-8 [10 pts]
#DO NOT MODIFY/DELETE THIS CELL
arr3=np.array([1,0,1,0,2,1])
assert SumMinMax(arr3)==3
arr3=np.array([0])
assert SumMinMax(arr3)==0
arr3=np.array([1,2,3,1,2,3,1,1,0,0,4])
assert SumMinMax(arr3)==5
arr3=np.array([1,2,3,0])
assert SumMinMax(arr3)==0
import pandas as pd
'''Make sure you run this cell before executing the cells below'''
11. Q6 [5 points]. Please follow the instructions very carefully#
Create a list
l6
and assign values 1, 2, 3, 4, 5, 6 to this list in the same order as listed here.Use
l6
to create a seriesS6
with a default index
# YOUR CODE HERE
raise NotImplementedError()
#Test cell-9 [3 pts]
#DO NOT MODIFY/DELETE THIS CELL
assert len(l6)==6
assert max(l6)==6
assert S6[3]==4
#Test cell-10 [2 pts]
#DO NOT MODIFY/DELETE THIS CELL
12. Q7 [10 points]. This function takes a list l7
as an input and creates 2 more lists (l71
and l72
) by manipulating l7
as follows#
l71 – copy l7 into l71 and add the 1st element of l7 to the end of l71
l72 – copy l71 into l72 and add the mean value of l71 to the end of l72
Now use
l7
,l71
andl72
to create a data framedf
with corresponding column names asfirst
,second
andthird
return this dataframe
df
def createDF(l7):
"""Returns the dataframe that is built using an original list
"""
# YOUR CODE HERE
raise NotImplementedError()
#Test cell-11 [5 pts]
#DO NOT MODIFY/DELETE THIS CELL
'''case-1'''
list7=[2,4,6,8]
assert list(createDF(list7)['second'])[:5]==[2.0,4.0,6.0,8.0,2.0]
'''case-2'''
list7=[2,4,6,8]
assert list(createDF(list7)['first'])[:4]==[2,4,6,8]
#Test cell-12 [5 pts]
#DO NOT MODIFY/DELETE THIS CELL
13. Q8 [10 points]. Read the .csv file from this url and load it into dataframe ‘df’#
Using pandas dataframe operations, output the total number of rows with
survived
==1 andgender
isfemale
asvar81
.Using pandas dataframe operations, output the total number of rows with
survived
==1 andgender
ismale
asvar82
.
url='https://raw.githubusercontent.com/lmanikon/lmanikon.github.io/master/teaching/datasets/titanic.csv'
# YOUR CODE HERE
raise NotImplementedError()
#Test cell-13 [6 pts]
#DO NOT MODIFY/DELETE THIS CELL
assert df['age'].sum()==21205.17
assert df.isnull().sum().sum()==869
assert var82==109
#Test cell-14 [4 pts]
#DO NOT MODIFY/DELETE THIS CELL
14. Q9 [10 points]. Read the .csv file from this url and load it into dataframe df
#
15. Using groupby operation over embark_town
and gender
,#
assign
var9
as the total number of people survived (survived
is 1), where they embarked (embark_town
) atSouthampton
and are female (gender
isfemale
).
url='https://raw.githubusercontent.com/lmanikon/lmanikon.github.io/master/teaching/datasets/titanic.csv'
# YOUR CODE HERE
raise NotImplementedError()
#Test cell-15 [10 pts]
#DO NOT MODIFY/DELETE THIS CELL
assert df['age'].sum()==21205.17
assert df.isnull().sum().sum()==869
assert len(df.columns)==15
assert var9==140