1. Assignment-1#
1.1. Total number of points: 50#
1.2. Due date: 9/15/2022 11:59 PM#
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.
Steps to evaluate your solutions:
Step-1: Try on Colab or 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 homework’s .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”
Step-6: Go to lms.rpi.edu and upload your homework at the appropriate link to submit this homework.
1.4. Q1[5 points]. Assign the value for m to 120. Set the value for n to 100 times m , and set the value for p to m times by n^2 (square of n).#
#Assign m ; n; p
# YOUR CODE HERE
raise NotImplementedError()
#Don't change this cell.
assert m==120
assert n==100*120
assert p==120*(12000**2)
1.5. Q2-1 [5 points]:#
Create an empty list
g1Create a list
g2with integers 1, 2, 3Create a nested list
g3withg1andg2Create a list
g4with only4repeated 3 times.Create a list
g5with your firstName and lastName as two separate strings – for example, g5 that the President would build would be [‘joe’, ‘biden’]
#Need to create lists according to the instructions
# YOUR CODE HERE
raise NotImplementedError()
#Don't change this cell.
assert len(g1) == 0
assert set(g2) == {1,2,3}
assert len(g3) == 2
assert len(set(g4)) == 1
#Don't change this cell.
1.6. Q2-2 [5 points]:#
1.6.1. Note that all are integers in this list. Please follow the exact order of execution here below from first line to the last line#
Create a list
s1with values in an order – 10, -10, 20, -20Append a new element
30to lists1Insert a new element
-30at position2Insert a new element
15at position20(Yes it is 20 – check what happens)
#Need to create li1 according to the instructions above
# YOUR CODE HERE
raise NotImplementedError()
#Don't change this cell.
assert len(s1)==7
assert s1[2]==-30
#Don't change this cell.
1.7. Q3-1 [5 points]#
1.7.1. Create a dictionary p1 with 5 keys 1, 2, 3, 4, 5 and their corresponding values as 2, 4, 6, 8, 10 respectively.#
#Creating the dictionary p1
# YOUR CODE HERE
raise NotImplementedError()
#Don't change this cell.
assert len(p1)==5
assert p1[3]==6
assert p1[5]==10
#Don't change this cell.
1.8. Q3-2 [5 points]#
1.8.1. Create a dictionary p2 with 5 keys 1, 2, 3, 4, 5 and their corresponding values as 2, 4, 6, 8, 10 respectively.#
1.8.2. Add a new key-value pair to p2 with key as 6 and value as 12#
#Creating the dictionary p2 and add a new key-value pair
# YOUR CODE HERE
raise NotImplementedError()
#Don't change this cell.
assert len(p2)==6
#Don't change this cell.
1.9. Q3-3 [5 points].#
Create a list
p31with numbers 1, 2, 3Create another list
p32with strings ‘a’, ‘b’, ‘c’Create a dictionary
d3where length ofp31is the key andp32is the valueCreate a list
p33that contains keys of dictionaryd3
#Creating l31 and l32 and follow the instructions to create dictionary d3
# YOUR CODE HERE
raise NotImplementedError()
#Don't change this cell.
assert len(p31)==3
assert set(p32)=={'a', 'b','c'}
assert len(d3)==1
assert len(p33)==1
#Don't change this cell.
1.10. Q4-1 [5 points].#
Create a set
S1with values 1, 2, 3 in the same order as stated.Assign the variable
Slenas the length of this setS1Create another set
S2with values 4, 5, 6 in the same order as stated.Create a new set
S3by merging setsS1andS2
#Please follow the above instructions to create S1, S2, S3
# YOUR CODE HERE
raise NotImplementedError()
#Don't change this cell.
assert Slen==3
assert 1 in S1
assert S3=={1, 2, 3, 4, 5, 6}
#Don't change this cell.
1.11. Q4-2 [5 points]#
Create a set
S4with values 1, 2, 3, 4, 5 passed as stringsAdd another string
'6'toS4Convert each element in S4 which is a string to integer and save this new set as
S5
# YOUR CODE HERE
raise NotImplementedError()
#Don't change this cell.
assert set(S4)=={'1', '2', '3', '4', '5','6'}
assert S5=={1, 2, 3, 4, 5, 6}
1.12. Q5 [5 points]. Write a function ReturnList that takes a number num (where num is >0) and returns a list of numbers from 0 to num (inclusive). If number num<=0 return [-1].#
def ReturnList(num):
# YOUR CODE HERE
raise NotImplementedError()
#Don't change this cell.
assert ReturnList(3)==[0, 1, 2, 3]
assert ReturnList(4)==[0, 1, 2, 3, 4]
assert ReturnList(6)==[0, 1, 2, 3, 4, 5, 6]
#Don't change this cell.
1.13. Q6 [5 points]. Write a function RepeatNums that takes two numbers num1 and num2. This function should return a list of numbers, where the length of the list is num2.#
1.13.1. All the numbers in this list should be equal to num1. In other words, create a list that contains num1 that is repeated num2 number of times.#
1.13.2. If num1 <= 0 or num2 <= 0 return [-1]#
def RepeatNums(num1, num2):
# YOUR CODE HERE
raise NotImplementedError()
#Don't change this cell.
assert RepeatNums(3,2)==[3, 3]
assert RepeatNums(2,4)==[2, 2, 2, 2]
assert RepeatNums(6,1)==[6]
#Don't change this cell.