# Programming Data Structures And Algorithms Using Python Online Programming Test Answers 2022

Are You also looking for Programming Data Structures And Algorithms Using Python Online Programming Test Answers then you are at right place. In this Article we will help you with Programming Data Structures And Algorithms Using Python Online Programming Test Answers of 18 September 2022

## IMPORTANT: CERTIFICATION CRITERIA

Candidates have to come to the exam centre and take the theory test on September 25, 2022

It is mandatory to take the Proctored in-person final exam to obtain a certificate.
To pass the course and get a certificate:

Assignment score >= 40/100 AND Programming exam score >= 40/100 AND Proctored exam score >= 40/100

OR

Assignment score >= 10/25 AND Programming exam score >= 10/25 AND Proctored exam score >= 20/50

All 3 conditions have to be satisfied.
All the best to the candidates!

## Programming Data Structures And Algorithms Using Python Online Programming Test Answers 18 September 2022 2nd Session 8 PM to 10 PM

Q1. Here is an function to return the minimum value in a list. There is an error in this function. Provide an input list for which minbad produces an incorrect output.

Code:-

``````[-9,-29,-39,-49]
``````

Q2. Here is a function stablesortbad that takes a list of pairs of integers as input and sorts them by the second coordinate in each pair. A stable sort preserves the order of pairs that have an equal second 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)].

``````[(2,2),(1,2)]
``````

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

``````
for j in range(i+1,len(l)):
if(l[i]>l[j]):
temp=l[i]
l[i]=l[j]
l[j]=temp
(mymax,mysecondmax,mythirdmax) =(l[-1],l[-2],l[-3])
``````

Q4. Recall that the positions in a list of length n are 0,1,…,n-1. We want to write a function mod3pos(l) that returns the elements at all positions in l that are divisible by 3. In other words, the function should return the list [l,l,…]. For instance mod3pos([]) == [], mod3pos() == , mod3pos([8,11,8,11]) == [8,11] and mod3pos([19,3,44,44,3,19,17,23]) == [19,44,17]. A recursive definition of mod3pos is given below. You have to fill in the missing argument for the recursive call.

`` [l]+mod3pos(l[3:])``

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

``````
def squarefree(n):

for i in range (2, round(n**0.5)):
if n % (i**2) == 0:
return False
return(True)
``````

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

``````def disjointlist(l1,l2):
a=set(l1)
b=set(l2)
c=a&b
if len(c)==0:
return True
return False``````

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

