Question
Please answer using Python. Instruction: Randomly fills a grid of size 10 x 10 with 0s and 1s and computes: - The size of the
Please answer using Python.
Instruction:
Randomly fills a grid of size 10 x 10 with 0s and 1s and computes:
- The size of the largest homogenous region starting from the top left corner, so the largest region consisting of connected cells all filled with 1s or all filled with 0s, depending on the value stored in the top left corner;
- The size of the largest homogenous region starting from the top left corner, so the largest region consisting of connected cells all filled with 1s or all filled with 0s, depending on the value stored in the top left corner;
Example input/output: Enter 2 integers, the second one being nonnegative: 0 12 Here is the grid that has been generated:
1 1 1 0 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 0 1
1 0 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
The size_of the largest homogenous region from the top left corner is 96.
This is the code that have given
import sys from random import seed, randint
dim = 10 grid = [[None] * dim for _ in range(dim)]
def display_grid(): for i in range(dim): print(' ', ' '.join(str(int(grid[i][j] != 0)) for j in range(dim)))
# Possibly define other functions
try: arg_for_seed, density = input('Enter two nonnegative integers: ').split() except ValueError: print('Incorrect input, giving up.') sys.exit() try: arg_for_seed, density = int(arg_for_seed), int(density) if arg_for_seed < 0 or density < 0: raise ValueError except ValueError: print('Incorrect input, giving up.') sys.exit() seed(arg_for_seed) # We fill the grid with randomly generated 0s and 1s, # with for every cell, a probability of 1/(density + 1) to generate a 0. for i in range(dim): for j in range(dim): grid[i][j] = int(randint(0, density) != 0) print('Here is the grid that has been generated:') display_grid()
size_of_largest_homogenous_region_from_top_left_corner = 0 # Replace this comment with your code print('The size_of the largest homogenous region from the top left corner is ' f'{size_of_largest_homogenous_region_from_top_left_corner}.' )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started