**1) Given the following permutation of a,b,c,d,e,f,g,h,i,j, what is the next permutation in lexicographic (dictionary) order? Write your answer without any blank spaces between letters.**

bfaijhgedc

Answer(s) : bfajcdeghi

**2) We want to add a function sum() to the class Node that implements user defined lists of numbers which will compute the sum of the values in a list. An incomplete implementation of sum() given below. You have to provide an expression to put in place of *** on the last line. You may assume that the quantities stored in Node.value can be added using the operator +.**

def sum(self): if self.value == None: return(0) elif self.next == None: return(self.value) else: return(***)

Answer(s) : self.value + self.next.sum()

**3) Suppose we add this function foo() to the class Tree that implements search trees. For a name mytree with a value of type Tree, what would mytree.foo() compute?**

def foo(self): if self.isempty(): return(0) elif self.isleaf(): return(1) else: return(1 + max(self.left.foo(),self.right.foo()))

a) The number of nodes in `mytree`.

b) The largest value in `mytree`.

c) The length of the longest path from root to leaf in `mytree`.

d) The number of paths in `mytree`.

Answer(s) : c) The length of the longest path from root to leaf in mytree.

**4) Inorder traversal of a binary tree has been defined in the lectures. A preorder traversal lists the vertices of a binary tree (not necessarily a search tree) as follows:**

Print the root.

Print the left subtree in preorder.

Print the right subtree in preorder.

Suppose we have a binary tree with 9 nodes labelled a, b, c, d, e, f, g, h, i, with preorder traversal bhgifdaec and inorder traversal ghifbeadc.

What is the right child of the root node?

Hint: The preorder traversal tells you the root of the tree. By locating the root in the inorder traversal, you can identify the nodes in the left and right subtrees. In this way, you can recursively reconstruct the tree from the two traversals.

a) `a`

b) `c`

c) `d`

d) `f`

Answer(s) : c) d

## 11 Comments

## Mahesh Kothari · September 11, 2017 at 5:31 PM

sum() function accepts one argument , but how come while doing self.next.sum() no argument is passed ? Wouldn’t this be compilation error ?

## syam · September 13, 2017 at 4:01 PM

can u upload week 8 assignment small dought in there

## Mayuresh · September 15, 2017 at 1:31 PM

Sir please upload week 8 programming assignment and sample test solution

## Mayuresh · September 15, 2017 at 1:33 PM

plz sir

## suresh · September 17, 2017 at 3:26 AM

sir please help us in doing NPTEL PDSA using Python, online test on Sunday 17 September. please upload test solution

## ramesh · September 17, 2017 at 4:28 AM

q1)

9,1,2

q2)

5,5,5,5,5

q3)

elif y<=z:

minimum=y

else:

minimum=z

q4)

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

q5)

def squarefree(n):

k=1

for i in range(2,n-1):

if(n%(i*i)==0):

return(False)

return(True)

q6)

def disjointlist(l1,l2):

for i in range(0,len(l1)):

for j in range(0,len(l2)):

if(l1[i]==l2[j]):

return(False)

return(True)

q7)

lines = []

while True:

line = input()

if line:

lines.append(line)

else:

break

text = '\n'.join(lines)

for i in range((len(lines)//2),len(lines)):

print(lines[i])

for i in range(0,len(lines)//2):

print(lines[i])

## ramesh · September 17, 2017 at 4:30 AM

ans for 8

def maxcount(l):

count=0

l3=[]

flag=0

maxi=0

for i in range(0,len(l)):

for j in range(0,len(l)):

if l[i]==l[j]:

count=count+1

flag=1

if flag==1:

maxi=max(count,maxi)

count=0

return(maxi)

## ramesh · September 17, 2017 at 4:31 AM

for sample test

1:

(1,1,2)

2:

([1,1,1,1,])

3:

if l[i]>mymax:

mymax=l[i]

if l[i]>mysecondmax and l[i]mysecondmax and l[i]<mymax:

mysecondmax=l[i]

4:

mypalindrome(l[1:len(l)-1]) if l[0]==l[len(l)-1] else False

5:

def perfect(n):

l=[]

sum=0

test=n/2

for i in range(1,n-1):

if n%i==0:

l.append(i)

for i in range(0,len(l)):

sum+=l[i]

if sum==n:

return True

else:

return False

6:

def sublist(l1,l2):

for j in range(0,len(l2)):

if l2[j:j+len(l1)] == l1:

return True

return False

7:

lines = []

while True:

line = input()

if line:

lines.append(line)

else:

break

text = '\n'.join(lines)

for i in range(2,len(lines),3):

print(lines[i])

8:

def repeated(l):

count=0

l3=[]

for i in range(0,len(l)):

for j in range(i+1,len(l)):

if l[i]==l[j]:

count=count+1

l=l[:j]+l[j+2:]

break

return(count)

## monika · September 22, 2017 at 10:55 AM

sir plz help with sample test question 6th and 8th

## nisha · September 23, 2017 at 6:35 AM

can you explain the concept of permutations?please

## nisha · September 23, 2017 at 6:36 AM

can you explain the concept of permutations?