The Joy of Computing using Python Week 7 Assignment

                     

Course name: The Joy of Computing Using Python

Link to Enroll: Click here

This is an assignment for The Joy of Computing using Python Week 7 Assignment


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

Answer: c


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.

Answer: a


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

Answer: a, b, c


This is an assignment for The Joy of Computing using Python Week 7 Assignment


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

Answer: a, c


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

Answer: b


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

Answer: b


This is an assignment for The Joy of Computing using Python Week 7 Assignment


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.

Answer: b


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

Answer: d


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

This is an assignment for The Joy of Computing using Python Week 7 Assignment
This is an assignment for The Joy of Computing using Python Week 7 Assignment

Answer: c


This is an assignment for The Joy of Computing using Python Week 7 Assignment


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


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:

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,end="")

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

DiagCalc(M)

This is an assignment for The Joy of Computing using Python Week 7 Assignment


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

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

This is an assignment for The Joy of Computing using Python Week 7 Assignment


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:

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

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

print(snake(M))

This is an assignment for The Joy of Computing using Python Week 7 Assignment

More Weeks solutions of this course: https://progies.in/answers/nptel/the-joy-of-computing-using-python

More NPTEL Solution: https://progies.in/answers/nptel


* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.


This is an assignment for The Joy of Computing using Python Week 7 Assignment