Answered step by step
Verified Expert Solution
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 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:
def isstairx y size, stepsize:
Check if a stair with the given size and step size starts at x y
if y stepsize size lengrid or x stepsize size lengrid:
return False
# Check horizontal steps
for i in rangesize:
for j in rangestepsize:
if gridx i stepsizey j:
return False
# Check vertical steps
for i in rangesize:
for j in rangestepsize:
if gridx jy i stepsize:
return False
return True
stairs defaultdictlambda: defaultdictint
dim lengrid
for stepsize in range dim :
for size in range dim stepsize :
for x in rangedim:
for y in rangedim:
if isstairx y size, stepsize:
stairsstepsizesize
# Convert defaultdict to regular dict for output
output
for stepsize in sortedstairs:
outputstepsize
for nbofsteps in sortedstairsstepsize:
outputstepsizeappendnbofsteps, stairsstepsizenbofsteps
return output
# A dictionary whose keys are step sizes, and whose values are 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
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