NPTEL Programming, Data Structures And Algorithms Using Python Week 2 Programming Assignment August 2023

Week 2 Programming Assignment

Due on 2023-08-10, 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 2023-08-04, 15:25 IST
Select the Language for this assignment. 
def factors(n):
    factorlist = []
    for i in range(1,n+1):
        if n%i == 0:
            factorlist.append(i)
    return(factorlist)
 
def isprime(n):
    return(factors(n) == [1,n])
 
def primeproduct(n):
    for i in range(1,n+1):
        if n%i == 0:
            if isprime(i) and isprime(n//i):
                return(True)
    return(False)
 
def delchar(s,c):
    if len(c) != 1:
        return(s)
    snew = ""
    for char in s:
        if char != c:
            snew = snew + char
    return(snew)
 
def shuffle(l1,l2):
    if len(l1) < len(l2):
        minlength = len(l1)
    else:
        minlength = len(l2)
    shuffled = []
    for i in range(minlength):
        shuffled.append(l1[i])
        shuffled.append(l2[i])
    shuffled = shuffled + l1[minlength:] + l2[minlength:]
    return(shuffled)
~~~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




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