The Joy of Computing Using Python Week 9 Solutions 2024

ABOUT THE COURSE :


Disclaimer: The answers to these questions are provided for educational and informational purposes only. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself.

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


 Write a function named depth that accepts a balanced expression (string) as an argument. It should return the maximum nesting depth in this expression.


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 :
  1. Average assignment score = 25% of average of best 8 assignments out of the total 12 assignments given in the course.
  2. Exam score = 75% of the proctored certification exam score out of 100
  3. 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.

Join With Us On :

Join Now


Previous Year Questions Now to Boost Your Exam Performance

LOADS OF LOGIC