The Joy of Computing using Python | Week 7

Session: JAN-APR 2024

Course Name: The Joy of Computing using Python

Q1. The Chaupar game, also known as Pachisi, involves a player moving their pieces around a cross-shaped board based on the throw of dice (or cowrie shells). The objective is to reach the centre of the board. The steps required to implement the game are given belowArrange them in correct order of implementation. The answer should be a sequence with the step numbers (Example :132)

1. Roll the dice (or simulate the throw of cowrie shells) to determine the number of steps the player can take. Move the player’s piece based on the dice roll. Check if the new position is within the board limits. If it is, proceed; otherwise, stay at the current position.

2. Create a cross-shaped board with four arms, each having three columns of eight squares. Initialize the player’s position to be inside the board at position 0..Define the centre of the board (position 24) as the goal.

3. Check if the player has reached or exceeded the centre of the board (position 24). If the player has reached the centre, declare them the winner. If not, proceed to the next players turn and roll the die again and repeat the steps for each player

Q2. Given Below is a simple algorithm for a Snake and Ladder game
I)Create a board with positions numbered from 1 to 100. Define the positions of snakes and ladders on the board. Each snake or ladder connects two positions.
II) Roll the dice to get a random number between 1 and 6. Move the player’s piece forward by the number obtained from the dice roll.

Check if the new position contains the head of a snake or the bottom of a ladder. If it does, move the player to the corresponding position as per the snake or ladder.
III)Check if the player has reached or exceeded position 100. If the player has reached or exceeded 100 declare them the winner. If not, proceed to the next player’s turn. Repeat the process for each player’s turn until a player wins.

The following variations were suggested for the algorithm.
A. If we are using a octahedral dice instead of a normal die, Step II) has to be changed to generate a random number between 1 and 8
B. Modification is required only in step I) if we use a board consisting of 200 tiles instead of 100
C. Modification is required in both steps I) and III) if we use a board consisting of 200 tiles instead of 100
Identify the correct Statements

A and B
Only B
A and C
Only C

These are the Joy of Computing using Python Assignment 7 Answers

Q3. Nishan enjoys the game of snake and ladders but feels that the player should be given an extra chance if the player lands on a prime numbered tile (Assume that no prime-numbered tile has a snake or ladder in it).
He generates the following function is_prime(position) to check if the position of the player is prime or not. However, there is a chance that there could be a small error in the code. Identify the line number corresponding to the error, in the absence of an error answer -1

Q4. Find the error in the line of code written using the library turtle to draw a square. In the absence of an error enter the answer as -1.

These are the Joy of Computing using Python Assignment 7 Answers

Q5. Like the spiral pattern, the matrix can be printed in many patterns and forms. In the snake pattern, the elements of the row are first printed from left to right and then the elements of the next row are printed from right to left and so on.
A crucial step in the algorithm to print the snake pattern is to check the row parity (remainder when the row number is divided by 2). Identify the correct statements regarding this step. (Assume the normal row indexing in a matrix starting from 0) [MSQ]

When parity is 0, we print the elements of the row from left to right
When parity is 1, we print the elements of the row from left to right
When parity is 0, we print the elements of the row from right to left
When parity is 1, we print the elements of the row from right to left

Q6. Which of these conditions guarantee that the element is a boundary element of the matrix of order m by n? Take i as the row index and j as the column index of the element of to be checked [MSQ]
i==0
i== m-1
j==0
j==n-1

These are the Joy of Computing using Python Assignment 7 Answers

Q7. Given a set of 4 integers as input, the following code tries to print the elements in the form of a square matrix of order 2 . But the code might contain errors. Identify the line number corresponding to the error, In the absence of an error give your answer as -1

Q8. How can you write data to a CSV file using the `csv` module?
write_csv_file()
csv.writer()
csv.write()
write_csv()

These are the Joy of Computing using Python Assignment 7 Answers

Q9. Which object is commonly used for reading rows from a CSV file in Python?
‘csv.parser’
‘csv.handler’
‘csv.iterator’

Q10. Identify the correct statement(s) regarding the gmplot library
I) gmplot is a Python library that allows you to plot data on Google Maps.
II) The syntax for creating a base map with gmplot is
the parameters are the geographical coordinates i.e., Latitude and Longitude and the zoom resolution

I only
II only
Both I and II
Neither I nor II

These are the Joy of Computing using Python Assignment 7 Answers

The Joy of Computing using Python Programming Assignment

Question 1

