Skip to main content

Posts

Showing posts from November, 2020

Finding your roots

I tried working on the Algorithms I course on Coursera a while back and I had no idea what was going on so I never continued with it. I decided to give it another try now that I've read up a little on algorithms. It's still using a lot of my brain cells but I am slowly making my way through it.  Learning about Quick-unions in Java(from the course) Java seems almost identical to C#. I've forgotten most of what I've learnt about C# but, there's enough in this brain for me to read this. I had such a tough time understanding the root method. I wrote it out in my notebook and worked through it to figure out how it functions and I am amazed! That line is so simple yet complex. And it reminded me of a binary tree LeetCode challenge I was trying to work on with my colleagues some time ago. I had no idea what binary trees even were at that point. The challenge involved finding roots and tre

Duplicate Zeros

Challenge: Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right. Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place, do not return anything from your function. This is one of those times that I have truly amazed myself. I overthought this problem so much and ended up with a solution that had such a long runtime. Lesson learnt! This is me overcomplicating my life An insanely simple solution by someone with more braincells than me

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.       

More list challenges from LeetCode

Challenge: Given an array nums of integers, return how many of them contain an even number of digits. This was pretty straightforward. Again, I did it in Python. Perhaps it's time I started working on these in C#. It would be a good way of trying to relearn C# and have a deeper understanding of the language. class Solution:     def findNumbers(self, nums: List[int]) -> int:         count = 0         for num in nums:             if len(str(num))%2 ==0:                 count +=1         return count

Algorithms: Max Consecutive Ones

Challenge from LeetCode: count the maximum number of consecutive 1s in a list of 1s and 0s. I got it done pretty quickly, or so I thought. I had not accounted for what would happen upon the last iteration through the for loop. Reworked it and this is what I got:  def checkMax(maxConsec, count):         if count > maxConsec:             return count, 0         else:             return maxConsec, 0          class Solution:         def findMaxConsecutiveOnes(self, nums: List[int]) -> int:         maxConsec = 0         count = 0                 for index, num in enumerate(nums):             if num == 1:                   count += 1                 if index+1 == len(nums):                     maxConsec, count = checkMax(maxConsec, count)             else:                 maxConsec, count = checkMax(maxConsec, count)                              return maxConsec The memory usage of my code   Runtime. Oops. I'm sure there are better ways to do this. I thought of putting all my count