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




















































































































































































































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.