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]
return(mymax)```

`[-3,-2,-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])
return(l)```

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

OR

[(2,2),(2,3),(2,4)]```

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
return(mythirdmin)```

Answer(Indent it to the function level) :-

```    l.sort()
mythirdmin = l[2]
break```

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:
return([])
else:
return(...)
```
Solution:-
```[l[0]] + evenpositions(l[2:])
```

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```

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 [].

```def uncommon(l1, l2):
l1 = list(set(l1))
l2 = list(set(l2))
l3=[]
for i in l2:
if i not in l1:
l3.append(i)
for i in l1:
if i not in l2 and i not in l3:
l3.append(i)

l3.sort()
return l3```

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:

```the
"Spot the mistake
in the following argument",
Jack challenged
1+(-1+1)+(-1+1)+...
= (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,`

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

```

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.

```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
```
Categories: NPTEL solution

#### krish · September 17, 2017 at 9:01 AM

Thanks again for the second set of solutions… But could you please look into answer 2? It seems to be not correct…

#### arasu su · September 17, 2017 at 9:22 AM

dei question no. 8 i got only 25marks only

#### kkm · September 17, 2017 at 9:31 AM

1,2 question how do i give out put
below myinput

#### Yash Sodha · September 17, 2017 at 9:45 AM

Yes, type it below myinput

#### hasi · September 17, 2017 at 9:35 AM

try tiz for 2
[(2,2),(2,3),(2,4)]

#### prabhakar · September 17, 2017 at 9:45 AM

def do():
store=[]
while(1):
a=input()
if(a==””):
break
store.append(a)
p=store[0]
a=””
for i in range(len(store)-1,0,-1):
if(store[i].find(p)!=-1):
a=store[i]
break
print(a)

do()

#### Yash Sodha · September 17, 2017 at 10:12 AM

All answers work. 100/100. Tested and verified.

#### aasman · September 17, 2017 at 10:18 AM

tnx alot yash sodha for your help… great job.

#### pritam · September 17, 2017 at 10:44 AM

3 rd ans is correct
Just remove break

#### Ankit Rohilla · September 17, 2017 at 9:02 AM

Love you a lot sir.

#### vikram · September 17, 2017 at 9:12 AM

it seems that answer 3 is also wrong

#### Mohan Ram .M · September 17, 2017 at 9:15 AM

[(2,3),(2,1),(1,5),(1,2)]

#### Siva surya · September 17, 2017 at 9:20 AM

[(-1,-2),(-1,2),(2,-6),(0,7)]

#### kkm · September 17, 2017 at 9:49 AM

[-3,-2,-1]
end = len(l) – 1
mymax = l[-1]
for i in range(end,0,-1):
if l[i] > mymax:
mymax = l[i]
return(mymax)
I give like this but
Input Expected Output Actual Output
True\n
False\n
what i do

#### hiranmai · September 17, 2017 at 9:50 AM

just type the output

#### Mit Patel · September 17, 2017 at 10:02 AM

You only have to submit [-3,-2,-1] in the box

#### suriya · September 17, 2017 at 10:06 AM

dude answer for 3 is incorrect for me wat abt u

#### Yash Sodha · September 17, 2017 at 10:17 AM

The answer is correct, you just need to set the indentation.

#### Krishnashree · September 17, 2017 at 9:28 AM

for the question no.7 the score that was given was 25/100.
why is it so?

#### hiranmai · September 17, 2017 at 9:53 AM

anyone pls its urgent

#### hiranmai · September 17, 2017 at 9:56 AM

pls anyone update its urgent

#### suriya · September 17, 2017 at 9:40 AM

for only 6 questions answers are there remaining 2 is not there post it as soon as possible.thank u

#### suriya · September 17, 2017 at 9:48 AM

3 answer seems to be incorrect

#### nope · September 17, 2017 at 9:50 AM

def something():
store=[]
while(1):
a=input()
if(a==””):
break
store.append(a)
p=store[0]
a=””
for i in range(len(store)-1,0,-1):
if(store[i].find(p)!=-1):
a=store[i]
break
print(a)

something()

for seventh

#### arasu su · September 17, 2017 at 9:54 AM

in question no 3,have run time error.

#### hiranmai · September 17, 2017 at 9:59 AM

thank you for updating 7 question

#### parkavi balamurugan · September 17, 2017 at 10:00 AM

thanks a lotzzzzzzz……..

#### Yash Sodha · September 17, 2017 at 10:15 AM

You are welcome. Do check out all Non-NPTEL posts. Thanks 🙂

#### parkavi balamurugan · September 17, 2017 at 10:01 AM

thanks a lotzzz…:)

