All answers have been verified.

Question – 1 :-

Here is an function to return the maximum value in a list of integers. There is an error in this function. Provide an input list for whichmaxbad produces an incorrect output.

def maxbad(l):
  mymax = 0
  for i in range(len(l)):
    if l[i] > mymax:
       mymax = l[i]



[quads id=1]

Question – 2:-

Here is a function stablesortbad that takes a list of pairs of integers as input and sorts them by the first coordinate in each pair. A stable sort preserves the order of pairs that have an equal first coordinate. This is not a stable sort. Provide an input for which stablesortbad produces an output that is not stably sorted. Your input should be a list of pairs of integers of the form [(i1,j1),(i2,j2),…,(in,jn)].

def stablesortbad(l):
 for j in range(len(l)-1):
 for i in range(len(l)-1):
 if l[i][0] >= l[i+1][0]:
 (l[i],l[i+1]) = (l[i+1],l[i])

Answer (thanks for commenting):-




[quads id=1]
Question – 3 :-

Here is a function to compute the third smallest value in a list of distinct integers. All the integers are guaranteed to be below 1000000. You have to fill in the missing lines. You can assume that there are at least three numbers in the list.

def thirdmin(l):
  (mymin,mysecondmin,mythirdmin) = (1000000,1000000,1000000)
  for i in range(len(l)):
  # Your code below this line

  # Your code above this line

Answer(Indent it to the function level) :-

    mythirdmin = l[2]

[quads id=1]

Question 4:-

Recall that the positions in a list of length n are 0,1,…,n-1. We want to write a function evenpositions(l) that returns the elements at the even positions in l. In other words, the function should return the list [l[0],l[2],...]

For instance evenpositions([]) == []evenpositions([7]) == [7]evenpositions([8,11,8]) == [8,8] and evenpositions([19,3,44,44,3,19]) == [19,44,3]. A recursive definition of evenpositions is given below. You have to fill in the missing argument for the recursive call.

def evenpositions(l):
  if len(l) < 1:
[l[0]] + evenpositions(l[2:])

[quads id=1]

Question 5:- A positive integer n is a sum of three squares if n = i2 + j2 + k2 for integers i,j,k such that i ≥ 1j ≥ 1 and k ≥ 1. For instance, 29 is a sum of three squares because 10 = 22 + 32 + 42, and so is 6 (12 + 12 + 22). On the other hand, 16 and 20 are not sums of three squares.

Write a Python function sumof3squares(n) that takes a positive integer argument and returns True if the integer is a sum of three squares, and False otherwise.


def sumof3squares(n):
  lst = range(1, n-1)
  for i in lst:
    for j in lst:
      for k in lst:
        if (i**2 + j**2 + k**2) == n:
          return True
  return False

[quads id=1]

Question 6:-

Write a Python function uncommon(l1,l2) that takes two lists sorted in ascending order as arguments and returns the list of all elements that appear in exactly one of the two lists. The list returned should be in ascending order. All such elements should be listed only once, even if they appear multiple times in l1 or l2.

Thus, uncommong([2,2,4],[1,3,3,4,5]) should return [1,2,3,5] while uncommon([1,2,3],[1,1,2,3,3]) should return [].

Answer :-

def uncommon(l1, l2):
  l1 = list(set(l1))
  l2 = list(set(l2))
  for i in l2:
    if i not in l1:
  for i in l1:
    if i not in l2 and i not in l3:
  return l3

[quads id=1]


Question 7:-

Write a Python program that reads input from the keyboard (standard input). The input will consist of some number of lines of text. The input will be terminated by a blank line. The first line will consist of a single word to be interpreted as a pattern, after discarding the new line character. Your program should print the last line from the second line onward that contains an occurrence of the pattern. If no lines match the pattern, the program should print an empty line. You can assume that the input will have a non-empty pattern line. Recall that for a string s and a pattern ps.find(p) returns the first position in s where p occurs, and returns -1 if pdoes not occur in s.

For instance, if the input is the following:

"Spot the mistake
in the following argument",
Jack challenged
= (1+ -1)+(1+ -1)+...
so therefore,
1 = 0

then your program should print the following. Note that the pattern string the is matched by the word therefore.

so therefore,

Answer( thanks to Prabhakar):-

for i in range(len(store)-1,0,-1):

[quads id=1]
Question 8 :-

Write a Python function maxaverage(l) that takes a list of pairs of the form (name,score) as argument, where name is a string and score is an integer. Each pair is to be interpreted as the score of the named player. For instance, an input of the form [(‘Kohli’,73),(‘Ashwin’,33),(‘Kohli’,7),(‘Pujara’,122),(‘Ashwin’,90)] represents two scores of 73 and 7 for Kohli, two scores of 33 and 90 for Ashwin and one score of 122 for Pujara. Your function should compute the players who have the highest average score (average = total across all scores for that player divided by number of entries) and return the list of names of these players as a list, sorted in alphabetical order. If there is a single player, the list will contain a single name.

For instance, maxaverage([('Kohli',73),('Ashwin',33),('Kohli',7),('Pujara',122),('Ashwin',90)]) should return ['Pujara'] because the average score of Kolhi is 40 (80 divided by 2), of Ashwin is 61.5 (123 divided by 2) and of Pujara is 122 (122 divided by 1), of which 122 is the highest.

Answer(100.0/100.0) :-

def maxaverage(l):
    d = {}
    for i in l:
        name, score = i
        if name in d:
            tot_score, num = d[name]
            d[name] = (tot_score+score, num+1)
            d[name] = (score, 1)

    max= -1
    for key in d:
        tot_score, num = d[key]
        ave = tot_score/num
        if(max < ave):
            max = ave

    l = []

    for key in d:
        tot_score, num = d[key]
        ave = tot_score/num
        if(max == ave):


    return l