Skip to main content

Sorting squares

Challenge: Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

I was killing myself yesterday using enumerate. And today I was using all sorts of conditional statements and I finally ended up with this. Kind of feels like I cheated because I'm just using a built in method.

class Solution:

    def sortedSquares(self, A: List[int]) -> List[int]:

        lenA = range(len(A))

        for i in lenA:

            A[i] = pow(A[i],2)

        A.sort()

        return A


It's kind of difficult to be proud because I had so much difficulty trying to work through a bug in my code. There were a few conditions I did not consider that kept breaking my code. It was frustrating. Can't say I'm entirely pleased that I used the sort method. I'll feel better when I can actually write code to sort a list from scratch. 

     

Comments