Home

Swayam Solver

Learn Programming & Prepare for NPTEL Exams...

Find your course

Explore Courses

Latest Blog Posts

NPTEL Programming, Data Structures And Algorithms Using Python July 2026


Programming, Data Structures And Algorithms Using Python

 Please scroll down for latest Programs   ðŸ‘‡ 


Code compiled and tested successfully!


Week 3 Programming Assignment

Due date on 2026-08-14, 23:59 IST

Your last recorded submission was on 2026-08-14, 21:09 IST.

Q.

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 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".

  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.

      >>> contracting([9,2,7,3,1])
      True
    
      >>> contracting([-2,3,7,2,-1]) 
      False
    
      >>> contracting([10,7,4,1])
      False
    
  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.

     
    >>> counthv([1,2,1,2,3,2,1])
    [2, 1]
    
    >>> counthv([1,2,3,1])
    [1, 0]
    
    >>> counthv([3,1,2,3])
    [0, 1]
    
    
  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

      1  2  3
      4  5  6
      7  8  9
    

    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

      3  6  9
      2  5  8    
      1  4  7
    

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

    Here are some examples of how your function should work.

     
      >>> leftrotate([[1,2],[3,4]])
      [[2, 4], [1, 3]]
    
      >>> leftrotate([[1,2,3],[4,5,6],[7,8,9]])
      [[3, 6, 9], [2, 5, 8], [1, 4, 7]]
    
      >>> leftrotate([[1,1,1],[2,2,2],[3,3,3]])
      [[1, 2, 3], [1, 2, 3], [1, 2, 3]]


Complete Program

Python
# --- START OF SOLUTION CODE ---
def contracting(l):
    if len(l) < 3:
        return True
    return (abs(l[1] - l[0]) > abs(l[2] - l[1])) and contracting(l[1:])

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

def leftrotate(m):
    size = len(m)
    rotated_m = []
    for i in range(size):
        rotated_m.append([])
    for c in range(size - 1, -1, -1):
        for r in range(size):
            rotated_m[size - (c + 1)].append(m[r][c])
    return rotated_m
# --- END OF SOLUTION CODE ---

Code Snippet to Paste in the Editor

Python
# --- START OF SOLUTION CODE ---
def contracting(l):
    if len(l) < 3:
        return True
    return (abs(l[1] - l[0]) > abs(l[2] - l[1])) and contracting(l[1:])

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

def leftrotate(m):
    size = len(m)
    rotated_m = []
    for i in range(size):
        rotated_m.append([])
    for c in range(size - 1, -1, -1):
        for r in range(size):
            rotated_m[size - (c + 1)].append(m[r][c])
    return rotated_m
# --- END OF SOLUTION CODE ---



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.

Evaluation Results

CompilationSuccessful|Public Test Cases: 9/9 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

contracting([9,2,7,3,1])

True
True\n
Presentation Error
Test Case 2

contracting([-2,3,7,2,-1])

False
False\n
Presentation Error
Test Case 3

contracting([10,7,4,1])

False
False\n
Presentation Error
Test Case 4

counthv([1,2,1,2,3,2,1])

[2, 1]
[2, 1]\n
Presentation Error
Test Case 5

counthv([1,2,3,1])

[1, 0]
[1, 0]\n
Presentation Error
Test Case 6

counthv([3,1,2,3])

[0, 1]
[0, 1]\n
Presentation Error
Test Case 7

leftrotate([[1,2],[3,4]])

[[2, 4], [1, 3]]
[[2, 4], [1, 3]]\n
Presentation Error
Test Case 8

leftrotate([[1,2,3],[4,5,6],[7,8,9]])

[[3, 6, 9], [2, 5, 8], [1, 4, 7]]
[[3, 6, 9], [2, 5, 8], [1, 4, 7]]\n
Presentation Error
Test Case 9

leftrotate([[1,1,1],[2,2,2],[3,3,3]])

[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]\n
Presentation Error