In spreadsheets, columns are labelled as follows:
A is the first column, B is the second column, Z is the 26th column, and so on. Using the table given above, deduce the mapping between column labels and their corresponding numbers. Accept the column label as input and print the corresponding column number as output.

Solution:

``````def letter_value(letter):
return ord(letter) - ord('A') + 1

def string_value(s):
n = len(s)
value = 0
for i in range(n):
value += letter_value(s[i]) * (26 ** (n - i - 1))
return value

code = input().upper()
print(string_value(code),end="")``````

These are the Joy of Computing using Python Assignment 7 Answers

Question 2

A queue at a fast-food restaurant operates in the following manner. The queue is a single line with its head at the left-end and tail at the right-end. A person can exit the queue only at the head (left) and join the queue only at the tail (right). At the point of entry, each person is assigned a unique token.
You have been asked to manage the queue. The following operations are permitted on it:
The queue is empty to begin with. Keep accepting an operation as input from the user until the string “END” is entered.

Solution:

``````class Queue:
def __init__(self):
self.queue = []

def join(self, token):
self.queue.append(token)

def leave(self):
if self.queue:
self.queue.pop(0)

def move(self, token, position):
if token in self.queue:
self.queue.remove(token)
self.queue.insert(0, token)
elif position.upper() == 'TAIL':
self.queue.append(token)

def print_queue(self):
print(",".join(map(str, self.queue)))

def main():
q = Queue()
while True:
operation = input().split(",")
if operation[0].upper() == "END":
break
elif operation[0].upper() == "JOIN":
q.join(operation[1])
elif operation[0].upper() == "LEAVE":
q.leave()
elif operation[0].upper() == "MOVE":
q.move(operation[1], operation[2])
elif operation[0].upper() == "PRINT":
q.print_queue()

chalo=main()``````

These are the Joy of Computing using Python Assignment 7 Answers

Question 3

Accept a string of lowercase letters from the user and encrypt it using the following image:
Each letter in the string that appears in the upper half should be replaced with the corresponding letter in the lower half and vice versa. Print the encrypted string as output.

Solution:

``````letter_map = {
'a': 'z', 'b': 'y', 'c': 'x', 'd': 'w', 'e': 'v', 'f': 'u', 'g': 't', 'h': 's', 'i': 'r', 'j': 'q', 'k': 'p', 'l': 'o', 'm': 'n', 'n': 'm', 'o': 'l', 'p': 'k', 'q': 'j', 'r': 'i', 's': 'h', 't': 'g', 'u': 'f', 'v': 'e', 'w': 'd', 'x': 'c', 'y': 'b', 'z': 'a'
}

def encrypt_message(message):
encrypted_message = ""
for char in message:
if char.isalpha():

encrypted_message += letter_map[char]
else:

encrypted_message += char
return encrypted_message

message = input()

encrypted_message = encrypt_message(message)
print( encrypted_message,end="")``````

These are the Joy of Computing using Python Assignment 7 Answers

Session: JULY-DEC 2023

Course Name: The Joy of Computing using Python

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q1. Values of CSV files are separated by?
Commas
Colons
Semi-colons
Slash

Q2. What is the output of the following code?
1, 2, 3, 7, 11, 10, 9, 5, 6
1, 2, 3, 5, 6, 7, 9, 10, 11
1, 5, 9, 10, 11, 7, 3, 2, 6
1, 5, 9, 2, 6, 10, 3, 7, 11

Answer: 1, 5, 9, 10, 11, 7, 3, 2, 6

Q3. What will be the output of the following code?
Scalar triangle
Right angle triangle
Equilateral triangle
Isosceles triangle

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q4. Which of the following program will draw a hexagon?

Q5. Which of the following library is used to render data on google maps?
gplot
gmplot
gmeplot

Q6. What is the output of the following code?

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q7. Which turtle command is equivalent to lifting up a pen.
penlift()
penup()
uppen()
penremove()

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q8. Why do we use functions?
To reuse code blocks.
For the ease of code debugging.
All of the above

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q9. Library used to import images?
PIL
Imageview
IMG
image

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q10. In snakes and ladder what can be the ways to track ladders and snakes?
Maintain a dictionary with snakes or ladder number blocks as keys.
Using the if condition to check on every number.
Both A and B.
None of the above

These are answers of The Joy of Computing using Python Assignment 7 Answers

The Joy of Computing using Python Programming Assignmnet

Question 1
Given a sqaure matrix M, write a function DiagCalc which calculate the sum of left and right diagonals and print it respectively.(input will be handled by us)
Input:
A matrix M
[[1,2,3],[3,4,5],[6,7,8]]
Output
13
13
Explanation:
Sum of left diagonal is 1+4+8 = 13
Sum of right diagonal is 3+4+6 = 13

