Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This code only gives grids and not the steps and stars as in the test case I need to fix it: from random import seed,

This code only gives grids and not the steps and stars as in the test case I need to fix it:
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))
)
)
try:
arg_for_seed, density, dim =(int(x) for x in
input('Enter three positive integers: ').split()
)
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()
def stairs_in_grid():
def is_stair(x, y, size, step_size):
"""Check if a stair with the given size and step size starts at (x, y)."""
if y + step_size *(size -1)>= len(grid) or x + step_size *(size -1)>= len(grid):
return False
# Check horizontal steps
for i in range(size):
for j in range(step_size):
if grid[x + i * step_size][y + j]!=1:
return False
# Check vertical steps
for i in range(size):
for j in range(step_size):
if grid[x + j][y + i * step_size]!=1:
return False
return True
stairs = defaultdict(lambda: defaultdict(int))
dim = len(grid)
for step_size in range(2, dim +1):
for size in range(2, dim // step_size +1):
for x in range(dim):
for y in range(dim):
if is_stair(x, y, size, step_size):
stairs[step_size][size]+=1
# Convert defaultdict to regular dict for output
output ={}
for step_size in sorted(stairs):
output[step_size]=[]
for nb_of_steps in sorted(stairs[step_size]):
output[step_size].append((nb_of_steps, stairs[step_size][nb_of_steps]))
return output
# A dictionary whose keys are step sizes, and whose values are pairs
# of the form (number_of_steps,
# number_of_stairs_with_that_number_of_steps_of_that_step_size
# ),
# ordered from smallest to largest number_of_steps.
stairs = stairs_in_grid()
for step_size in sorted(stairs):
print(f'
For steps of size {step_size}, we have:')
for nb_of_steps, nb_of_stairs in stairs[step_size]:
stair_or_stairs = 'stair' if nb_of_stairs ==1 else 'stairs'
step_or_steps = 'step' if nb_of_steps ==1 else 'steps'
print('', nb_of_stairs, stair_or_stairs, 'with',
nb_of_steps, step_or_steps
)
image text in transcribed

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

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions

Question

How is natural gas related to oil?

Answered: 1 week ago

Question

=+ (b) Show that the condition is sufficient as well.

Answered: 1 week ago

Question

What is the referencing environment in the body of function B ?

Answered: 1 week ago

Question

3. Explain the forces that influence how people handle conflict

Answered: 1 week ago