Skip to main content

Posts

Showing posts with the label algorithms

Oh Happy Day!

 I wrote my first recursive function today! I've read about recursion and wanted to attempt it for the longest time. I kept letting fear get in the way. Working on problems with my team has definitely helped me learn more, faster. It has also helped with my confidence. I finally attempted a simple problem using recursion on LeetCode. I was meant to reverse a string.  First, I had to figure out how a recursive function worked exactly. When a function calls itself, it stops the execution of the rest of the code in the function until it reaches the "end" of the chain of function calls. The base case is what determines when the recursive function stops. Without a base case, it would keep calling itself forever like images reflecting off parallel mirrors facing each other. In my function, the base case is when the end of the list is reached, index >= len(s).  Once the base case is reached, it returns to the previous recursive function call on line 5 and line 6-12 are execut...

Creating a linked list

 I'm working my way through the mini courses on LeetCode to understand data structures and algorithms better. I am currently on linked lists. After a few slides, I was tasked with creating a linked list. I had no idea what to do and was stuck on this problem for days. I would look at it for a bit, not know what to do, and try to work on something else, and not be able to focus on that something else because I didn't know what to do about the linked list. Meh! It was a long 3-4 days. I finally figured it out and I have to say I feel very pleased with myseld :D  Step 1: Define a node class and a linked list class.  Each node has a value and a pointer to the next node. There is just a single pointer here because I chose to create a singly linked list. You can also have a pointer in the reverse direction point at the previous node. That would be a doubly linked list. I also had to define my actual linked list which held the value for the head node.  Step 2: Create a get ...

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 s...

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             ...