# The Joy of Computing using Python | Week 6

Session: JAN-APR 2024

Course Name: The Joy of Computing using Python

#### Q1. What is the output of following code:1493035275025610None of the above

Q2. Which method in Python is used to convert lowercase letters in a string to uppercase?
upper()
capitalize()
casefold()
swapcase()

These are the Joy of Computing using Python Assignment 6 Answers

Q3. The “Min Max” strategy, often referred to as the minimax algorithm, is a decision-making algorithm used in game theory and artificial intelligence. It’s commonly employed in two-player games with alternate turns and perfect information, such as chess or tic-tac-toe. What is the primary objective of the min max algorithm?
Maximizing the player’s score
Minimizing the opponent’s score
Minimizing the maximum possible loss
Maximizing the number of moves

Answer: Minimizing the maximum possible loss

Q4. Typecasting refers to the conversion of one data type to another. Python provides several built-in functions for typecasting, such as int(), float(), str(), list(), tuple(), set(), and dict() among others, to convert between different data types. What would be the output of the following code?
123
ValueError
SyntaxError
NameError

These are the Joy of Computing using Python Assignment 6 Answers

Q5. What term is used in programming languages when a function calls itself?
Self-referencing
Iteration
Recursion
Circular referencing

Q6. Consider the following snippet of code.
If L is a non-empty list of positive integers, which of the following statements is correct about the recursive function func(L)?

It returns the total number of odd elements in the list L
It returns the total number of even elements in the list L
It returns the sum of the even elements in the list L
It returns the sum of the odd elements in the list L

Answer: It returns the total number of even elements in the list L

These are the Joy of Computing using Python Assignment 6 Answers

Q7. Consider the following snippet of code.
If L is a non-empty list of positive integers, which of the following statements is correct about the recursive function func(L)?

It returns the total number of odd elements in the list L
It returns the total number of even elements in the list L
It returns the sum of the even elements in the list L
It returns sum of the odd elements in the list L

Answer: It returns sum of the odd elements in the list L

Q8. What purpose does a base case serve in recursive functions?
To make the function run faster
To eliminate the need for additional functions
To prevent infinite recursion and ensure termination
To enable the function to handle complex calculations

Answer: To prevent infinite recursion and ensure termination

These are the Joy of Computing using Python Assignment 6 Answers

Q9. What is the value returned by f(10000)?

Q10. What is the value returned by f(-8)?

These are the Joy of Computing using Python Assignment 6 Answers

### The Joy of Computing using Python Programming Assignment

Question 1

The distance between two letters in the English alphabet is one more than the number of letters between them. Alternatively, it can be defined as the number of steps needed to move from the alphabetically smaller letter to the larger letter. This is always a non-negative integer. For example:
The distance between two words is defined as follows:
It is -1, if the words are f unequal lengths.
If the word-lengths are equal, it is the sum of the distances between letters at corresponding positions in the words. For example:
​dword​(dog,cat)=dletter​(d,c)+dletter​(o,a)+dletter​(g,t)=1+14+13=28

Write a function named distance that accepts two words as arguments and returns the distance between them.
You do not have to accept input from the user or print output to the console. You just have to write the function definition.

Solution:

``````def distance(word1, word2):
if len(word1) != len(word2):
return(-1)

dis = 0
for i in range(len(word1)):
dis += abs(ord(word1[i]) - ord(word2[i]))

return(dis)``````

These are the Joy of Computing using Python Assignment 6 Answers

Question 2

P is a dictionary of father-son relationships that has the following structure: for any key in the dictionary, its corresponding value is the father of key.

Solution:

``````def ancestry(P, present, past):
if present == past:
return([present])

if present not in P:
return(None)
ancestral_chains = ancestry(P, P[present], past)
if ancestral_chains:
return([present] + ancestral_chains)
return(None)``````

These are the Joy of Computing using Python Assignment 6 Answers

Question 3

Fibonacci is a young resident of the Italian city of Pisa. He spends a lot of time visiting the Leaning Tower of Pisa, one of the iconic buildings in the city, that is situated close to his home. During all his visits to the tower, he plays a strange game while climbing the marble steps of the tower.
You do not have to accept input from the user or print output to the console. You just have to write the function definition.

Solution:

``````def steps(n):
if n <= 0:
return 0
elif n == 1:
return 1
elif n == 2:
return 2
elif n == 3:
return 4
one_steps = steps(n-1)
two_steps = steps(n-2)
three_steps = steps(n-3)
return(one_steps + two_steps + three_steps)``````

These are the Joy of Computing using Python Assignment 6 Answers

Session: JULY-DEC 2023

Course Name: The Joy of Computing using Python

These are the Joy of Computing using Python Assignment 6 Answers

Q1. What will be the output of the following code?
Shift every letter in a given word by value.
Shift every letter in a given word by 1.
Shift every letter in a given word by 26.
Returns the same word.

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

Q2. In the list L = [4,6,7,4,6,2,1], What is the index of element ‘7’?
0
1
2
3

