Home

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

NPTEL Programming, Data Structures And Algorithms Using Python July 2025

 

NPTEL » Programming, Data Structures and Algorithms using Python


  Please scroll down for latest Programs ðŸ‘‡ 



Week 2 Programming Assignment

Due on 2025-08-07, 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. A positive integer m is a prime product if it can be written as p×q, where p and q are both primes. .

    Write a Python function primeproduct(m) that takes an integer m as input and returns True if m is a prime product and False otherwise. (If m is not positive, your function should return False.)

    Here are some examples of how your function should work.

    >>> primeproduct(6)
    True
    
    >>> primeproduct(188)
    False
    
    >>> primeproduct(202)
    True
    
  2. Write a function delchar(s,c) that takes as input strings s and c, where c has length 1 (i.e., a single character), and returns the string obtained by deleting all occurrences of c in s. If c has length other than 1, the function should return s

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

     
    >>> delchar("banana","b")
    'anana'
    
    >>> delchar("banana","a")
    'bnn'
    
    >>> delchar("banana","n")
    'baaa'
    
    >>> delchar("banana","an")
    'banana'
    
  3. Write a function shuffle(l1,l2) that takes as input two lists, 11 and l2, and returns a list consisting of the first element in l1, then the first element in l2, then the second element in l1, then the second element in l2, and so on. If the two lists are not of equal length, the remaining elements of the longer list are appended at the end of the shuffled output.

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

    >>> shuffle([0,2,4],[1,3,5])
    [0, 1, 2, 3, 4, 5]
    
    >>> shuffle([0,2,4],[1])
    [0, 1, 2, 4]
    
    >>> shuffle([0],[1,3,5])
    [0, 1, 3, 5]
    
Your last recorded submission was on 2025-08-01, 13:11 IST
Select the Language for this assignment. 
1
def factors(n):
2
    factorlist = []
3
    for i in range(1,n+1):
4
        if n%i == 0:
5
            factorlist.append(i)
6
    return(factorlist)
7
def isprime(n):
8
    return(factors(n) == [1,n])
9
def primeproduct(n):
10
    for i in range(1,n+1):
11
        if n%i == 0:
12
            if isprime(i) and isprime(n//i):
13
                return(True)
14
    return(False)
15
def delchar(s,c):
16
    if len(c) != 1:
17
        return(s)
18
    snew = ""
19
    for char in s:
20
        if char != c:
21
            snew = snew + char
22
    return(snew)
23
def shuffle(l1,l2):
24
    if len(l1) < len(l2):
25
        minlength = len(l1)
26
    else:
27
        minlength = len(l2)
28
    shuffled = []
29
    for i in range(minlength):
30
        shuffled.append(l1[i])
31
        shuffled.append(l2[i])
32
    shuffled = shuffled + l1[minlength:] + l2[minlength:]
33
    return(shuffled)
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
primeproduct(6)
True
True\n
Passed after ignoring Presentation Error
Test Case 2
primeproduct(188)
False
False\n
Passed after ignoring Presentation Error
Test Case 3
primeproduct(202)
True
True\n
Passed after ignoring Presentation Error
Test Case 4
delchar("banana","b")
anana
anana\n
Passed after ignoring Presentation Error
Test Case 5
delchar("banana","a")
bnn
bnn\n
Passed after ignoring Presentation Error
Test Case 6
delchar("banana","n")
baaa
baaa\n
Passed after ignoring Presentation Error
Test Case 7
delchar("banana","an")
banana
banana\n
Passed after ignoring Presentation Error
Test Case 8
shuffle([0,2,4],[1,3,5])
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5]\n
Passed after ignoring Presentation Error
Test Case 9
shuffle([0,2,4],[1])
[0, 1, 2, 4]
[0, 1, 2, 4]\n
Passed after ignoring Presentation Error
Test Case 10
shuffle([0],[1,3,5])
[0, 1, 3, 5]
[0, 1, 3, 5]\n
Passed after ignoring Presentation Error





Week 4 Programming Assignment

Due on 2025-08-21, 23:59 IST

