The Joy of Computing Using Python Week 12 Solutions 2024
ABOUT THE COURSE :
A fun filled whirlwind tour of 30 hrs, covering everything you need to know
to fall in love with the most sought after skill of the 21st century. The
course brings programming to your desk with anecdotes, analogies and
illustrious examples. Turning abstractions to insights and engineering to
art, the course focuses primarily to inspire the learner's mind to think
logically and arrive at a solution programmatically. As part of the course,
you will be learning how to practice and culture the art of programming with
Python as a language. At the end of the course, we introduce some of the
current advances in computing to motivate the enthusiastic learner to pursue
further directions.
Summary
Course Type
Elective
Duration
12 weeks
Category
Computer Science and Engineering
Start Date
22 Jan 2024
End Date
12 Apr 2024
Exam Registration Ends
16 Feb 2024
Exam Date
21 Apr 2024 IST
Assignment Solutions :
Programming Assignment :
1. Programming Assignment | Week 10
Write a function named rotate that accepts a matrix mat as argument. It should return a matrix that is rotated by 90 degrees in the clockwise direction. For example:
You do not have to accept input from the user or print output to the console. You just have to write the function definition.
def rotate(mat):
"""
Rotate a matrix 90 degrees clockwise
Argument:
mat: list of lists
Return:
rotated_mat: list of lists
"""
if not mat:
return list()
rows = len(mat)
cols = len(mat[0])
rotated = [[0] * rows for csk in range(cols)]
for ipl in range(rows):
for jk in range(cols):
rotated[jk][rows - 1 - ipl] = mat[ipl][jk]
return(rotated)
2. Programming Assignment | Week 10
A n×n square matrix of positive integers is called a magic square if the following sums are equal:
(1) row-sum: sum of numbers in every row; there are n such values, one for each row
(2) column-sum: sum of numbers in every column; there are n such values, one for each column
(3) diagonal-sum: sum of numbers in both the diagonals; there are two values
There are n+n+2=2n+2 values involved. All these values must be the same for the matrix to be a magic-square.
Write a function named is_magic that accepts a square matrix as argument and returns YES if it is a magic-square and NOif it isn't one.
Notes
(1) The cells of a magic square need not be distinct. Some or even all the cells could be identical.
(2) You do not have to accept input from the user or print output to the console. You just have to write the function definition.
def is_magic(matrix):
"""
determine if a matrix is magic square
Argument:
mat: list of lists
Return:
string: 'YES' or 'NO'
"""
n = len(matrix)
magic_sum = sum(matrix[0])
for r1 in matrix:
if sum(r1) != magic_sum:
return "NO"
for j in range(n):
column_sum = sum(matrix[i][j] for i in range(n))
if column_sum != magic_sum:
return "NO"
diagonal_sum1 = sum(matrix[i][i] for i in range(n))
diagonal_sum2 = sum(matrix[i][n - 1 - i] for i in range(n))
if diagonal_sum1 != magic_sum or diagonal_sum2 != magic_sum:
return "NO"
return("YES")
3. Programming Assignment | Week 10
You are given certain details of the trains that stop at a
station. Your task is to store these details in a nested dictionary.
The first line of input is n, the number of
trains that stop at the station. n blocks of input follow. The
first line in each block corresponds to the train name. The second line in each
block corresponds to m, the number of compartments in the
train. m lines of input follow. Each of these m lines
has two values separated by a comma: name of the compartment and number of
passengers in it.
Your task is to create a nested dictionary named station_dict.
The keys of the dictionary are train names, the value corresponding to a key is
another dictionary. The keys of the inner dictionary are the compartment names
in this train, the values are the number of passengers in each compartment. For
example:
{
'Mumbai Express': {
'S1': 10,
'S2': 20,
'S3': 30
},
'Chennai Express': {
'S1': 10,
'S2': 20,
'S3': 30
}
}
(1) The values of the compartments should be represented as integers and not as strings.(2) You do not have to print the output to the console. Do not try to print the output that you observe in the "Expected Output". You just have to process the input and create the dictionary station_dict.
n = int(input())
station_dict =dict()
for chapri in range(n):
train_name = input()
m = int(input())
compartments = {}
for hardik in range(m):
compartment, passengers = input().split(",")
compartments[compartment] = int(passengers)
station_dict[train_name] = compartments
CRITERIA TO GET A CERTIFICATE :
Average assignment score = 25% of average of best 8 assignments out of the
total 12 assignments given in the course.
Exam score = 75% of the proctored certification exam score out of 100
Final score = Average assignment score + Exam score
YOU WILL BE ELIGIBLE FOR A CERTIFICATE ONLY IF AVERAGE ASSIGNMENT SCORE
>=10/25 AND EXAM SCORE >= 30/75. If one of the 2 criteria is not met,you will not get the certificate even if the Final score >= 40/100.
Follow Us