Solution:

``````def DiagCalc(L):
L_sum=0
R_sum=0
m=L[::-1]
for i in range(len(L)):
for y in range(len(L)):
if i==y:
L_sum+=L[i][y]
R_sum+=m[i][y]
print(L_sum)
print(R_sum,end="")``````

These are answers of The Joy of Computing using Python Assignment 7 Answers

Question 2
Given a matrix M write a function Transpose which accepts a matrix M and return the transpose of M.
Transpose of a matrix is a matrix in which each row is changed to a column or vice versa.
Input
A matrix M
[[1,2,3],[4,5,6],[7,8,9]]
Output
Transpose of M
[[1,4,7],[2,5,8],[3,6,9]]

Solution:

``````import numpy
from array import *
def Transpose(M):
arrays = numpy.array(M)
num_list = arrays.transpose().tolist()
return(num_list)``````

These are answers of The Joy of Computing using Python Assignment 7 Answers

Question 3
Given a matrix M write a function snake that accepts a matrix M and returns a list which contain elements in snake pattern of matrix M. (See explanation to know what is snake pattern)
Input
A matrix M
91 59 21 63 81 39 56 8 28 43 61 58 51 82 45 57
Output
[91, 59, 21, 63, 8, 56, 39, 81, 28, 43, 61, 58, 51, 82, 45, 57]
Explanation:
For row 1 elements are inserted from left to right
For row 2 elements are inserted from right to left
For row 3 elements are inserted form left to right
and so on

Solution:

``````def snake(M):
sk=0
sm=[]
for r in M:
m=[]
sk=sk+1
if sk%2==0:
m=r[::-1]
else:
m=r
sm+=m
return(sm)``````

These are answers of The Joy of Computing using Python Assignment 7 Answers

Session Jan-Apr 2023

Course Name: The Joy of Computing using Python

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q1. Which of the following is/are uses of functions?a. Gives a higher-level overview of the task to be performedb. Reusability- uses the same functionality at various placesc. A better understanding of coded. All of the abovee. None of the above

Answer: d. All of the above

Q2. What is the output of the following spiral print python function?

a. 1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11
b. 1 2 3 4 5 6 12 18 17 16 15 14 13
c. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
d. 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Answer: b. 1 2 3 4 5 6 12 18 17 16 15 14 13

Q3. Which of the following library moves the turtle backward?
a. turtle.back(distance)
b. turtle.bk(distance)
c. turtle.backward(distance)
d. All of the above

Answer: d. All of the above

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q4. Which of the following library has to be imported to plot the route map using GPS locations in python?
a. gmplot
b. csv
c. both
d. None

Q5. bytes, bytearray, memoryview are type of the _ data type.
a. Mapping Type
b. Boolean Type
c. Binary Types
d. All of the above
e. None of the above

Q6. In the Snakes and Ladders game, the least number of times a player has to roll a die with the following ladder positions is _ ladders = { 3: 20, 6: 14, 11: 28, 15: 34, 17: 74, 22: 37, 38: 59, 49: 67, 57: 76, 61: 78, 73: 86, 81: 98, 88: 91 }
a. 4
b. 5
c. 6
d. 7

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q7. Which of the following code snippet will create a tuple in python?
d. None of the above

Q8. What does the following program plot?

a. Plots the random number generated in each iteration
b. Plots the number of times the given input matches with the random number generated
c. Plots the input entered for each iteration
d. none of the above

Answer: b. Plots the number of times the given input matches with the random number generated

Q9. Sentiment analysis involves working with ______.
a. a piece of information is useful or not
b. a piece of information is biased or unbiased
c. a piece of information is true or false
d. a piece of information is positive or negative

Answer: d. a piece of information is positive or negative

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q10. What does the following code snippet in python compute

a. checks whether the two given texts are the same
b. searches for text2 in text1
c. finds all the occurrences of text2 in text1
d. none of the above

Answer: c. finds all the occurrences of text2 in text1

The Joy of Computing using Python Programming Assignmnet

Question 1

Given a sqaure matrix M, write a function DiagCalc that calculates the sum of left and right diagonals and prints it respectively.
Input:
A Number M
A Matrix with M rows and M columns
[[1,2,3],[3,4,5],[6,7,8]]
Output
13
13
Explanation:
Sum of left diagonal is 1+4+8 = 13
Sum of right diagonal is 3+4+6 = 13

Solution:

``````def DiagCalc(M):
countL = 0
countR = 0
for i in range(len(M)):
countL += M[i][i]
countR += M[i][-1 - i]

