Session 2 Assessment - Google Forms

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

6/25/24, 4:17 PM Session 2 Assessment - Fundamentals of Python and NumPy

Session 2 Assessment - Fundamentals of


Python and NumPy
While you're attempting this assignment, keep the following in mind -

1. Effort counts more than the answer. Make sure you comment your code to explain
what you are doing
2. This assignment is a series of questions that help you finally build one large chunk of
code that does something cool :). At each stage, make sure to copy your code into a
new cell so I can follow your progress.
3. Make sure that your email and phone number are the same ones that you provided
while registering.
4. Have fun! This assignment is meant to keep you interested in the topic. Remember
that we'll be covering a lot more in the following lectures.
5. The deadline for this assignment is Sunday, 23 June

Some helpful resources -

GeeksForGeeks - https://www.geeksforgeeks.org/python-programming-language-
tutorial/
W3Schools - https://www.w3schools.com/python/default.asp
FreeCodeCamp - https://www.freecodecamp.org/news/learning-python-from-zero-to-
hero-120ea540b567 (My personal favourite)
And of course, the Python Docs - https://docs.python.org/3/tutorial/introduction.html

* Indicates required question

1. Email *

2. Name *

3. Email *

https://docs.google.com/forms/d/17DHfyUvJF-7R34IIYQI8CZm9BL17EDlNBk03qRjGOfU/edit 1/11
6/25/24, 4:17 PM Session 2 Assessment - Fundamentals of Python and NumPy

4. Phone Number *

