Answered step by step
Verified Expert Solution
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 s and s whose dimension is controlled by user input, as
well as the density of s in the grid, and finds out for a given stepnumber and a given stepsize
the number of stairs of stepnumber many steps, with all steps of size stepsize.
A stair of step of size is of the form:
A stair of steps of size is of the form
A stair of step of size is of the form
A stair of steps of size is of the form
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 stairsingrid
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 displaygrid:
for i in rangelengrid:
printjoinstrintgridij
for j in rangelengrid
try:
argforseed, density, dim intx for x in
inputEnter three positive integers: split
if argforseed or density or dim :
raise ValueError
except ValueError:
printIncorrect input, giving up
sysexit
seedargforseed
grid randint density for in rangedim for in rangedim
printHere is the grid that has been generated:
displaygrid
def stairsingrid:
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 numberofsteps,
# numberofstairswiththatnumberofstepsofthatstepsize
#
# ordered from smallest to largest numberofsteps.
stairs stairsingrid
for stepsize in sortedstairs:
printf
For steps of size stepsize we have:
for nbofsteps, nbofstairs in stairsstepsize:
stairorstairs 'stair' if nbofstairs else 'stairs'
steporsteps 'step' if nbofsteps else 'steps'
print nbofstairs, stairorstairs, 'with',
nbofsteps, steporsteps
Grid can only contain and s The patterns and testcases are in the images please make sure the caclulation passes the correct calculation of stairs
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