``````a=[]
while True:
n=input()
if n!="":
a.append(n)
else:
break
for i in range(len(a)//2,len(a)):
print(a[i])
for i in range(len(a)//2):
print(a[i])``````

Q8. Write a Python function maxdifference(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),(‘Kohli’,66),(‘Ashwin’,90)] represents three scores of 73, 66 and 7 for Kohli, two scores of 33 and 90 for Ashwin and one score of 122 for Pujara. Your function should compute the difference between the maximum and minimum score for each player and return the list of players for whom this difference is maximum. The list should be sorted in ascending order by the name of the player.

``````def maxdifference(l):
d={}
for i in range(len(l)):
(name,scores )=l[i]
try:
d[name].append(scores )
except KeyError:
d[name]={}
d[name]=[scores ]
res=[]
maaa=0
for i in d.keys():
ma=max(d[i])
mi=min(d[i])
mx=ma-mi
if maaa<mx:
maaa=mx
for i in d.keys():
ma=max(d[i])
mi=min(d[i])
mx=ma-mi
if mx==maaa:
res.append(i)
res.sort()
return(res)``````

## Programming Data Structures And Algorithms Using Python Online Programming Test Answers 18 September 2022 1st Session 10 AM to 12 PM

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.

Code:-

``[52,13,20,44]``

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.

``````[12,15,5,9,8,3,77,15]
``````

3. The median of three numbers x,y,z is the second number in the sequence when the three numbers are sorted in ascending or descending order. Here is a function to compute the median of three input integers. You have to fill in the missing lines.

``````def median3(x,y,z):
if x <= y:
if x >= z:
mymedian = x``````

4. A list is a decreasing if each element is strictly smaller than the preceding one. For instance [], , [11,8] and [89,63,44,19,3] are decreasing, while [3,18,4] and [23,14,14,3] are not. Here is a recursive function to check if a list is decreasing. You have to fill in the missing argument for the recursive call.

``decreasing(l[1:]) if l > l else False``

5. A positive integer n is a sum of three cubes if n = i3 + j3 + k3 for integers i,j,k such that i ≥ 1j ≥ 1 and k ≥ 1. For instance, 10 is a sum of three cubes because 10 = 13 + 13 + 23, and so is 36 (1+ 23 + 33). On the other hand, 4 and 11 are not sums of three cubes.

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

``````def sum3cubes(n):
sum=0
for i in range(1,n) :
for j in range(1, n) :
for k in range(1, n) :
if(i*i*i+j*j*j+k*k*k) == n:
sum+=1
if sum>=1:
return True
else:
return False``````

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.

``````def uncommon(l1,l2) :
s1 = set(l1)
s2 = set(l2)
union = s1|s2
intersection = s1 & s2
ans = list(union - intersection)
ans.sort()
return ans``````

7. Write a Python program that reads input from the keyboard (standard input). The input will be terminated by a blank line. The lines are numbered 0,1,2,… Your program should print out the even numbered lines followed by the odd numbered lines. In other words, first print lines 0,2,4,… then lines 1,3,5,…

``````x = input()
c = 0
even = []
odd = []
while len(x) > 0 :
if c%2 == 0 :
even.append(x)
else :
odd.append(x)
x = input()
c+=1
for eve in even :
print(eve)
for od in odd :
print(od)``````

8. Write a Python function aboveaverage(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 list of players whose individual average score is greater than or equal to the overall average score. For an individual player, the average score is the total across all scores for that player divided by number of entries for that player. The overall average score is the total across all scores for all the players divided by the total number of entries across all players. The list should be sorted in ascending order by the name of the player.

``````def aboveaverage(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)
else:
d[name] = (score, 1)
max= -1
for k in d:
tot_score, num = d[k]
ave = tot_score/num
if(max < ave):
max = ave
l = []
for k in d:
tot_score, num = d[k]
ave = tot_score/num
if(max == ave):
l.append(k)
l.sort()
return l``````

## Programming Data Structures And Algorithms Using Python Online Programming Test Answers March 2022 2nd Session 8 PM to 10 PM:

Q1. 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 which maxbad produces an incorrect output.

Code:-

``[-1,-2,-4,-5]``

Q2. Here is a function stablesortbad that takes a list of pairs of integers as input and sorts them by the second coordinate in each pair. A stable sort preserves the order of pairs that have an equal second 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)].

Code:-

``[(12,15),(5,9),(8,3),(77,15)]``

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

Code:-

``````l.sort()
mythirdmin=l
break``````

Q4. Recall that the positions in a list of length n are 0,1,…,n-1. We want to write a function oddpositions(l) that returns the elements at the odd positions in l. In other words, the function should return the list [l,l,…]. For instance oddpositions([]) == [], oddpositions() == [], oddpositions([8,11,8]) ==  and oddpositions([19,3,44,44,3,19]) == [3,44,19]. A recursive definition of oddpositions is given below. You have to fill in the missing argument for the recursive call.

Code:-

``[l]+oddpositions(l[2:])``

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

Code:-

``````def sumof3squares(n):
f=0
for i in range(1,int(n**(0.5)),1):
for j in range(1,int(n**(0.5)),1):
for k in range(1,int(n**(0.5)),1):
if (i*i + j*j + k*k)==n:
f=1
return (f==1)``````

Q6. Write a Python function intersect(l1,l2) that takes two sorted lists as arguments and returns the list of all elements common to both l1 and l2 in the same order that they appear in the two lists. If the same element occurs more than once in both lists, it should appear in the output exactly once.

Code:-

``````def intersect(l1,l2):
l1s=set(l1)
l2s=set(l2)
ls = l1s.intersection(l2s)
return list(ls)
``````

