Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

NPTEL The Joy of Computing using Python Jan-2024 Week 12 | Swayam

NPTEL The Joy of Computing using Python Jan-2024 Week 12 | Swayam

 


1. Programming Assignment | Week 12

Due on 2024-04-18, 23:59 IST

The Pearson correlation coefficient is a measure of association between two sets of data. In this problem, the data shall be represented as vectors (lists) X and Y. The formula for computing the coefficient is given below:

Xi corresponds to the element X[0] in the list X. We have used zero-indexing here.


Write a function named pearson that accepts two vectors X and Y as arguments and returns the Pearson correlation coefficient between them.

To help you with the computation, some functions have already been defined in the prefix code. You can write the entire function by just using the pre-defined functions. This is an important exercise that will help you in future projects when you start working with libraries.


You do not have to accept the input from the user or print output to the console. You just have to write the function definition.

Your last recorded submission was on 2024-04-05, 21:58 IST
Select the Language for this assignment. 
1
def f(P):
2
    mean = sum(P) / len(P)
3
    return [p - mean for p in P]
4
5
def g(P, Q):
6
    return sum(P[i] * Q[i] for i in range(len(P)))
7
8
def h(x):
9
    return x ** 0.5
10
def pearson(X, Y):
11
  return g( f(X), f(Y) ) / ( h(g( f(X), f(X) ) ) * h(g( f(Y), f(Y) ) )  )
0
X = [float(x) for x in input().split()]
1
Y = [float(y) for y in input().split()]
2
print(f'{pearson(X, Y):.2f}')
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1 2 3 4 5 6 7 8 9
1 2 3 2 3 4 3 4 5
0.89
0.89\n
Passed after ignoring Presentation Error
Test Case 2
1 2 3 4 5 6 7 8 9 10
-1 -2 -3 -4 -5 -1 -2 -3 -4 -5
-0.49
-0.49\n
Passed after ignoring Presentation Error




2. Programming Assignment | Week 12

Due on 2024-04-18, 23:59 IST

A square metal plate in 2D space is the setup we are going to work with. The spatial extent of the metal plate is given by:

0 ≤ x,≤ 5

The temperature at any point (x,y) on the plate is given by the following equation. The temperature is measured in Celsius and can be negative:

f(x,y) = 30 + x2 + y2 − 3− 4y

A micro-organism lives on the surface of the metal plate. It occupies only those points on the plate where both the coordinates are integers. The organism cannot survive in high temperatures and instinctively moves to regions of low temperature that are less or equal to a threshold T. If no such region is found, it can't survive. The terms high and low are used in a relative sense and should be compared with respect to the threshold.


Write a function named survival that accepts the value of T as argument and returns True if the organism can survive on the metal plate, and False otherwise.


You do not have to accept input from the user or print the output to the console. You just have to write the function definition.

Your last recorded submission was on 2024-04-05, 22:04 IST
Select the Language for this assignment. 
1
def survival(T):
2
    """
3
    Determine if the organism will survive or not
4
    
5
    Argument:
6
        T: integer
7
    Return:
8
        result: bool
9
    """
10
    for x in range(6):
11
      for y in range(6):
12
        if 30 + x**2 + y**2 - 3*x - 4*y <= T:
13
          return True
14
      
15
    return False
16
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
30
True
True\n
Passed after ignoring Presentation Error
Test Case 2
10
False
False\n
Passed after ignoring Presentation Error




3. Programming Assignment | Week 12

Due on 2024-04-18, 23:59 IST

Write a function named std_dev that accepts a list of real numbers X as argument. It should return the standard deviation of the points given by the following formula:

Here, Xi​ refers to the element X[i] in the list and X refers to the arithmetic mean of the numbers in X. Try to use list-comprehension wherever possible. However, we won't be evaluating you on this.


You do not have to accept the input from the user or print output to the console. You just have to write the function definition.

Your last recorded submission was on 2024-04-05, 22:12 IST
Select the Language for this assignment. 
1
def std_dev(X):
2
    """
3
    Compute the standard deviation
4
    
5
    Argument:
6
        X: list of integers
7
    Return:
8
        sigma: float
9
    """
10
    mean = sum(X) / len(X)
11
    ad = [p - mean for p in X]
12
    return ( sum(ad[i] * ad[i] for i in range(len(ad))) / (len(X)-1) )** 0.5
0
X = [float(x) for x in input().split(',')]
1
sigma = std_dev(X)
2
print(f'{sigma:.2f}')
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1,2,3,4,5,6,7,8,9
2.74
2.74\n
Passed after ignoring Presentation Error
Test Case 2
124,1124,-1342,-214,-153,-215,-15
721.94
721.94\n
Passed after ignoring Presentation Error




No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.