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.
# 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
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