#MODIFY THE TEMPLATE TO: Recall the grids from previous chapters. #Write code th
ID: 3719146 • Letter: #
Question
#MODIFY THE TEMPLATE TO: Recall the grids from previous chapters.
#Write code that gets a width and height from the user, and returns a
#two dimensional list, where each element is a string representation
#of the coordinates of that tile. PLEASE NOTE: ONLY use the len() and/or append() functions for this problem.
ONE MORE NOTE: Here is the row and column formulas if we want to find out what they might be based upon a given tile number for a grid of any dimensions, whether its 3X3, or 4X7, etc.
row = (tile - 1) // width
col = (tile - 1) % width
import subprocess
#from answer import *
import sys
#print sys.argv[0]
def template(w,h):
width = w
height = h
return [[]]
#END OF YOUR CODE
failed = False
w = 1
while w <= 3:
h = 1
while h <= 3:
result = []
row = 0
while row < h:
col = 0
inner = []
while col < w:
inner.append("("+str(col)+","+str(row)+")")
col += 1
result.append(inner)
row += 1
if template(w,h) == result:
print "you got it RIGHT!"
else:
print "for width:"+str(w) +" height:"+str(h)+" we expected "+str(result)+", but you got: " + str(template(w,h))
failed = True
h += 1
w += 1
source = open(sys.argv[0],"r")
lines = source.readlines()
source.close()
ctr = 1
Failed = False
while ctr < len(lines) and lines[ctr][:-1] != 'source = open(sys.argv[0],"r")':
line = lines[ctr]
if ('(".index(" in line or ".rindex(" in line' not in line) and (".index(" in line or ".rindex(" in line or ".remove(" in line or ".count(" in line or ".find(" in line or ".split(" in line or ".rsplit(" in line or ".rindex(" in line or ".join(" in line or "sorted(" in line or "min(" in line):
print "ERROR: on line " +str(ctr) + ": "+ line + " you are using a forbidden function or method. You are only allowed to use len(...) and/or .append(...) for this assignment."
failed = True
ctr += 1
if not Failed and failed == False:
print "Your code is CORRECT!"
else:
print "Please check your code, at least one test case did not pass."
Explanation / Answer
template Code:
def template(w,h):
width = w
height = h
l = [] #empty list
#loop height times
for i in range(height):
l1 = [] #empty list to store one line of coordinates
for j in range(width):
#add all points to l1
l1.append("(" + str(j) + "," +str(i) + ")")
l.append(l1) #append l1 to l
return l #return l
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.