Programming Assignment - 7

Programming Assignment - Week 7


Note: There could be optimised ways to solve the problem, but the goal here is to solve it effectively. The most intuitive solution has been provided for clarity.

Week 7: Programming Assignment 1

Question:

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:

  • ladders: A list containing the indices of blocks with ladders.
  • snakes: A list containing the indices of blocks with snakes.
  • current_position: An integer representing the player's current position on the board.
  • die_roll: An integer 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.

def move_player(ladders, snakes, current_position, die_roll):
    # Calculate the new position after the die roll
    new_position = current_position + die_roll
    
    # Check if the new position is a ladder, snake, or a normal block
    if new_position in ladders:
        return 1  # Player lands on a ladder
    elif new_position in snakes:
        return -1  # Player lands on a snake
    else:
        return 0  # Player lands on a normal block

# Take inputs
ladders = list(map(int, input().split()))
snakes = list(map(int, input().split()))
current_position = int(input())
die_roll = int(input())

# Call the function and print the result
result = move_player(ladders, snakes, current_position, die_roll)
print(result,end="")

Week 7: Programming Assignment 2

Question:

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.

def isPalindrome(string, i, j):
    # Base case: if the characters at i and j are not the same, it's not a palindrome
    if string[i] != string[j]:
        return 0
    # Base case: if i has crossed j, it means all characters have been checked
    if i >= j:
        return 1
    # Recursive call: move towards the center of the string
    return isPalindrome(string, i + 1, j - 1)

# Take input
string = input()
# Check if the string is a palindrome
print(isPalindrome(string, 0, len(string) - 1), end="")

Week 7: Programming Assignment 3

Question:

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.

def is_binary(matrix):
    # Check if all elements in the matrix are either 0 or 1
    for row in matrix:
        for element in row:
            if element not in (0, 1):
                return False
    return True

def is_symmetric(matrix):
    # Check if the matrix is equal to its transpose
    rows = len(matrix)
    cols = len(matrix[0])
    if rows != cols:
        return False
    for i in range(rows):
        for j in range(cols):
            if matrix[i][j] != matrix[j][i]:
                return False
    return True

# Input the matrix
r = int(input())
c = int(input())
matrix = []

# Read the matrix elements
for _ in range(r):
    row = list(map(int, input().split()))
    matrix.append(row)

# Check the properties of the matrix
binary = is_binary(matrix)
symmetric = is_symmetric(matrix)

# 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="")

Comments

Popular posts from this blog

The Joy of Computing using python -Week 1