5. 1. Identity Matrix
Before we start with the main project question, do make sure to try out the identity matrix
question from the session that we skipped. Write a function that takes a parameter N
denoting the matrix size, and return an NxN identity matrix. Note, that this is optional to
attempt, but I would highly recommend you give it a try. Submit the link to your Colab
notebook (make sure it's publicly viewable).

2. Quiz Questions
In this project, we will be attempting to write code that performs vector operations. After each
question, make sure to copy your code into a new cell so that I can follow your progress, and see
if, and where you got stuck. At the end of this assignment, you should have a program that is
capable of doing arithmetic on matrices!

1. Setting up the Prompt


First off, write an infinite loop that presents the user with the following options -
1. Store A
2. Store B
3. Display A
4. Display B
5. Add: A + B
6. Subtract: A - B
7. Dot Product: A · B
8. Quit

Then, allow the user to input a number. If the number is from 1-7, then print the option. If the
number is 8, exit the loop. If the number is anything else, print an error message - "Invalid Choice!",
and continue.

Hint: You might find something useful here.

https://docs.google.com/forms/d/17DHfyUvJF-7R34IIYQI8CZm9BL17EDlNBk03qRjGOfU/edit 2/11
6/25/24, 4:17 PM Session 2 Assessment - Fundamentals of Python and NumPy

Sample Output:

https://docs.google.com/forms/d/17DHfyUvJF-7R34IIYQI8CZm9BL17EDlNBk03qRjGOfU/edit 3/11
6/25/24, 4:17 PM Session 2 Assessment - Fundamentals of Python and NumPy

https://docs.google.com/forms/d/17DHfyUvJF-7R34IIYQI8CZm9BL17EDlNBk03qRjGOfU/edit 4/11
6/25/24, 4:17 PM Session 2 Assessment - Fundamentals of Python and NumPy

2. Reading the Vectors


Now, lets write functions to store the values of vectors A and B. For simplicity, let us assume A
and B are 10-dimensional vectors. So, first create two global variables called vector_A and
vector_B and initialise these to 10 element long arrays with each value as 0. Define a function
called store_A(), which takes in user input, reads the numbers from it, and stores it as a list in
vector_A. Naturally, this list would have 10 elements. Do the same for vector B, in a function called
store_B(). Call these functions, if the corresponding input is entered in the infinite loop.

Note: For all examples from now on, we shall assume that A is [1, 2, 3, 4, 5, 5, 4, 3, 2, 1] and B is [-1,
1, 0, 2, 1, 3, 2, 4, 3, 5].

Hint: Take a look at the split() method to generate lists. Also, remember that inputs are strings,
and we need integers.

https://docs.google.com/forms/d/17DHfyUvJF-7R34IIYQI8CZm9BL17EDlNBk03qRjGOfU/edit 5/11
6/25/24, 4:17 PM Session 2 Assessment - Fundamentals of Python and NumPy

Sample Output

3. Displaying the Vectors


Now, write functions display_A() and display_B() that print out the vectors A and B respectively
when called. Call these when choices 3 and 4 are selected.

https://docs.google.com/forms/d/17DHfyUvJF-7R34IIYQI8CZm9BL17EDlNBk03qRjGOfU/edit 6/11
6/25/24, 4:17 PM Session 2 Assessment - Fundamentals of Python and NumPy

Sample Output

4. Addition and Subtraction!


Right! Let's get down to some arithmetic. Write two functions, add() and subtract() that perform A
+ B and A - B. Make sure to call these when the corresponding option is entered.

https://docs.google.com/forms/d/17DHfyUvJF-7R34IIYQI8CZm9BL17EDlNBk03qRjGOfU/edit 7/11
6/25/24, 4:17 PM Session 2 Assessment - Fundamentals of Python and NumPy

Sample Output - Addition

Sample Output - Subtraction

https://docs.google.com/forms/d/17DHfyUvJF-7R34IIYQI8CZm9BL17EDlNBk03qRjGOfU/edit 8/11
6/25/24, 4:17 PM Session 2 Assessment - Fundamentals of Python and NumPy

5. Guess What's Next


We now have only one remaining operation - the beautiful dot product! Implement this in a
function called dot_product() and make sure this is called when the corresponding option is
chosen.

Sample Output

6. Going Further
We've made a basic vector arithmetic calculator. Congratulations! Give yourself a pat on the back
and feel free to play around with it.

Now, how can we improve this calculator? Right now, it only works in 10-dimensional space. Could
you add an option that sets the number of dimensions to be used for all vectors? Keep in mind
that, if in the middle of the program, the number of dimensions is changed, then vectors A and B
must be reset to some default value of the specified dimensions. Good luck!

6. Provide the link to your Google Colab notebook here. Make sure it's publicly *
viewable.

NumPy Section
For the following questions, submit your Google Colab links (make sure they're publicly viewable)

https://docs.google.com/forms/d/17DHfyUvJF-7R34IIYQI8CZm9BL17EDlNBk03qRjGOfU/edit 9/11
6/25/24, 4:17 PM Session 2 Assessment - Fundamentals of Python and NumPy

7. 1. Create a 2D array of shape (5,5) with values ranging from 1 to 50 with a step of *
2. Find the sum of all numbers in this array. Find the cumulative sum of the array.
Then find the cumulative sum along axis = 1.

8. 2. Create two 3x3 matrices with positive integers of your choice. Perform matrix *
addition and multiplication of the two matrices. After that give transpose of each of
the matrices.

9. 3. Create a 3D array of shape (2, 3, 4) with random integers between 1 and 20. *
Slice to get a subarray of shape (2, 2, 2) from the first two rows and first two
columns of each 2D array, then reshape the subarray into a 2x4 matrix. Calculate
mean, variance, and standard deviation of the resulting matrix.

10. 4. Create a user defined function (a function that the coder created by himself) *
and take in a user defined input: a Numpy array called *height*.
*height* is a one dimensional array height of length n. There are n vertical lines
drawn such that the two endpoints of the ith line are (i,0) and (i,height[i]).
Find two lines that together with the x axis form a container, such that the
container contains the most water.
Return the maximum amount of water a container can store.
Note you may not slant the container

This content is neither created nor endorsed by Google.

Forms

https://docs.google.com/forms/d/17DHfyUvJF-7R34IIYQI8CZm9BL17EDlNBk03qRjGOfU/edit 10/11
6/25/24, 4:17 PM Session 2 Assessment - Fundamentals of Python and NumPy

https://docs.google.com/forms/d/17DHfyUvJF-7R34IIYQI8CZm9BL17EDlNBk03qRjGOfU/edit 11/11

You might also like