Area of square python assignment expert

Area of Square

Given an MxN matrix filled with

The first line of input will be containing two space-separated integers, denoting M and N.

The next M lines will contain N space-separated integers, denoting the elements of the matrix.

The output should be a single line containing the area of the maximum square.

For example, if the given M, N and elements of matrix are as the following

4 5

X O X O O

X O X X X

X X O X X

X O O X O

The matrix from indices [1, 3] to [2, 4] has the maximum square with

X. So the output should be the area of the maximum rectangle with X, which is 4.

Sample Input 1

4 5

X O X O O

X O X X X

X X O X X

X O O X O

Sample Output 1

4

Sample Input 2

3 3

O X X

O X X

O O O

Sample Output 2

4

Given an MxN matrix filled with

The first line of input will be containing two space-separated integers, denoting M and N.

The next M lines will contain N space-separated integers, denoting the elements of the matrix.

The output should be a single line containing the area of the maximum square.

def max_square[matrix]:
    M, N = len[matrix], len[matrix[0]],
    arr = [[0 for _ in range[N + 1]] for _ in range[M + 1]]
    side_length = 0

    for i in range[1, len[arr]]:
        for j in range[1, len[arr[0]]]:
            if matrix[i - 1][j - 1] == "X":
                arr[i][j] = min[arr[i][j - 1], arr[i - 1][j], arr[i - 1][j - 1]] + 1
                side_length = max[side_length, arr[i][j]]

    return side_length * side_length


def parse_matrix[]:
    M, N = map[int, input[].split[]]
    matrix = [input[].split[] for _ in range[M]]
    return matrix


matrix = parse_matrix[]
print[max_square[matrix]]

Number of moves:

you are given a nxn square chessboard with one bishop and k number of obstacles placed on it. A bishop can go to different places in a single move. find the total no.of places that are possible for the bishop in a single move. Each square is referenced by a type describing the row, R, and column, C, where the square is located.

explanation: given N=6 K=2

bishop position 5 2

obstacle positions [2 2], [1 5]

the bishop can move in so o/p is 6

I/p:

6 2

5 2

2 2

1 6

O/p:

6

I/p:

6 4

3 3

1 3

3 1

5 1

1 5

O/p: 7

[n, k] = input[].split[' ']
n = int[n]
k = int[k]
board = []
for i in range[n]:
    for j in range[n]:
        board.append[[i+1, j+1]]
lst = []
obst = []
bp = input[].split[' ']
bp = [int[bp[0]], int[bp[1]]]
for i in range[k]:
    obst = input[].split[' ']
    lst.append[[int[obst[0]], int[obst[1]]]]
count = 0
for shift in [[1, 1], [-1, 1], [1, -1], [-1, -1]]:
    new_pos = [bp[0] + shift[0], bp[1] + shift[1]]
    while new_pos in board and new_pos not in obst:
        count += 1
        new_pos = [new_pos[0] + shift[0], new_pos[1] + shift[1]]
print[count]

Area of Square

Given an MxN matrix filled with

The first line of input will be containing two space-separated integers, denoting M and N.

The next M lines will contain N space-separated integers, denoting the elements of the matrix.

The output should be a single line containing the area of the maximum square.

For example, if the given M, N and elements of matrix are as the following

4 5

X O X O O

X O X X X

X X O X X

X O O X O

The matrix from indices [1, 3] to [2, 4] has the maximum square with

X. So the output should be the area of the maximum rectangle with X, which is 4.

Sample Input 1

4 5

X O X O O

X O X X X

X X O X X

X O O X O

Sample Output 1

4

Sample Input 2

3 3

O X X

O X X

O O O

Sample Output 2

4

Last update on August 19 2022 21:50:48 [UTC/GMT +8 hours]

Python Basic - 1: Exercise-54 with Solution

if you draw a straight line on a plane, the plane is divided into two regions. For example, if you pull two straight lines in parallel, you get three areas, and if you draw vertically one to the other you get 4 areas.
Write a Python program to create maximum number of regions obtained by drawing n given straight lines.

Input:
[1 ≤ n ≤ 10,000]
Input number of straight lines [o to exit]:
5
Number of regions:
16

Sample Solution:

Python Code:

while True:
    print["Input number of straight lines [o to exit]: "]
    n=int[input[]]
    if n=4, Goldbach number] from the user and create a combinations that express the given number as a sum of two prime numbers. Print the number of combinations.
Next: Write a Python program to test AB and CD are orthogonal or not.

import numpy as np
 print['']
 print["Enter two numbers, low then high."]
 l = int[input["low = "]]
 h = int[input["high = "]]


 def binarySearch [arr, l, r, num]:
 if r >= l:
 mid = l + [r - l] // 2

print["Is your number Less than, Greater than, or Equal to {0}?".format[arr[mid]]]
 while True:
 x = str[input["Type 'L', 'G' or 'E':"]]
 if x in ['L', 'G', 'E', 'l', 'g', 'e']:
 break

print['']
 if x == 'E' or x == 'e':
 num = num + 1
 if num == 1:
 print["I found your number in 1 guess."]
 else :
 print["Your number is {0}. I found it in {1} guesses.".format[arr[mid], num]]
 elif x == 'L' or x == 'l':
 num = num + 1
 return binarySearch[arr, l, mid-1, num]
 else:
 num = num + 1
 return binarySearch[arr, mid + 1, r, num]
 else:
 print["Your answers have not been consistent."]

while l>h:
 print['']
 print["Please enter the smaller followed by the larger number."]
 l = int[input["low = "]]
 h = int[input["high = "]]

print['']
 print["Think of a number in the range {0} to {1}.".format[l, h]]

print['']
 if l == h:
 print["Your number is {0}. I found it in 0 guesses.".format[l]]

else :
 arr = np.arange[l+1, h]
 binarySearch[arr, 0, len[arr]-1, 0]

How do you find the area of Square in Python?

Python Program.
area_square=s*s..
print["Area of the square="+str[area_square]].

How do you find the area and perimeter of a square in Python?

Python program to find area and perimeter of a square.
Firstly, we will take input from the user for the side value of the square using the input[] method..
Now, calculate the area of the square by using the formula Area = s*s..
Next, we will calculate the perimeter of a square by using the formula Perimeter=4*s..

Chủ Đề