#### sirushtika · September 17, 2017 at 10:02 AM

thank so… much sir

#### Yash Sodha · September 17, 2017 at 10:16 AM

You are welcome. Do check out all other posts like NFC and Airplane Wi-Fi. Thanks 🙂

#### suriya · September 17, 2017 at 10:19 AM

answer for 3 is incorrect sir

#### aditya · September 17, 2017 at 10:02 AM

thank you sir for the solution

#### Yash Sodha · September 17, 2017 at 10:16 AM

You are welcome. Do check out all other posts like NFC and Airplane Wi-Fi. Thanks 🙂

#### suriya · September 17, 2017 at 10:02 AM

answer for 3 is incorrect so pls post correct answetr.thank u

#### Mit Patel · September 17, 2017 at 10:09 AM

The answer is correct. You just only have to write it in for loop.

#### arasu su · September 17, 2017 at 10:16 AM

write full code for 3

#### viccky · September 17, 2017 at 10:17 AM

i didnt get the correct output
plz explain how to fix this

#### Mit Patel · September 17, 2017 at 10:26 AM

In which question?

#### Yash Sodha · September 17, 2017 at 10:15 AM

The answer is correct. Just the indentation needs to be set

#### Anurag · September 17, 2017 at 10:04 AM

Thank you so much for all the solutions, sir. Please provide the solution for week 8 programming assignment as well. We’ll be very grateful to you.

Thanks a lot again.

#### Yash Sodha · September 17, 2017 at 10:17 AM

It works. Just the indentation needs to be set.

ok sir

#### Yash Sodha · September 17, 2017 at 10:13 AM

You are welcome. Keep supporting 🙂

#### aasman · September 17, 2017 at 10:08 AM

thanks alot to yash @Yash Sodha and all other ppls who solved this questions ..great job..well done.

#### Yash Sodha · September 17, 2017 at 10:17 AM

You are welcome. Do check out all other Non-NPTEL posts like NFC and Airplane Wi-Fi. We will be glad, thanks 🙂

#### Viraj · September 17, 2017 at 10:33 AM

Thanks a lot for all your efforts

#### Viraj · September 17, 2017 at 10:08 AM

Hi there
Also how many of examples they are expecting to be correct?

#### sirushtika · September 17, 2017 at 10:08 AM

sir could you please send us week 8 solutions too

#### sam · September 17, 2017 at 10:15 AM

def thirdmin(l):
(mymin,mysecondmin,mythirdmin) = (1000000,1000000,1000000)
for i in range(len(l)):
# Your code below this line
l.sort()
mythirdmin = l[2]
break
# Your code above this line
return(mythirdmin)

>>> thirdmin([4,1,3,2])

3
>>> thirdmin([13,12,2,17,3,6,8,5])

5
>>>

#### vishal · September 17, 2017 at 10:16 AM

sir question number three answere is giving error
Test Case 1 Runtime Error
Input Expected Output Actual Output
thirdmin([4,1,3,2])
3\n
File “test.py”, line 5\n
l.sort()\n
^\n
TabError: inconsistent use of tabs and spaces in indentation
Test Case 2 Runtime Error
Input Expected Output Actual Output
thirdmin([13,12,2,17,3,6,8,5])
5\n
File “test.py”, line 5\n
l.sort()\n
^\n
TabError: inconsistent use of tabs and spaces in indentation

#### Mit Patel · September 17, 2017 at 10:27 AM

The answer is correct, you just need to set the indentation.

#### suriya · September 17, 2017 at 10:36 AM

post me with indentation i do no

#### sam · September 17, 2017 at 10:45 AM

def thirdmin(l):
(mymin,mysecondmin,mythirdmin) = (1000000,1000000,1000000)
for i in range(len(l)):
# Your code below this line
l.sort()
mythirdmin = l[2]
break
# Your code above this line
return(mythirdmin)

#### vishal · September 17, 2017 at 10:48 AM

i corrected and got correct answere

thanks

#### kani · September 17, 2017 at 10:26 AM

how to do 3 question

thanks a lot

#### dev · September 17, 2017 at 10:36 AM

can you also put 7th question answer

#### Yash Sodha · September 17, 2017 at 10:47 AM

Just indent it or remove break

#### karthi · September 17, 2017 at 10:59 AM

pls send the answer sir for 3

#### vishal · September 17, 2017 at 10:50 AM

Sir can you send a solution of Week Eight programming assignment