Q3. Which of the following is true about recursion?
Recursion always performs better than non-recursive code.
Recursive code is easier to debug.
The base case is necessary for recursion.
Recursive code can be shorter than non-recursive code

These are the Joy of Computing using Python Assignment 6 Answers

Q4. What will be the output of the following program?
Calculating sum of first n terms.
Calculating product of first n terms.
Calculating power of first n terms.
Calculating sum of last n terms.

Answer: Calculating product of first n terms.

Q5. In Caesar cipher,the mediator needs to make maximum of how many trails to break the code?
1
26
no trail needed
10

Q6. What is the output of the following program?
3628800
Runs into an infinite loop
55
Syntax error

Answer: Runs into an infinite loop

These are the Joy of Computing using Python Assignment 6 Answers

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

These are the Joy of Computing using Python Assignment 6 Answers

Q8. Which of the following is TRUE about MIN-MAX strategy?
Maximize the chances of your winning and minimize the changes of the opponent winning
The game with min-max strategy can never be drawn
Minimize the chances of your winning and maximize the chances of the opponent winning
All the above are true

Answer: Maximize the chances of your winning and minimize the changes of the opponent winning

These are the Joy of Computing using Python Assignment 6 Answers

Q9. A program that is written recursively cannot be written in a non-recursive manner.
True
False

These are the Joy of Computing using Python Assignment 6 Answers

Q10. what will be the output of the following program?

**********
********
******
****
**
*

These are the Joy of Computing using Python Assignment 6 Answers

### The Joy of Computing using Python Programming Assignmnet

Question 1
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.
Input:
N (No of planets)
L (List of distances of planet)
K (position of Aman’s favourite planet in unsorted list)
Output:
Position of Aman’s favourite planet in sorted List.
Example:
Input
5
[4,5,3,2,1]
2
Output
5
Explanation:
The position of aman’s favourite planet in unsorted list is 2 and list of distances of planets at position 2 the distance is 5. After sorting the list [1,2,3,4,5] the position of aman’s favourite is changed to 5.
Note: Index is different from position. Indexing of the list starts from 0 whereas positioning in the list start form 1.

Solution:

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

These are the Joy of Computing using Python Assignment 6 Answers

Question 2
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.

Input
A string S
Output
Encrypted string
Example
Input
Hello Juliet
Output
Ecjjm Gsjgcr
Explanation
H is shifted by -3 position and changed to E. ‘e’ is shifted by -2 position and changed to c and so on.

Solution:

``````Cap1=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 Cap1:
ans+=Cap1[Cap1.index(a)-3]
elif a in Small:
ans+=Small[Small.index(a)-2]
else:
ans+=a
print(ans,end="")``````

These are the Joy of Computing using Python Assignment 6 Answers

Question 3
write a function whole(N) which takes a number N and return the sum of first N whole number. Write the program using recursion.
Input
N
Output
sum of whole numbers till N
Example:
Input
3
Output
6
Explanation
Sum of first 3 natural numbers are 0+1+2+3 = 6

Solution:

``````def whole(n):
if n==0:
return 0
else:
return n+whole(n-1)``````

These are the Joy of Computing using Python Assignment 6 Answers

Session Jan-Apr 2023

Course Name: The Joy of Computing using Python

These are the Joy of Computing using Python Assignment 6 Answers

#### Q1. Which of the following is true about recursion?a. Recursion always performs better than non-recursive code.b. Recursive code can be reused.c. The base case is necessary for recursion.d. Recursive code can be shorter than non-recursive code

Q2. If PYTHON is encoded by TCXLSR then DIAMOND will be encoded as?
a. EJBNPOE
b. FKCORPF
c. HMERTSH
d. HMEQSRH

Q3. Let L be a list containing different names of movies. Which statement is correct to select a random movie name from that list L?
a. random.choices(L)
b. random.select(L)
c. random.movie(L)
d. random.random(L)

These are the Joy of Computing using Python Assignment 6 Answers

Q4. In the list L = [4,6,7,4,6,2,1], What is the index of element ‘7’?
a. 0
b. 1
c. 2
d. 3

Q5. What will be the output of the following code?

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.

Q6. Library used to import images?
a. PIL
b. Imageview
c. IMG
d. image

These are the Joy of Computing using Python Assignment 6 Answers

Q7. Values of CSV files are separated by?
a. Commas
b. Colons
c. Semi-colons
d. Slash

Q8. what will be the output of the following program?

a. **********
*********
********
*******
******
*****
****
***
**
*
b. *********
*******
*****
***
*
c. Runs into infinite loop
d. **********
********
******
****
**
*

********
******
****
**
*

Q9. What will happen if we don’t check for a base case in recursion.
a. The program will run smoothly
b. The program will return a wrong output.
c. The program will enter into an infinite loop.
d. The program will never run.

Answer: c. The program will enter into an infinite loop.

These are the Joy of Computing using Python Assignment 6 Answers

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

### The Joy of Computing using Python Programming Assignmnet

Question 1

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.

