Question: #MODIFY THE TEMPLATE TO: Write code that walks over a two dimensional list of integers, and checks to see if a neighbor of an element

#MODIFY THE TEMPLATE TO: Write code that walks over a two dimensional list of integers, and checks to see if a neighbor of an element (so to its right or left) is negative; if so, it returns a string containing all pairs in parentheses of the (neighbor,current element) separated by spaces. PLEASE NOTE: ONLY use the len() and/or append() functions for this problem.

ONE MORE NOTE: Please Read Below

I see that there is a needs more information indication for this question. So what we do is we are going through this 2D list, and we are seeing if its left or right neighbor is a negative number. If so, we return that as a string containing the negative number as well as its preceding number. So for the element [], its left neighbor is -2, so we return (-2, 1). For the element -1, its left neighbor is [], so we don't do anything. For -2 in the 3rd list within the 2d, its left neighbor is -1, and the next PRECEDING number (note that [] doesn't count as a number) would be -2, so we'd return -1, -2. For -2, its right neighbor is -3, so it's be (-3,-2), and since 3's left neighbor is -2, it'd be (-2,-3). And 4's left neighbor is -3, so we'd return (-3,4).

#For example, the list [[1,-2],[],[-1,-2,-3,4]] would be returned as the string # (-2,1) (-2,-1) (-1,-2) (-3,-2) (-2,-3) (-3,4) import subprocess #from answer import * import sys def template(l1): list1 = l1 return "(0,0)" #END OF YOUR CODE test1 = template([[1,-2],[],[-1,-2,-3,4]]) 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 test1 == "(-2,1) (-2,-1) (-1,-2) (-3,-2) (-2,-3) (-3,4)" or test1 == "(-2,1) (-2,-1) (-1,-2) (-3,-2) (-2,-3) (-3,4) ": print "you got it CORRECT!"

else: print "for [[1,-2],[],[-1,-2,-3,4]] we expected '(-2,1) (-2,-1) (-1,-2) (-3,-2) (-2,-3) (-3,4)', but you got: " + str(test1)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!