def gravitate(nums, direction):
Description:
o Apply "sideways gravity" to nums by combining all the numbers in
toone , placing the result on one
side in some direction, replacing all the other numbers with zeroes. Also return nums when done.
Assumptions:
o nums is a two-dimensional list of numbers (floats, or integers)
o direction is a string, either "left" or "right"
Restrictions:
o You need to modify the list in-place, and also return it.
Updates:
o We'd not intended it, but it's okay to use sum() on this function. You'll still need to do
modifications in-place, which was the point of the question.
o We changed one test case to avoid problematic floating point approximation issues.
• Examples:
o gravitate([[1,2,3],[4,5,6]], "left") o
gravitate([[1,2,3],[4,5,6]], "right")
def gravitate(nums, direction): for row in nums: s = sum(row) for i in range(len(row)): row[i] = 0 if direction == 'left': row[0] = s else: row[-1] = s return nums print(gravitate([[1,2,3],[4,5,6]], "left")) print(gravitate([[1,2,3],[4,5,6]], "right"))
**************************************************
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
def gravitate(nums, direction): Description: o Apply "sideways gravity" to nums by combining all the numbers in...