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

Popular posts from this blog

Shortcuts

 I saw one of my colleagues do something magical. He automatically fixed the alignment of student code on Visual Studio with just 1 little keyboard shortcut. Ctrl-k, followed by D. Holding down Ctrl the entire time Sometimes shortcuts really do work and make your life better!   

Using Modals to render forms

A student wanted to create several tables with foreign keys linking them. He was placing the forms all over the place and that just didn't seem to make sense to me. I suggested that he link all the forms to one main registration page, and while he worked on it, I came up with my own solution that I feel rather proud of. Though I'm well aware it is a very simple solution.  views.py def register (request): # form for team formteam = TeamForm(request.POST or None ) if formteam.is_valid(): formteam.save() return redirect( 'registerForTeam' ) else : print (formteam.errors) formteam = TeamForm() # form for coach formcoaches = CoachesForm(request.POST or None ) if formcoaches.is_valid(): formcoaches.save() return redirect( 'registerForTeam' ) else : print (formcoaches.errors) formcoaches = CoachesForm() # form for player formplayer = PlayersForm(request.POST or None ...

So long and thanks for all the fish! Part 1 of 2

I have been with the Tech Academy both as a software developer bootcamp student, as well as an employee. After my bootcamp, I was hired first as the live project instructor, and then as Live Project Director. This, I believe, gives me a unique point of view. I have absolutely no regrets and would join the bootcamp again. But there are a number of things I would do differently. What I have learnt as a former student 1. DO NOT WORK PART TIME.   I worked part-time(20-30hrs) during my bootcamp. I was up at 2.30-3.00am every day to work for several hours. I took a short nap, and then I took a 1hr bus ride down to campus. Studied for 7- 9 hours. Took a 1hr bus ride back home. Lather, rinse, repeat. I also had some family obligations. My weekends and half the summer were taken up caring for my young stepdaughter. I was completely exhausted by the end of the bootcamp and I didn't know if I could do more. Learning to program is HARD. You need to be fully focused. I am fortunate because I di...