Question – 1 :- Here is an function to return the maximum value in a list. There is an error in this function. Provide an input list for which `maxbad` produces an incorrect output.

def maxbad(l):
  end = len(l) - 1
  mymax = l[-1]
  for i in range(end,0,-1):
    if l[i] > mymax:
       mymax = l[i]
  return(mymax)

Answer:-

[9,8,7]


Question – 2:-

Here is an implementation of quicksort, which splits the input list according the pivot value, sorts each part and arranges the sorted parts with the pivot in between to give the final sorted sequence. There is a small error in the implementation. Provide an input list for which this version of quicksort produces an incorrect output.

def quicksortbad(l):
  if len(l) < 2:
    return(l)
  else:
    pivot = l[0]
    smaller = [l[j] for j in range(1,len(l)) if l[j] < pivot]
    bigger = [l[j] for j in range(1,len(l)) if l[j] > pivot]
    rearrange = quicksortbad(smaller) + [pivot] + quicksortbad(bigger)
    return(rearrange)

Answer:-

[5,5,5,5,5] OR [1,2,2,3]


Question – 3 :-

Here is a function to compute the smallest of three input integers. You have to fill in the missing lines.

def min3(x,y,z):
  if x <= y:
    if x <= z:
      minimum = x
  # Your code below this line


  # Your code above this line
  return(minimum)

Answer :-

elif y<=z:
    minimum=y
else:
    minimum=z

Question – 4 :-

Here is a recursive function to reverse a list. You have to fill in the missing argument for the recursive call.

def myreverse(l):
  if l==[]:
    return(l)
  else:
    return(....)

Answer :-

myreverse(l[1:])+[l[0]]

Question – 5 :-

A positive integer is said to be square free, if it is not divisible by any square integer strictly greater than 1. For instance, 5, 10 and 21 are square free, while 4 and 48 are not, since 4 is divisible by 22 and 48 is divisible by 42.

Write a Python function squarefree(n) that takes a positive integer argument and returns True if the integer is square free, and False otherwise.

Answer :-

def squarefree(n):
      k=1
      for i in range(2,n-1):
            if(n%(i*i)==0):
                  return(False)
      return(True)

Question – 6 :- Write a Python function disjointlist(l1,l2) that takes two lists as arguments and returns True if the two lists are disjoint, otherwise returns False.

Two lists are said to be disjoint if there is no element that common to both the lists. For instance, [2,2,3,4,5] and [6,8,8,1] are disjoint, while [1,2,3,4] and [2,2] are not.

Answer :-

def disjointlist(l, ll):
  for i in l:
    if i in ll:
      return False
  return True

Question – 7 :-

Write a Python program that reads input from the keyboard (standard input). The input will consist of an even number of lines of text. The input will be terminated by a blank line. Suppose there are 2n lines of input. Your program should print out the last n lines of the input, i.e., the second half of the input, followed by the first n lines, i.e., the first half of the input.

E.g., if the input is the following:
“our dear friend,
let’s eat”

then the output should be:
“let’s eat
our dear friend,”

Answer:-

lis = []

zz = input('') 

while zz != '':
  lis.append(zz)
  zz = input('')
  
length = len(lis)

for i in range(int(length/2), length):
  print(lis[i])

for i in range(0, int(length/2)):
  print(lis[i])

Question – 8 :-

Write a Python function maxcount(l) that takes a list of immutable values as argument. The list could have repeated values. The function should return the number of times the most frequent value is repeated.

For instance, maxcount([1,17,31,17,22,17]) should return 3 because the most frequent value, 17, occurs 3 times. Likewise maxcount(["the","higher","you","climb","the","further","you","fall"]) is 2 becaues the most frequent values, "the"and "you" both occur 2 times.

def maxcount(l):
  mcount = 0
  for i in l:
    count = 0
    for j in l:
      if (i==j):
        count += 1
        if (count > mcount):
          (count,mcount) = (mcount,count)
  return(mcount)

 

Categories: NPTEL solution

10 Comments

sai · September 17, 2017 at 5:06 AM

_/\_
thanks a lot sir.

zeel patel · September 17, 2017 at 5:08 AM

thank you so much yash i really appreciate your work.

elzabeth thomas · September 17, 2017 at 6:14 AM

thankyou sooo much..!!

Saurab.B · September 17, 2017 at 6:24 AM

thank you for the answer
but in 3 answer i think so logical error might be there

    Saurab.B · September 17, 2017 at 6:26 AM

    thank you for the answer

Rafik · September 17, 2017 at 6:29 AM

plz provide week 8 programming assignment….

krish · September 17, 2017 at 6:58 AM

Thanks a lot

Mayuresh · September 17, 2017 at 8:10 AM

Sir there was some internet issue so i have not attended the 9-11 session
so i m attending 2 to 4 session please help me with solution.

    kkm · September 17, 2017 at 8:59 AM

    please post 2 to 4

pavan · September 17, 2017 at 4:31 PM

sir 8 th week programming assignment sir please…..

Leave a Reply

Your email address will not be published. Required fields are marked *