NPTEL Joy Of Computing Using Python Week 4 Programming Assignment 2024

Hey Folks, Hello Everyone. We returned with a new Assignment Joy Of Computing Using Python Week 4 Programming Assignment. We have been providing the answers since the start of the assignment. We are providing with 100 % Accuracy. Then Why Wait, Come with us till Last Let's solve this week's assignments also. 

Join our telegram channel 👇👇 

 CLICK HERE  for more updates and answers.

About Swayam NPTEL

Swayam is an initiative by the Government of India's Ministry of Education to provide online education courses for free to anyone who wants to learn. Swayam offers courses on various topics, including engineering, humanities, management, and more.

NPTEL, which stands for National Program on Technology Enhanced Learning, is a joint initiative by IITs and IISc to provide free online courses and study material in engineering, science, and technology. NPTEL offers over 2,000 courses in various domains and is one of India's most popular online learning platforms.

NPTEL Joy Of Computing Using Python

NPTEL Joy Of Computing Using Python Week 4 Programming Assignment 2024

Last Date: 22-02-2024

Q1. You are given the names and dates of birth of a group of people. Find all pairs of members who share a common date of birth. Note that this date need not be common across all pairs. It is sufficient if both members in a pair have the same date of birth.

The first line of input is a sequence of comma-separated names. The second line of input is a sequence of comma-separated positive integers. Each integer in the sequence will be in the range [1,365], endpoints inclusive, and stands for someday in the year.

Find all pairs of names that share a common date of birth and store them in a list called common. Each element of this list is itself a list and should be of the form [name1, name2], such that name1 comes before name2 in alphabetical order.

Notes

1)     You can assume that each test case will have at least one pair of members who share the same date of birth.

2)     You do not have to print the output to the console. This is the responsibility of the autograder. You just have to populate the list common in the required format.

Sample Input/Output

For example, consider the input:

sachin,ramesh,rohit,priya,saina,sandeep,stella

100,50,100,20,30,20,20

Your list common could look like this:

[['rohit', 'sachin'], ['priya', 'sandeep'], ['priya', 'stella'], ['sandeep', 'stella']]


Program Code: 

def find_common_birthdays(names, dates):
    birth_dict = {}
    common = []

    # Create a dictionary where keys are dates and values are lists of names born on that date
    for name, date in zip(names, dates):
        if date not in birth_dict:
            birth_dict[date] = [name]
        else:
            birth_dict[date].append(name)

    # Iterate through the birth_dict to find pairs with common dates
    for date, name_list in birth_dict.items():
        if len(name_list) > 1:
            name_list.sort()  # Sort the names alphabetically
            # Generate pairs of names
            for i in range(len(name_list)):
                for j in range(i+1, len(name_list)):
                    pair = [name_list[i], name_list[j]]
                    # Ensure that only one of the pairs is added
                    if pair not in common and pair[::-1] not in common:
                        common.append(pair)

    # Sort the list common based on alphabetical order of names within each pair
    # and the index of the first name in the original input list
    common.sort(key=lambda x: (names.index(x[0]), x[0], x[1]))

    return common


# Sample Input
names = input().strip().split(',')
dates = list(map(int, input().strip().split(',')))

# Find pairs with common birthdates
common = find_common_birthdays(names, dates)

# Printing the result in the desired format {codeBox}


Q2. Accept two square matrices A and B of dimensions n×n as input and compute their product AB.

The first line of the input will contain the integer n. This is followed by 2n lines. Out of these, each of the first n lines is a sequence of comma-separated integers that denotes one row of the matrix A. Each of the last n lines is a sequence of comma-separated integers that denotes one row of the matrix B.

Your output should again be a sequence of n lines, where each line is a sequence of comma-separated integers that denote a row of the matrix AB.

Program Code: 

def matrix_product(A, B):
    n = len(A)
    result = [[0] * n for ads in range(n)]

    for i in range(n):
        for j in range(n):
            for k in range(n):
                result[i][j] += A[i][k] * B[k][j]

    return(result)


# Input
n = int(input())
A = []
B = []

# Input matrix A
for apple in range(n):
    row = list(map(int, input().strip().split(',')))
    A.append(row)

# Input matrix B
for mango in range(n):
    row = list(map(int, input().strip().split(',')))
    B.append(row)

# Compute matrix product AB
AB = matrix_product(A, B)

# Output
for row in AB:
    print(','.join(map(str, row))) {codeBox}


Q3. Accept a square matrix A and an integer s as input and print the matrix sâ‹…A as output. Multiplying a matrix by an integer s is equivalent to multiplying each element of the matrix by s. For example:

2 * |  1   2  |  =  |  2  4 | 
      |  3   4  |  =  |  6  8 | 

The first line of input is a positive integer, n, that denotes the dimension of the matrix A. Each of the next n lines contains a sequence of space-separated integers. The last line of the input contains the integer s.

Print the matrix sâ‹…A as output. Each row of the matrix must be printed as a sequence of space-separated integers, one row on each line. There should not be any space after the last number on each line. If the expected output looks exactly like the actual output and still you are getting a wrong answer, it is because of the space at the end.

Program Code: 

# Read input
n = int(input())  # Dimension of the matrix A
A = []  # Initialize matrix A

# Input matrix A
for pay in range(n):
    row = list(map(int, input().split()))  # Read a row of integers
    A.append(row)

s = int(input())  # Scalar value

# Multiply each element of matrix A by the scalar s
result = [[s * A[i][j] for j in range(n)] for i in range(n)]

# Print the result
for row in result:
    print(*row) {codeBox}


Conclusion: 

Follow for more Updates and answers

One Comment Please !

Post a Comment (0)
Previous Post Next Post