Question: 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.

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,

# 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 Enter three nonnegative integers: 01 3 Here is the grid that has been generated: 1 1 0 For triangles pointing N, we have: 2 triangles of size 2 For triangles pointing E, we have: 2 triangles of size 2 For triangles pointing S, we have: 1 triangle of size 2 For triangles pointing W, we have: 1 triangle of size 2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!