NPTEL Joy Of Computing Using Python Week 7 Programming Assignment 2024

Welcome back, everyone! In this article, we'll walk you through the solutions for the Week 7 programming assignment of the NPTEL course "Joy of Computing Using Python." These answers are provided for reference, so feel free to use them as a guide. If you have any doubts or questions, don't hesitate to ask in the comments section below. Ready to dive in? Let's solve these problems together! 

Join Telegram:  CLICK HERE  


NPTEL Joy Of Computing Using Python Week 7 Programming Assignment 2024



This Week 7 had various topics related to Snake and Ladders, programming code to design the game is presented in the video course of NPTEL. Also, the Week 7 Consists of spiral traversing and GPS track route topics. 

Let present the Week 7 Programming Assignment Answers, 

Last Date: 12-09-2024

Q1. 
You are given a board game scenario with ladders and snakes. A player starts at a given position on the board, and you are provided with the result of a die roll. You need to determine whether the player lands on a ladder, a snake, or a normal block after moving.

Write a Python function named move_player that takes four inputs:

  • A list ladders containing the indices of blocks with ladders.
  • A list snakes containing the indices of blocks with snakes.
  • An integer current_position representing the player's current position on the board.
  • An integer die_roll representing the result of a die roll.
The function should return:
  • 1 if the player lands on a block with a ladder,
  • -1 if the player lands on a block with a snake,
  • 0 if the player lands on a block with neither.
Take necessary inputs, pass the inputs to the function, and print the return value of the function.

Input Format

The input consists of four lines:
  1. The first line contains space-separated integers representing the ladder indices.
  2. The second line contains space-separated integers representing the snake indices.
  3. The third line contains a single integer representing the current position of the player.
  4. The fourth line contains a single integer representing the die roll result.
Output Format

The output consists of a single integer (1, -1, or 0) as per the conditions mentioned above.

Program Code: 

def move_player(ladders, snakes, current_position, die_roll):
    # Calculate the new position after rolling the die
    new_position = current_position + die_roll
    
    # Check if the new position is a ladder
    if new_position in ladders:
        return 1
    # Check if the new position is a snake
    elif new_position in snakes:
        return -1
    # If it's neither a ladder nor a snake
    else:
        return 0

# Taking inputs from the user
ladders = list(map(int, input().split()))
snakes = list(map(int, input().split()))
current_position = int(input())
die_roll = int(input())

# Calling the function and printing the result
result = move_player(ladders, snakes, current_position, die_roll)
print(result, end="")  {codeBox}

Q2. 

Write a Python program that includes a recursive function named is_palindrome which takes a non-empty string s as input. The function should return 1 if the string is a palindrome (reads the same backward as forward), and 0 otherwise.

Take necessary inputs, pass the inputs to the function, and print the return value of the function.


Input Format:

The input consists of a single line containing the string s.

Output Format:

The output consists of a single integer value (1 or 0) indicating whether the string is a palindrome.

Example:

Input:  racecar

Output:     1  

Program Code:

def is_palindrome(s):
    # Base case: if the string has 0 or 1 character, it's a palindrome
    if len(s) <= 1:
        return 1
    # Check if the first and last characters are the same
    elif s[0] == s[-1]:
        # Recur on the substring excluding the first and last characters
        return is_palindrome(s[1:-1])
    else:
        return 0

# Take input from the user
s = input()

# Call the function and print the result
print(is_palindrome(s), end="") {codeBox}

Q3. 

Write a Python program that takes a matrix of size r x c as input, where r is the number of rows and c is the number of columns. The program should check if the matrix is binary (i.e., all elements are either 0 or 1) and if it is symmetric (i.e., the matrix is equal to its transpose).

The program should output:

  • 11 if the matrix is both binary and symmetric,
  • 10 if the matrix is binary but not symmetric,
  • 01 if the matrix is not binary but symmetric,
  • 00 if the matrix is neither binary nor symmetric.
Input Format:

  • The first line contains an integer r representing the number of rows.
  • The second line contains an integer c representing the number of columns.
  • The next r lines each contain c space-separated integers representing the elements of the matrix.
Output Format:

The output consists of a single string (either 11, 10, 01, or 00) as per the conditions mentioned above.
Example:

Input:
3
3
1 0 1
0 1 0
1 0 1

Output:
11

Program Code:

def is_binary(matrix):
    for row in matrix:
        for element in row:
            if element != 0 and element != 1:
                return False
    return True

def is_symmetric(matrix, r, c):
    if r != c:
        return False
    for i in range(r):
        for j in range(c):
            if matrix[i][j] != matrix[j][i]:
                return False
    return True

# Input number of rows and columns
r = int(input())
c = int(input())

# Input the matrix
matrix = []
for i in range(r):
    matrix.append(list(map(int, input().split())))

# Check if the matrix is binary and symmetric
binary = is_binary(matrix)
symmetric = is_symmetric(matrix, r, c)

# Determine the output based on the conditions
if binary and symmetric:
    print("11",end="")
elif binary and not symmetric:
    print("10",end="")
elif not binary and symmetric:
    print("01",end="")
else:
    print("00",end="") {codeBox}

Conclusion

If any changes in the answer, please visit the website on or before to the last date. 

Join Telegram:  CLICK HERE  


One Comment Please !

Post a Comment (0)
Previous Post Next Post