Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are provided with a stub in which you need to insert your code where indicated without doing any changes to the existing code to

You are provided with a stub in which you need to insert your code where indicated without doing any
changes to the existing code to complete the task. Although it is not needed for this quiz, you may import
any extra module that is already installed in Ed if you wish.
The program 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 step_number >=1 and a given step_size >=2,
the number of stairs of step_number many steps, with all steps of size step_size.
A stair of 1 step of size 2 is of the form:
11
11
A stair of 2 steps of size 2 is of the form
11
11
11
A stair of 1 step of size 3 is of the form
111
1
111
A stair of 2 steps of size 3 is of the form
111
1
111
1
111
2
The output lists the number of stairs from smallest step sizes to largest step sizes, and for a given step size,
from stairs with the smallest number of steps to stairs with the largest number of stairs.
Your task is to implement the function called stairs_in_grid().
You may possibly define other functions.
The provided stub and the outputs of the sample test cases explain the task to be performed.
here is the stub :
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():
return {}
# REPLACE THE RETURN STATEMENT ABOVE WITH YOUR CODE
# POSSIBLY DEFINE OTHER FUNCTIONS
# A dictionary whose keys are step sizes, and whose values are lists of 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
)
Grid can only contain 0 and 1's, The patterns and testcases are in the images please make sure the caclulation passes the correct calculation of stairs
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

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions