Randomly generates a grid with 0s and 1s, whose dimension is controlled by user
ID: 3736679 • Letter: R
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, 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 < 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}')
Explanation / Answer
ANS:-
Given that,
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.
PROGRAM:-
import java.util.*;
public class Grids {
public static void main(String args[]) {
int n;
Scanner in = new Scanner(System.in);
System.out.print("Grid number: ");
n = in.nextInt();
North(n);
East(n);
South(n);
West(n);
}
public static void North(int n) {
Random rand = new Random();
System.out.println(" Triangles pointing North");
for (int i = 2; i <= n; i++) {
System.out.println(" of size " + i);
for (int j = 0; j < i; j++) {
for (int k = 0; k < i - j - 1; k++) {
System.out.printf("%2c", ' ');
}
for (int k = 0; k < 2 * (j + 1) - 1; k++) {
int r = rand.nextInt(2) % 2;
System.out.printf("%2d", r);
}
System.out.println();
}
}
}
public static void South(int n) {
System.out.println(" Triangles pointing South");
Random rand = new Random();
for (int i = 2; i <= n; i++) {
System.out.println(" of size " + i);
for (int j = 0; j < i; j++) {
for (int k = 0; k < j; k++) {
System.out.printf("%2c", ' ');
}
for (int k = 0; k < 2 * (i - j) - 1; k++) {
int r = rand.nextInt(2) % 2;
System.out.printf("%2d", r);
}
System.out.println();
}
}
}
public static void East(int n) {
System.out.println(" Triangles pointing East");
Random rand = new Random();
for (int i = 2; i <= n; i++) {
System.out.println(" of size " + i);
for (int j = 0; j < i; j++) {
for (int k = 0; k < j; k++) {
int r = rand.nextInt(2) % 2;
System.out.printf("%2d", r);
}
System.out.println();
}
for (int j = i-1; j >= 0; j--) {
for (int k = 0; k < j + 1; k++) {
int r = rand.nextInt(2) % 2;
System.out.printf("%2d", r);
}
System.out.println();
}
}
}
public static void West(int n) {
System.out.println(" Triangles pointing West");
Random rand = new Random();
for (int i = 2; i <= n; i++) {
System.out.println(" of size " + i);
for (int j = 0; j < i; j++) {
for (int k = 0; k < i-j; k++) {
int r = rand.nextInt(2) % 2;
System.out.printf("%2c", ' ');
}
for (int k = 0; k < j + 1; k++) {
int r = rand.nextInt(2) % 2;
System.out.printf("%2d", r);
}
System.out.println();
}
for (int j = i-2; j >= 0; j--) {
for (int k = 0; k < i-j; k++) {
int r = rand.nextInt(2) % 2;
System.out.printf("%2c", ' ');
}
for (int k = 0; k < j + 1; k++) {
int r = rand.nextInt(2) % 2;
System.out.printf("%2d", r);
}
System.out.println();
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.