Table of Contents

## NPTEL The Joy Of Computing Using Python Assignment 6 Answers (Week 6)

**Q1. The following code will**

```
import string
def shift (word, value):
letters = string.ascii_lowercase
new =''
for i in range (len (word)):
if word[i] in letters:
index= letters.index (word [i])
new = new + letters [(index+value)&26]
else:
new = new + word[i]
return new
```

a. Shift every letter in a given word by ‘value’.

b. Shift every letter in a given word by 1.

c. Shift every letter in a given word by 26.

d. Returns the same word.

**Answer:** a. Shift every letter in a given word by ‘value’.

**Follow Sikshapath’s Telegram Channel for instant notifications of future updates.**

**Q2. What will be the output of the following code if the input is ‘The Joy OF Computing’ ?**

```
import string
def test (word):
lower = string.ascii_lowercase
upper = string.ascii_uppercase
new_word =''
for ch in word:
if(ch in lower):
index= Lower.index(ch)
new_word = new_word + lower [(index+2) % 26]
elif(ch in upper):
index= upper.index(ch)
new_word = new_word + upper [(index+3) % 26]
else:
new_word = new_word + ch
return new_word
print(test('The Joy OF Computing'))
```

a. Wjg Mqa Rh Fqorwvkpi

b. vjg lqa eh eqorwvkpi

c. Wjg Mqa RI Fqorwvkpi

d. vjg Mqa RI Fqorwvkpi

**Answer:** c. Wjg Mqa RI Fqorwvkpi

**Q3. Which of the following is true about recursion?**

a. Recursion increases the speed of the program.

b. Recursion decreases the speed of the program.

c. Speed of the program remains the same.

d. Recursion is easier to understand than non-recursive programs.

**Answer:** b. Recursion decreases the speed of the program.

**Q4.** **What will be the output of the following program?**

```
def recursive (num):
if(num == 1):
return 1
return num*(num-1)
```

a. Calculating sum of first n terms.

b. Calculating product of first n terms.

c. Calculating power of first n terms.

d. Calculating sum of last n terms.

**Answer:** b. Calculating product of first n terms.

**Q5. What will be the output of the following program?**

a. 55

b. 45

c. Infinite loop

d. 50

**Answer:** a. 55

**Q6. What is the output of the following program?**

a. 3628800

b. Runs into infinite loop

c. 55

d. Syntax Error

**Answer:** b. Runs into infinite loop

**Q7. What’s the correct code for Binary search?**

a.

```
def Binary(L, find, start, end):
mid = int((start+end)/2)
if(start < end):
if(L[end] == find):
return end
else:
return-100
if(L[mid] == find):
return mid
elif(find > L[mid]):
return Binary(L, find, mid+1, end)
else:
return Binary(L, find, start, mid-1)
```

b.

```
def Binary(L, find, start, end):
mid = int((start+end)/2)
if(start == end):
if(L[end] == find):
return end
else:
return-100
if(L[mid] == find):
return mid
elif(find > L[mid]):
return Binary(L, find, start, mid-1)
else:
returnBinary(L, find, mid+1, end)
```

c.

```
def Binary(L, find, start, end):
mid = int((start+end)/2)
if(start == end):
if(L[end] == find):
return end
else:
return-100
if(L[mid] == find):
return mid
elif(find > L[mid]):
return Binary(L, find, mid+1, end)
else:
return Binary(L, find, start, mid-1)
```

d.

```
def Binary(L, find, start, end):
mid = int((start+end)/2)
if(start >= end):
if(L[end] == find):
return end
else:
return-100
if(L[mid] != find):
return mid
elif(find > L[mid]):
return Binary(L, find, mid+1, end)
else:
return Binary(L, find, start, mid-1)
```

**Answer:** (c.)

**Q8. What the following code will do?**

a. Returns product of elements from 5th index onwards.

b. Returns product of elements from 1st to 5th index.

c. Returns product of elements from 6th index onwards.

d. Returns product of elements from 4th index onwards.

**Answer:** a. Returns product of elements from 5th index onwards.

**Q9. A programme which is written in a recursive manner cannot be written in a non-recursive manner.**

a. True

b. False

**Answer:** b. False

**Q10. What will be the output of the following program?**

a.

```
**********
*********
********
*******
******
*****
****
***
**
*
```

b.

```
*********
*******
*****
***
*
```

c. Runs into infinite loop

d.

```
**********
********
******
****
**
*
```

**Answer:** (d.)

### The Joy Of Computing Using Python Week 6 Programming Assignment

**Q1. Aman likes to study about planets. Every night he goes outside and observe some planets with his telescope. Then he guesses the distance of each planet and pen it down. In this process he also pen down his favourite planet position. Given the distance of each planet to be unique you need to return position of Aman’s favourite planet after sorting all the distances.**

**Program:**

`print(sorted(L).index(L[K-1])+1,end="")`

**Q2. Romeo and Juliet love each other. Romeo wants to send a message to Juliet and also don’t want anyone to read it without his permission. So he shifted every small letter in the sentence by -2 position and every capital letter by -3 position. (If the letter is c, after shifting to by -2 position it changes to a, and for D new letter will be A).But the letter is too long and Romeo does not have enough time to encrypt his whole letter. Write a program to help Romeo which prints the encrypted message. You can assume there are no special characters except spaces and numeric value**

**Program:**

```
Cap=list("ABCDEFGHIJKLMNOPQRSTUVMWXYZ")
Small=list('abcdefghijklmnopqrstuvwxyz')
ans=""
for a in S:
if a=='a':
ans+='y'
elif a=='b':
ans+='z'
elif a=='A':
ans+='X'
elif a=='B':
ans+='Y'
elif a=='C':
ans+='Z'
elif a=='W':
ans+='T'
else:
if a in Cap:
ans+=Cap[Cap.index(a)-3]
elif a in Small:
ans+=Small[Small.index(a)-2]
else:
ans+=a
print(ans,end="")
```

**Q3. write a function whole(N) which takes a number N and return the sum of first N whole number. Write the program using recursion.**

**Program:**

```
def whole(N):
return(sum(list(range(N+1))))
```

**Disclaimer:** These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself.

**Also Available:**

Data Science for Engineers NPTEL Assignment Solutions Week 6

Data Science for Engineers NPTEL Assignment Solutions Week 7