Input:
N (No of planets)
L (List of distances of planet)
K (position of Aman’s favourite planet in unsorted list)
Output:
Position of Aman’s favourite planet in sorted List.
Example:
Input
5
[4,5,3,2,1]
2
Output
5
Explanation:
The position of aman’s favourite planet in unsorted list is 2 and list of distances of planets at position 2 the distance is 5. After sorting the list [1,2,3,4,5] the position of aman’s favourite is changed to 5.
Note: Index is different from position. Indexing of the list starts from 0 whereas positioning in the list start form 1.

Solution:

``````N = int(input())
L = [int(i) for i in input().split()]
K = int(input())
position = L[K-1]
L.sort()
for i in range(N):
if L[i] == position:
print(i+1)
break``````

These are the Joy of Computing using Python Assignment 6 Answers

Question 2

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 lower-case letter in the sentence by -2 position and every upper-case 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 that prints the encrypted message. You can assume there are no special characters except spaces and numeric value.

Input
A string S
Output
Encrypted string
Example
Input
Hello Juliet
Output
Ecjjm Gsjgcr
Explanation
H is shifted by -3 position and changed to E. ‘e’ is shifted by -2 position and changed to c and so on.

Solution:

``````S = input()
import string

low = string.ascii_lowercase
cap = string.ascii_uppercase

ans  = ''

for i in S:
if i in low:
index = low.index(i)
index = ((index-2)+26)%26
ans+=low[index]
elif i in cap:
index = cap.index(i)
index = ((index-3)+26)%26
ans+=cap[index]
else:
ans+=i

print(ans)``````

These are the Joy of Computing using Python Assignment 6 Answers

Question 3

write a function whole(N) which takes a number N and return the sum of first N whole number. Write the program using recursion.
Input
N
Output
sum of whole numbers till N
Example:
Input
3
Output
6
Explanation
Sum of first 3 natural numbers are 0+1+2+3 = 6

Solution:

``````def whole(n):
if n==0:
return 0
else:
return n+whole(n-1)
N = int(input())
print(whole(N))``````

These are the Joy of Computing using Python Assignment 6 Answers

Session JUL-DEC 2022

#### Course name: The Joy of Computing Using Python

These are the Joy of Computing using Python Assignment 6 Answers

Q1. In the list L = [4,6,7,4,6,2,1], What is the index of element ‘7’?
a. 0
b. 1
c. 2
d. 3

Q2. Which of the following is true about recursion?
a. Recursion always performs better than non-recursive code.
b. Recursive code is easier to debug
c. The base case is necessary for recursion.
d. Recursive code can be shorter than non-recursive code.

These are the Joy of Computing using Python Assignment 6 Answers

Q3. What will be the output of the following program?

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

Q5. What’s the correct code for Binary search?

These are the Joy of Computing using Python Assignment 6 Answers

Q6. Which of the following can be used to see the dimension of a NumPy array named ‘arr’?
a. dim(arr)
b. shape(arr)
c. arr.shape
d. arr.shape()

Q7. If PYTHON is encoded by TCXLSR then DIAMOND will be encoded as?
a. EJBNPOE
b. FKCORPF
c. HMERTSH
d. HMEQSRH

These are the Joy of Computing using Python Assignment 6 Answers

8. Select the correct statement
1) print(‘9a’.isalnum()) will return True.
2) ‘9a’ contains both alphabetic and numeric parts.

a. Option 1 is correct, option 2 is correct. Option 2 is the correct explanation for option 1.
b. Option 1 is correct, option 2 is incorrect.
c. Option 1 is correct, option 2 is correct. Option 2 is not the correct explanation for option 1.
d. Option 1 is incorrect, option 2 is incorrect.

Q9. What will be the output of the following program?
a. A dictionary with the count of each character in s.
b. A dictionary with the count of each special character in s.
c. A dictionary with the count of letters in s.
d. Error

These are the Joy of Computing using Python Assignment 6 Answers

Q10. Let L be a list containing different names of movies. Which statement is correct to select a random movie name from that list L?
a. random.choices(L)
b. random.select(L)
c. random.movie(L)
d. random.random(L)

These are the Joy of Computing using Python Assignment 6 Answers

## Programming Assignment Questions

Question 1
Given a list L containing integers, write a program that creates and prints a dictionary ‘d’ containing all the numbers that occur twice or more in the list as keys and their indexes as values. Both the keys are and their values should be in the same order as given the list.
You have to take the input.
Input
List
Output
Dictionary D

Solution:

``//Code``

These are the Joy of Computing using Python Assignment 6 Answers

Question 2
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.
Input
A string S
Output
Encrypted string

Solution:

``//Code``

These are the Joy of Computing using Python Assignment 6 Answers

Question 3
Take a string S as an input and print ‘palindrome’ if string S is a palindrome or ‘not palindrome’ if string S is not a palindrome.
A palindrome is a word which spells same from forward and backward. Example DAD.
Input
A string S
Output:
palindrome or not palindrome

Solution:

``//Code``

These are the Joy of Computing using Python Assignment 6 Answers

More NPTEL Solution: https://progiez.com/nptel

 The content uploaded on this website is for reference purposes only. Please do it yourself first.