Write two Python functions as specified below. Paste the text for both 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 10 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 Python function histogram(l) that takes as input a list of integers with repetitions and returns a list of pairs as follows:.

    • for each number n that appears in l, there should be exactly one pair (n,r) in the list returned by the function, where r is the number of repetitions of n in l.

    • the final list should be sorted in ascending order by r, the number of repetitions. For numbers that occur with the same number of repetitions, arrange the pairs in ascending order of the value of the number.

    For instance:

    >>> histogram([13,12,11,13,14,13,7,7,13,14,12])
    [(11, 1), (7, 2), (12, 2), (14, 2), (13, 4)]
    
    >>> histogram([7,12,11,13,7,11,13,14,12])
    [(14, 1), (7, 2), (11, 2), (12, 2), (13, 2)]
    
    >>> histogram([13,7,12,7,11,13,14,13,7,11,13,14,12,14,14,7])
    [(11, 2), (12, 2), (7, 4), (13, 4), (14, 4)]
    
  2. A college maintains academic information about students in three separate lists

    • Course details: A list of pairs of form (coursecode,coursename), where both entries are strings. For instance,
      [ ("MA101","Calculus"),("PH101","Mechanics"),("HU101","English") ]

    • Student details: A list of pairs of form (rollnumber,name), where both entries are strings. For instance,
      [ ("UGM2018001","Rohit Grewal"),("UGP2018132","Neha Talwar") ]

    • A list of triples of the form (rollnumber,coursecode,grade), where all entries are strings. For instance,
      [ ("UGM2018001", "MA101", "AB"), ("UGP2018132", "PH101", "B"), ("UGM2018001", "PH101", "B") ]. You may assume that each roll number and course code in the grade list appears in the student details and course details, respectively.

    Your task is to write a function transcript (coursedetails,studentdetails,grades) that takes these three lists as input and produces consolidated grades for each student. Each of the input lists may have its entries listed in arbitrary order. Each entry in the returned list should be a tuple of the form

    (rollnumber, name,[(coursecode_1,coursename_1,grade_1),...,(coursecode_k,coursename_k,grade_k)])

    where the student has grades for k ≥ 1 courses reported in the input list grades.

    The output list should be organized as follows.

    • The tuples shold sorted in ascending order by rollnumber

    • Each student's grades should sorted in ascending order by coursecode

    For instance

     
    >>>transcript([("MA101","Calculus"),("PH101","Mechanics"),("HU101","English")],[("UGM2021001","Rohit Grewal"),("UGP2021132","Neha Talwar")],[("UGM2021001","MA101","AB"),("UGP2021132","PH101","B"),("UGM2021001","PH101","B")])
    
    [('UGM2021001', 'Rohit Grewal', [('MA101', 'Calculus', 'AB'), ('PH101', 'Mechanics', 'B')]), ('UGP2021132', 'Neha Talwar', [('PH101', 'Mechanics', 'B')])]
    
    >>>transcript([("T1","Test 1"),("T2","Test 2"),("T3","Test 3")],[("Captain","Rohit Sharma"),("Batsman","Virat Kohli"),("No3","Cheteshwar Pujara")],[("Batsman","T1","14"),("Captain","T1","33"),("No3","T1","30"),("Batsman","T2","55") ,("Captain","T2","158"),("No3","T2","19"), ("Batsman","T3","33"),("Captain","T3","95"),("No3","T3","51")])
    
    [('Batsman', 'Virat Kohli', [('T1', 'Test 1', '14'), ('T2', 'Test 2', '55'), ('T3', 'Test 3', '33')]), ('Captain', 'Rohit Sharma', [('T1', 'Test 1', '33'), ('T2', 'Test 2', '158'), ('T3', 'Test 3', '95')]),('No3', 'Cheteshwar Pujara', [('T1', 'Test 1', '30'), ('T2', 'Test 2', '19'), ('T3', 'Test 3', '51')])]
    
Your last recorded submission was on 2025-08-20, 20:52 IST
Select the Language for this assignment. 
1
def histogram(l):
2
  tuplelist=[]
3
  for i in range(len(l)):
4
    v=l[i]
5
    count=0
6
    for j in range(len(l)):
7
      if l[j] == v:
8
        count+=1
9
    if i==0:
10
        tuplelist+=[(v,count)]
11
    else :
12
        for k in range(len(tuplelist)):
13
          if tuplelist[k] == (v,count):
14
            break
15
        else:
16
          tuplelist+=[(v,count)]
17
  SelectionSort(tuplelist)
18
  return(tuplelist)
19
def SelectionSort(l):
20
  for start in range(len(l)):
21
    minpos = start
22
    for i in range(start,len(l)):
23
      if l[i][1] < l[minpos][1]:
