Programming Data Structure And Algorithms Using Python Assignment 3

                     

Course Name: Programming Data Structure And Algorithms Using Python

Link of Course: Click Here

These are the solutions of Programming Data Structure And Algorithms Using Python Assignment 3


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.

These are the solutions of Programming Data Structure And Algorithms Using Python Assignment 3

  • 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 15 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”.

These are the solutions of Programming Data Structure And Algorithms Using Python Assignment 3

Question 1
Write a function contracting(l) that takes as input a list of integer l and returns True if the absolute difference between each adjacent pair of elements strictly decreases.Here are some examples of how your function should work.

These are the solutions of Programming Data Structure And Algorithms Using Python Assignment 3

Question 2
In a list of integers l, the neighbours of l[i] are l[i-1] and l[i+1]. l[i] is a hill if it is strictly greater than its neighbours and a valley if it is strictly less than its neighbours. Write a function counthv(l) that takes as input a list of integers l and returns a list [hc,vc] where hc is the number of hills in l and vc is the number of valleys in l. Here are some examples to show how your function should work.

These are the solutions of Programming Data Structure And Algorithms Using Python Assignment 3

Question 3
A square n×n matrix of integers can be written in Python as a list with n elements, where each element is in turn a list of n integers, representing a row of the matrix. For instance, the matrix would be represented as [[1,2,3], [4,5,6], [7,8,9]].
Write a function leftrotate(m) that takes a list representation m of a square matrix as input, and returns the matrix obtained by rotating the original matrix counterclockwize by 90 degrees. For instance, if we rotate the matrix above, we get

Your function should not modify the argument m provided to the function rotate().

Here are some examples of how your function should work.

These are the solutions of Programming Data Structure And Algorithms Using Python Assignment 3

Solution:

def contracting(l):
  for i in range(0,len(l)-3):
    if (abs(l[i+2]-l[i+1]) < abs(l[i+1]-l[i])):
      continue
    else:
      return False
  return True



def counthv(l):
  hc=0
  vc=0
  if len(l) > 2:
    for i in range(1,len(l)-1):
      if l[i] > l[i-1] and l[i] > l[i+1]:
        hc+=1
      elif l[i] < l[i-1] and l[i] < l[i+1]:
        vc+=1
  return [hc,vc]



def leftrotate(m):
    b=[]
    for i in range(len(m)):
        b.append([])
        for j in range(len(m)):
            b[i].append(m[j][len(m)-i-1])
    return(b)

These are the solutions of Programming Data Structure And Algorithms Using Python Assignment 3

All weeks solution of Programming Data Structure And Algorithms Using Python: https://progies.in/answers/nptel/programming-data-structure-and-algorithms-using-python

More NPTEL Solutions: https://progies.in/answers/nptel


* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.


These are the solutions of Programming Data Structure And Algorithms Using Python Assignment 3