Question

def gravitate(nums, direction): Description: o Apply "sideways gravity" to nums by combining all the numbers in...

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")

 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
def gravitate(nums, direction): Description: o Apply "sideways gravity" to nums by combining all the numbers in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT