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]
[quads id=1]
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]
[quads id=1]
Question – 3 :-
Here is a function to compute the smallest of three input integers. You have to fill in the missing lines.
elif y<=z: minimum=y else: minimum=z
[quads id=1]
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]]
[quads id=1]
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)
[quads id=1]
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
[quads id=1]
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])
[quads id=1]
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)