NPTEL Programming, Data Structures And Algorithms Using Python Week-3 August 2023

 

Week 3 Programming Assignment

Due on 2023-08-17, 23:59 IST
Write three Python functions as specified below. Paste the text for all three functions together into the submission window. Your function will be called automatically with various inputs and should return values as specified. Do not write commands to read any input or print any output.
  • You may define additional auxiliary functions as needed.
  • In all cases you may assume that the value passed to the function is of the expected type, so your function does not have to check for malformed inputs.
  • For each function, there are normally some public test cases and some (hidden) private test cases.
  • "Compile and run" will evaluate your submission against the public test cases.
  • "Submit" will evaluate your submission against the hidden private test cases. There are 12 private test cases, with equal weightage. You will get feedback about which private test cases pass or fail, though you cannot see the actual test cases.
  • Ignore warnings about "Presentation errors".

    1. Write a function expanding(l) that takes as input a list of integer l and returns True if the absolute difference between each adjacent pair of elements strictly increases.

      Here are some examples of how your function should work.

        >>> expanding([1,3,7,2,9])
        True
      

      Explanation: Differences between adjacent elements are 3-1 = 27-3 = 47-2 = 59-2 = 7.

        >>> expanding([1,3,7,2,-3]) 
        False
      

      Explanation: Differences between adjacent elements are 3-1 = 27-3 = 47-2 = 52-(-3) = 5, so not strictly increasing.

        >>> expanding([1,3,7,10])
        False
      

      Explanation: Differences between adjacent elements are 3-1 = 27-3 = 410-7 = 3, so not increasing.

    2. Write a Python function sumsquare(l) that takes a nonempty list of integers and returns a list [odd,even], where odd is the sum of squares all the odd numbers in l and even is the sum of squares of all the even numbers in l.

      Here are some examples to show how your function should work.

      >>> sumsquare([1,3,5])
      [35, 0]
      
      >>> sumsquare([2,4,6])
      [0, 56]
      
      >>> sumsquare([-1,-2,3,7])
      [59, 4]
      
    3. A two dimensional matrix can be represented in Python row-wise, as a list of lists: each inner list represents one row of the matrix. For instance, the matrix

      1  2  3  4
      5  6  7  8
      

      would be represented as [[1, 2, 3, 4], [5, 6, 7, 8]].

      The transpose of a matrix converts each row into a column. The transpose of the matrix above is:

      1  5
      2  6
      3  7
      4  8
      

      which would be represented as [[1, 5], [2, 6], [3, 7], [4, 8]].

      Write a Python function transpose(m) that takes as input a two dimensional matrix m and returns the transpose of mThe argument m should remain undisturbed by the function.

      Here are some examples to show how your function should work. You may assume that the input to the function is always a non-empty matrix.

      >>> transpose([[1,2,3],[4,5,6]])
      [[1, 4], [2, 5], [3, 6]]
      
      >>> transpose([[1],[2],[3]])
      [[1, 2, 3]]
      
      >>> transpose([[3]])
      [[3]]
      
      
Your last recorded submission was on 2023-08-10, 16:23 IST
Select the Language for this assignment. 
def expanding(l):
    if len(l) < 3:
        return(True)
    for i in range(len(l)-2):
        diff = abs(l[i+1]-l[i])
        if diff >= abs(l[i+2]-l[i+1]):
            return(False)
    return(True)
 
def sumsquare(l):
    odd = 0
    even = 0
    for x in l:
        if x%2 :
            odd = odd + x**2
        else:
            even = even + x**2
    return([odd,even])
 
def transpose(m):
  list=[]
  c=len(m[0])
  r=len(m)
  for i in range(0,c):
    li=[]
    for j in range(0,r):
      li=li+[m[j][i]]
    list=list+[li] 
  return(list)
~~~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.
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed
Test Case 6
Passed
Test Case 7
Passed
Test Case 8
Passed
Test Case 9
Passed
Test Case 10
Passed
Test Case 11
Passed
Test Case 12
Passed


No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...