Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Randomly generates a grid with 0s and 1s, whose dimension is controlled by user input, as well as the density of 1s in the grid,

Randomly generates a grid with 0s and 1s, whose dimension is controlled by user input, as well as the density of 1s in the grid, and finds out, for a given direction being one of N, E, S or W (for North, East, South or West) and for a given size greater than 1, the number of triangles pointing in that direction, and of that size. Triangles pointing North: # - of size 2: # 1 # 1 1 1 # - of size 3: # 1 # 1 1 1 # 1 1 1 1 1 # # Triangles pointing East: # - of size 2: # 1 # 1 1 # 1 # - of size 3: # 1 # 1 1 # 1 1 1 # 1 1 # 1 # # Triangles pointing South: # - of size 2: # 1 1 1 # 1 # - of size 3: # 1 1 1 1 1 # 1 1 1 # 1 # # Triangles pointing West: # - of size 2: # 1 # 1 1 # 1 # - of size 3: # 1 # 1 1 # 1 1 1 # 1 1 # 1 # # The output lists, for every direction and for every size, the number of triangles # pointing in that direction and of that size, provided there is at least one such triangle. # For a given direction, the possble sizes are listed from largest to smallest. # # We do not count triangles that are truncations of larger triangles, that is, obtained # from the latter by ignoring at least one layer, starting from the base. from random import seed, randint import sys from collections import defaultdict def display_grid(): for i in range(len(grid)): print(' ', ' '.join(str(int(grid[i][j] != 0)) for j in range(len(grid)))) def triangles_in_grid(): return {} # Replace return {} above with your code # Possibly define other functions try: arg_for_seed, density, dim = input('Enter three nonnegative integers: ').split() except ValueError: print('Incorrect input, giving up.') sys.exit() try: arg_for_seed, density, dim = int(arg_for_seed), int(density), int(dim) if arg_for_seed < 0 or density < 0 or dim < 0: raise ValueError except ValueError: print('Incorrect input, giving up.') sys.exit() seed(arg_for_seed) grid = [[randint(0, density) for _ in range(dim)] for _ in range(dim)] print('Here is the grid that has been generated:') display_grid() # A dictionary whose keys are amongst 'N', 'E', 'S' and 'W', # and whose values are pairs of the form (size, number_of_triangles_of_that_size), # ordered from largest to smallest size. triangles = triangles_in_grid() for direction in sorted(triangles, key = lambda x: 'NESW'.index(x)): print(f' For triangles pointing {direction}, we have:') for size, nb_of_triangles in triangles[direction]: triangle_or_triangles = 'triangle' if nb_of_triangles == 1 else 'triangles' print(f' {nb_of_triangles} {triangle_or_triangles} of size {size}')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logidata+ Deductive Databases With Complex Objects Lncs 701

Authors: Paolo Atzeni

1st Edition

354056974X, 978-3540569749

More Books

Students also viewed these Databases questions

Question

What are the stages of project management? Write it in items.

Answered: 1 week ago