print(countL)
print(countR)

n = int(input())
M = []
for i in range(n):
L = list(map(int, input().split()))
M.append(L)

DiagCalc(M)``````

These are answers of The Joy of Computing using Python Assignment 7 Answers

Question 2

Given a matrix M write a function Transpose which accepts a matrix M and return the transpose of M.
Transpose of a matrix is a matrix in which each row is changed to a column or vice versa.
Input
A matrix M
[[1,2,3],[4,5,6],[7,8,9]]
Output
Transpose of M
[[1,4,7],[2,5,8],[3,6,9]]

Solution:

``````def Transpose(M):
rowlen = len(M)
collen = len(M[0])
ans  = [[] for i in range(collen)]
for i in range(collen):
for j in range(rowlen):
ans[i].append(M[j][i])
return ans``````

These are answers of The Joy of Computing using Python Assignment 7 Answers

Question 3

Given a matrix M write a function snake that accepts a matrix M and returns a list which contain elements in snake pattern of matrix M. (See explanation to know what is a snake pattern)
Input
A matrix M

Output
[91, 59, 21, 63, 8, 56, 39, 81, 28, 43, 61, 58, 51, 82, 45, 57]
Explanation:
For row 1 elements are inserted from left to right
For row 2 elements are inserted from right to left
For row 3 elements are inserted form left to right
and so on

Solution:

``````def snake(M):
rowlen = len(M)
collen = len(M[0])
ans  = []

for i in range(rowlen):
if i%2 == 0:
for j in M[i]:
ans.append(j)
else:
l = M[i]

for j in range(-1, -collen-1, -1):
ans.append(l[j])
return ans``````

These are answers of The Joy of Computing using Python Assignment 7 Answers

Session JUL-DEC 2022

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q1. How ladders & snakes are represented by the instructor?
a. Through lists.
b. Through dictionaries.
c. Through if and elif conditions.
d. Through sets.

Q2. Which of the following is the correct full form of CSV?
a. Comma separated values.
b. Colon separated values.
c. Semi-Colons separated values.
d. Tab separated values.

Q3. Why do we use functions?
b. To reuse code blocks.
c. For the ease of code debugging.
d. For fun.

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q4. In snakes and ladders, what can be the other ways, except for one used by the instructor, to keep track of ladders and snakes?
a. Maintain a dictionary with snakes or ladder number blocks as keys.
b. Using the if condition to check on every number.
c. Using lists.
d. None of the above

Q5. Which of the following libraries is used for animation?
a. Matplotlib
b. Turtle
c. Random
d. PIL

Q6. The spiral animation problem can be broken down into?
a. A list.
b. A 2D table.
c. A 3D table.
d. A dictionary.

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q7. What is the purpose of the GPS program shown in the lectures?
a. To show directions to the user.
b. To track the directions of the user.
c. To show the way to the user on maps.
d. None of the above.

Q8. What does the following program will do after execution?
a. Rename snakesraw.png as snakes.png
b. Make a copy of snakesraw.png with the name snakes.png
c. Make a copy of snakesraw.png with the name snakes.jpeg
d. Throws an error

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q9. Which of the following code will draw a star?

These are answers of The Joy of Computing using Python Assignment 7 Answers

Q10. Which method is used to fill color in shapes drawn by the turtle?
a. color
b. fillcolor
c. changecolor
d. putcolor

These are answers of The Joy of Computing using Python Assignment 7 Answers

Programming Assignment Questions

Question 1
Given a sqaure matrix M, write a function DiagCalc which calculate the sum of left and right diagonals and print it respectively.(input will be handled by us)
Input:
A matrix M
[[1,2,3],[3,4,5],[6,7,8]]
Output
13
13

Solution:

``//Code``

These are answers of The Joy of Computing using Python Assignment 7 Answers

Question 2
Given a matrix M write a function Transpose which accepts a matrix M and return the transpose of M.
Transpose of a matrix is a matrix in which each row is changed to a column or vice versa.
Input
A matrix M
[[1,2,3],[4,5,6],[7,8,9]]
Output
Transpose of M
[[1,4,7],[2,5,8],[3,6,9]]

Solution:

``//Code``

These are answers of The Joy of Computing using Python Assignment 7 Answers

Question 3
Given a matrix M write a function snake that accepts a matrix M and returns a list which contain elements in snake pattern of matrix M. (See explanation to know what is snake pattern)

Solution:

``//Code``

These are answers of The Joy of Computing using Python Assignment 7 Answers

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

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