Q7. 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 p, s.find(p) returns the first position in s where p occurs, and returns -1 if p does not occur in s.

Code:-

``````pattern = input()
while True:
try:
x = input()
if pattern in x:
a = x
except:
print(a)
break``````

Q8.

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.

Code:-

``````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)
else:
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):
l.append(key)
l.sort()
return l``````

## Programming Data Structures And Algorithms Using Python Online Programming Test Answers 2022 1st Session 10 AM to 12 PM:

Q1. Here is a function isprimebad that takes a positive integer as input and returns True if the number is prime and False otherwise. There is an error in this function. Provide an input n, which is a positive integer, for which isprimebad produces an incorrect output.

Code:-

``4``

Q2. Here is a function lexsortbad that takes a list of pairs of integers as input and returns them in lexicographically sorted order (i.e., dictionary order). There is an error is this function. Provide an input for which lexsortbad produces an incorrect output. Your input should be a list of pairs of integers of the form [(i1,j1),(i2,j2),…,(in,jn)].

Code:-

``[(12, 53), (52, 69), (43, 43), (55, 9), (25, 0) ]``

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

Code:-

``````else:
minimum=z
elif y<=z and y<=x:
minimum=y
else:
minimum=z``````

Q4. A list is a non-decreasing if each element is at least as big as the preceding one. For instance [], , [8,8,11] and [3,19,44,44,63,89] are non-decreasing, while [3,18,4] and [23,14,3,14,3,23] are not. Here is a recursive function to check if a list is non-decreasing. You have to fill in the missing argument for the recursive call.

Code:-

``````l<=l and nondecreasing(l[1:])
``````

Q5. A positive integer n is a sum of squares if n = i2 + j2 for integers i,j such that i ≥ 1 and j ≥ 1. For instance, 10 is a sum of squares because 10 = 12 + 32, and so is 25 (32 + 42). On the other hand, 11 and 3 are not sums of squares.

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

Code:-

``````def sumofsquares(n) :
i = 1
while i * i <= n :
j = 1
while(j * j <= n) :
if (i * i + j * j == n) :
return True
j = j + 1
i = i + 1
return False``````

Q6. Write a Python function subsequence(l1,l2) that takes two sorted lists as arguments and returns True if the the first list is a subsequence of the second list, and returns False otherwise.

A subsequence of a list is obtained by dropping some values. Thus, [2,3,4] and [2,2,5] are subsequences of [2,2,3,4,5], but [2,4,4] and [2,4,3] are not

Code:-

``````def subsequence(l1, l2):
count = 0
for i in l1:
if i in l2:
l2.remove(i)
count += 1
if count == len(l1):
return(True)
else:
return(False)``````

Q7. 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 every line from the second line onward that contains an occurrence of the pattern. You can assume that the input will have a non-empty pattern line. Recall that for a string s and a pattern p, s.find(p) returns the first position in s where p occurs, and returns -1 if p does not occur in s.

Code:-

``````a = input()
b =a
l =[]
while a != "":
a = input()
l.append(a)
for i in range(0,len(l)):
if l[i].find(b) != -1:
print(l[i])``````

Q8. Write a Python function maxaggregate(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 aggregate score (aggegrate = total, so add up all scores for that name) 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, maxaggregate([(‘Kohli’,73),(‘Ashwin’,33),(‘Kohli’,7),(‘Pujara’,122),(‘Ashwin’,90)]) should return [‘Ashwin’] because the aggregate score of Kolhi is 80, of Ashwin is 123 and of Pujara is 122, of which 123 is the highest.

Code:-

``````def maxaggregate(l):
dic = {}
for i in l:
if i in dic:
dic[i] += i
else:
dic[i] = i
m = max(list(dic.values()))
x = []
for i in dic:
if dic[i] == m:
x.append(i)
return sorted(x)``````

Disclaimer:- We do not claim 100% surety of solutions, these solutions are based on our sole expertise, and by using posting these answers we are simply looking to help students as a reference, so we urge to do your assignment on your own.