# The Joy Of Computing Using Python Week 7 : Assignment 1 | NPTEL | Answers Jan 2023

## Which of the following is/are uses of functions?

a. Gives a higher-level overview of the task to be performed
b. Reusability- uses the same functionality at various places
c. A better understanding of code
d. All of the above
e. None of the above

d. All of the above

Functions provide a way to encapsulate and organize code by breaking it down into smaller, more manageable pieces. This allows for a higher-level overview of the task to be performed, which can improve understanding and maintainability of the code. Functions can also be reused throughout the code, leading to more efficient and modular code.

## What is the output of the following spiral print python function?

```def spiralprint(m, n, spiralmatrix):
k = 0
l = 0
while (k < m and l < n):
for i in range(l, n):
print(spiralmatrix[k][i], end=" ")
k += 1

for i in range(k, m):
print(spiralmatrix[i][n - 1], end=" ")
n -= 1

if (k < m):
for i in range(n - 1, (l - 1), -1):
print(spiralmatrix[m - 1][i], end=" ")
m -= 2

if (l < n):
for i in range(m - 1, k - 1, -1):
print(spiralmatrix[i][l], end=" ")
l += 2

spiralmatrix = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18]]
rows = 3
cols = 6
spiralprint(rows, cols, spiralmatrix)
```
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

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

```This code defines a function named spiralprint which takes three parameters: m, n, and spiralmatrix. The function prints the elements of the given spiralmatrix in a spiral order.

Initially, the function initializes two variables k and l to zero. These variables are used to keep track of the starting row and column of the current loop iteration.

The function uses a while loop to iterate over the matrix elements in a spiral order. The loop condition is that k and l must be less than m and n respectively.

The first loop in the while loop prints the elements of the top row of the current spiral, from left to right. It uses a for loop to iterate over the range from l to n. The end parameter in the print function is set to a space character to print the elements in a single row.

After printing the top row elements, k is incremented to move to the next row.

The second loop in the while loop prints the elements of the rightmost column of the current spiral, from top to bottom. It uses a for loop to iterate over the range from k to m. The n-1 index is used to get the rightmost element of the row. The end parameter in the print function is set to a space character to print the elements in a single column.

After printing the rightmost column elements, n is decremented to move to the next column.

The third loop in the while loop prints the elements of the bottom row of the current spiral, from right to left. It uses a for loop to iterate over the range from n-1 to l-1 (inclusive) in reverse order. The m-1 index is used to get the bottommost element of the row. The end parameter in the print function is set to a space character to print the elements in a single row.

After printing the bottom row elements, m is decremented by 2 to move to the next row.

The fourth loop in the while loop prints the elements of the leftmost column of the current spiral, from bottom to top. It uses a for loop to iterate over the range from m-1 to k-1 (inclusive) in reverse order. The l index is used to get the leftmost element of the column. The end parameter in the print function is set to a space character to print the elements in a single column.

After printing the leftmost column elements, l is incremented by 2 to move to the next column.

The output of the function is the elements of the spiralmatrix printed in a spiral order.```

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

d. All of the above

All of the above options move the turtle backward, with each option being a different syntax for the same action.

a. turtle.back(distance) moves the turtle backward by the specified distance.

b. turtle.bk(distance) is a shorthand for turtle.back(distance), which also moves the turtle backward by the specified distance.

c. turtle.backward(distance) is another method that moves the turtle backward by the specified distance.

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

a. gmplot

a. gmplot is a Python library that allows you to create Google Maps plots with your GPS location data. It provides an easy-to-use interface for creating Google Maps-based plots of points, lines, and polygons.
b. csv is a Python library that allows you to read and write comma-separated values (CSV) files. While it can be used to read GPS location data from CSV files, it does not provide the functionality to plot this data on a map.

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

c. Binary Types

- The bytes, bytearray, and memoryview are types of the binary data type in Python.

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

b. 5

```from collections import deque

# Create a visited set to keep track of visited squares
visited = set()

# Create a queue to store the squares to be visited
queue = deque([(0, 0)])

# Loop until all squares have been visited
while queue:
square, rolls = queue.popleft()

# Check if the player has reached the last square
if square == 100:
return rolls

# Check if the current square has already been visited
if square in visited:
continue

# Add the current square to the visited set

# Roll the dice from 1 to 6 and add the resulting square to the queue
for i in range(1, 7):
next_square = square + i

# Check if the next square is a ladder

# Add the next square to the queue
queue.append((next_square, rolls + 1))

# If the player cannot reach the last square, return -1
return -1

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 }
print(min_rolls)```

## Which of the following code snippet will create a tuple in python?

d. None of the above

In Python, tuples are created using round brackets () and separated by commas. In option a, the values 'kiran', 'bhushan', and 'madan' are enclosed in round brackets, which creates a tuple.

Option b creates a set, as sets are created using curly braces {} in Python.

Option c creates a list, as lists are created using square brackets [] in Python.

## What does the following program plot?

```import random
import matplotlib.pyplot as plt
rn=random.randint(0,9)
print(rn)
l=[0 for i in range(10)]
y=[]
for i in range(10):
x=int(input())
y.append(i)
if [x]==rn:
l[x]+=1
plt.plot(y,l)
plt.show()
```
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

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

```The program generates a random integer between 0 and 9 using random.randint(0,9) and assigns it to rn. It then creates a list l of 10 zeros. It takes input from the user 10 times using a for loop and appends the iteration number to a list y. If the input matches the randomly generated number rn, it increments the count of the corresponding index in the l list. Finally, it plots the values of l against the values of y using plt.plot() and displays the plot using plt.show().

Therefore, the plot shows how many times the user input matched the randomly generated number rn. The x-axis represents the iteration number, and the y-axis represents the count of the matches.
```

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

d. a piece of information is positive or negative

Sentiment analysis, also known as opinion mining, is the process of determining the emotional tone or attitude expressed in a piece of text, such as a tweet, review, or article. It involves analyzing the language used and the context in which it is used to determine whether the sentiment expressed is positive, negative, or neutral. This type of analysis is commonly used in marketing, social media monitoring, and customer feedback analysis.

## What does the following code snippet in python compute

```text1= input()
len1 = = len(text1)
text2 = input()
len2 = len(text2)
for i in range(0,len1-len2+1):
j = 0
while ((j < len2) and (text1[i + j] == text2[j])):
j = j + 1
if (j==len2):
print(text2)```
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

c. finds all the occurrences of text2 in text1

```This code is designed to compare two strings text1 and text2 and print out the value of text2 if it appears as a substring in text1.

First, the code prompts the user to input two strings: text1 and text2. It then calculates the lengths of both strings using the len() function and assigns the values to len1 and len2 respectively.

The code then enters a for loop that iterates over the indices of text1. The loop runs for len1-len2+1 times because we only need to check a substring of text1 of length len2 against text2.

Inside the loop, there is a while loop that compares characters of the two strings. The loop runs until either j equals len2 (i.e., all characters of text2 have been matched), or until a mismatch is found.

If j equals len2 at the end of the while loop, it means that text2 appears as a substring in text1 starting at the index i. In this case, the code prints out the value of text2.```

Disclaimer:
"This page contains multiple choice questions (MCQs) related to The Joy of Computing using Python . The answers to these questions are provided for educational and informational purposes only.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."