ABOUT THE COURSE :

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 9
word is
a string that contains one or more parentheses of the following types: "{
}", "[ ]", "( )". The string is said to be balanced if
all the following conditions are satisfied.
When read from left to right:
(1) The number of opening parentheses of a given type is equal to the number of
closing parentheses of the same type.
(2) An opening parenthesis cannot be immediately followed by a closing
parenthesis of a different type.
(3) Every opening parenthesis should be eventually closed by a closing
parenthesis of the same type.
Write a function named balanced that
accepts the string word as an
argument.Return True if the string is
balanced, and False otherwise. You can
assume that the string doesn't contain any characters other than parentheses.
You do not have to accept the input from the user or print
output to the console. You just have to write the function definition.
def balanced(word):
stack = list()
opening = "([{"
closing = ")]}"
mapping = {')': '(', ']': '[', '}': '{'}
for char in word:
if char in opening:
stack.append(char)
elif char in closing:
if not stack or stack.pop() != mapping[char]:
return(not True)
return(not stack)
2. Programming Assignment | Week 9
Consider
the problem about balanced expressions discussed in 1. Programming Assignment | Week 9. We have a
balanced expression (string) that has only the flower brackets: '( )'. We can
recursively define a concept called nesting depth for each
pair of opening and closing brackets.
The
nesting depth of a pair that lies within another pair is one more than the
nesting depth of the pair that immediately englobes it. For a pair that is not
surrounded by any other pair, the nesting depth is 1.
You do not have to accept the input
from the user or print output to the console. You just have to write the
function definition.
def depth(expr):
max_deep = 0
current_deep= 0
for char in expr:
if char == '(':
current_deep += 1
max_deep = max(max_deep, current_deep)
elif char == ')':
current_deep -= 1
return(max_deep)
3. Programming Assignment | Week 9
Write a recursive function
named power that
accepts a square matrix A and a positive integer m as
arguments and returns Am.
You do not have to accept input
from the user or print the output to the console. You just have to write the
function definition.
def power(A, m):
"""
A recursive function to compute A ** m
Arguments:
A: list of lists
m: integer
Return:
result: list of lists
"""
if m == 0:
ap = len(A)
return [[1 if i == j else 0 for j in range(n)] for i in range(ap)]
elif m == 1:
return A
else:
G = power(A, m // 2)
if m % 2 == 0:
return matrix_multiply(G, G)
else:
return matrix_multiply(matrix_multiply(G, G), A)
def matrix_multiply(A, B):
rows_A = len(A)
cols_A = len(A[0])
cols_B = len(B[0])
result = [[0] * cols_B for i in range(rows_A)]
for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
result[i][j] += A[i][k] * B[k][j]
return result
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
Follow Us