24
        minpos = i
25
      elif l[i][1] == l[minpos][1]:
26
        if l[i][0] < l[minpos][0]:
27
          minpos = i
28
    (l[start],l[minpos]) = (l[minpos],l[start])
29
def transcript (cd,st,gra):
30
  list=[]
31
  for i in range(len(st)):
32
    rollno = st[i][0]
33
    student_name= st[i][1]
34
    clist=[]
35
    for j in range(len(gra)):
36
      if gra[j][0] == rollno:
37
        coursecode= gra[j][1]
38
        grade= gra[j][2]
39
        for k in range(len(cd)):
40
          if cd[k][0] == coursecode:
41
            coursename = cd[k][1]
42
            break
43
        clist+=[(coursecode,coursename,grade)]
44
        csort(clist)
45
    list+=[(rollno,student_name,clist)]
46
  csort(list)
47
  return(list)
48
def csort(l):
49
  for start in range(len(l)): 
50
    minpos = start
51
    for i in range(start,len(l)):
52
      if l[i][0] < l[minpos][0]:
53
        minpos = i
54
    (l[start],l[minpos]) = (l[minpos],l[start])
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.
   



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





Week 8 Programming Assignment - Dividing Sequences

Due on 2025-09-18, 23:59 IST

For this assignment, you have to write a complete Python program. Paste your code in the window below.

  • You may define additional auxiliary functions as needed.
  • There are 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 10 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".

Dividing Sequences

(IARCS OPC Archive, K Narayan Kumar, CMI)

This problem is about sequences of positive integers a1,a2,…,aN. A subsequence of a sequence is anything obtained by dropping some of the elements. For example, 3,7,11,3 is a subsequence of 6,3,11,5,7,4,3,11,5,3 , but 3,3,7 is not a subsequence of 6,3,11,5,7,4,3,11,5,3 .

fully dividing sequence is a sequence a1,a2,…,aN where ai divides aj whenever i < j. For example, 3,15,60,720 is a fully dividing sequence.

Given a sequence of integers your aim is to find the length of the longest fully dividing subsequence of this sequence.

Consider the sequence 2,3,7,8,14,39,145,76,320

It has a fully dividing sequence of length 3, namely 2,8,320, but none of length 4 or greater.

Consider the sequence 2,11,16,12,36,60,71,17,29,144,288,129,432,993 .

It has two fully dividing subsequences of length 5,

  • 2,11,16,12,36,60,71,17,29,144,288,129,432,993 and
  • 2,11,16,12,36,60,71,17,29,144,288,129,432,993

and none of length 6 or greater.

Solution hint

Let the input be a1, a2, …, aN. Let us define Best(i) to be the length of longest dividing sequence in a1,a2,…ai that includes ai.

Write an expression for Best(i) in terms of Best(j) with j<i, with base case Best(1) = 1. Solve this recurrence using dynamic programming.

Full solution

.

Input format

The first line of input contains a single positive integer N indicating the length of the input sequence. Lines 2,…,N+1 contain one integer each. The integer on line i+1 is ai.

Output format

Your output should consist of a single integer indicating the length of the longest fully dividing subsequence of the input sequence.

Test Data

You may assume that N ≤ 2500.

Example:

Here are the inputs and outputs corresponding to the two examples discussed above.

Sample input 1:

9
2 
3 
7 
8 
14 
39 
145 
76 
320

Sample output 1:

3

Sample input 2:

14
2
11 
16 
12 
36 
60 
71 
17 
29 
144 
288 
129 
432 
993

Sample output 2:

5
Your last recorded submission was on 2025-09-15, 01:52 IST
Select the Language for this assignment. 
1
n = int(input())
2
L = list()
3
bestvals =list()
4
best_stored = list()
5
for x in range(n):
6
  L.append(int(input()))
7
  best_stored.append(0)
8
best_stored[0] = 1
9
for i in range(n):
10
  maxval = 1
11
  for j in range(i):
12
    if L[i] % L[j] == 0:
13
      maxval = max(maxval,(best_stored[j])+1)
14
  best_stored[i] = maxval
15
print(max(best_stored),end="")
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
9
2 
3 
7 
8 
14 
39 
145 
76 
320
3
3
Passed
Test Case 2
14
2
11 
16 
12 
36 
60 
71 
17 
29 
144 
288 
129 
432 
993
5
5
Passed










































































































































































































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.