Search This Blog

Thursday, September 24, 2020

NPTEL Assignment Solution for Data structures and algorithm using Python

 

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

 def primeproduct(x):
    z=0
    for i in range(2,x):
        if x % i == 0:
            k=x//i
            for j in range(2,k):
                if k % j == 0:
                    z=1
    if z==0:
        return True
    else:
        return False

NPTEL Assignment Solution for Data structures and algorithm using Python

 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

 def delchar(s,c):
  k=s.replace(c,"")
  return k

NPTEL Assignment Solution for Data structures and algorithm using Python

Write a function shuffle(l1,l2) that takes as input two lists, 11 and l2, and returns a list consisting of the first elment in l1, then the first element in l2, then the second element in l2, 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.

def shuffle(list_a,list_b):
    common_num = []
    for a in range(max(len(list_a),len(list_b))):
      try:
        common_num.append(list_a[a])
      except IndexError:
                pass
      for b in range(a,a+1):
        try:
          common_num.append(list_b[b])
        except IndexError:
          break
    return common_num