Posts

Showing posts from April, 2022

Tutor Time

Image
               Tutor Time By Ashur Baroutta 

IP #8 - The List Goes on! Merging two sorted lists.

Image
Merging two sorted lists by Ashur Baroutta      This week we explore merging two sorted linked lists. My first try at this problem looks wildly different than the final submission. At first I tried creating a new list first, then I setup a loop to iterate and check the nodes within list 1 and list 2 and kept adding them to the newly created list one by one. To be fair, that approach worked fine, nothing wrong with it and my submission was accepted. Though its speed was low and the memory usage was even worse than my final submission.      So I got annoyed and started looking more into how to optimize or change up how im solving the problem. I saw recursion as a better way to go about getting better performance. Recursion is when a method calls itself within its execution. So with recursively iteratingthrough each node, I was able to get a huge leg up on my first submission in terms of performance. Satisfied with the performance in terms of speed I'm still l...

IP #7 : Palindrome Number

Image
   Palindrome Number    by Ashur Baroutta     Palindrome Number follows up on last week's work where we learned how to reverse an integer, which required a different sort of approach than the usual problem sets we've been working on.      A palindrome number is when the number reads the same way forward and backward, similar to the famous "racecar". So our problem statement simply asks us to return true if the number passed to us is a palindrome.      Given last week we learned how to reverse an integer, I thought it'd be useful to use the same type of approach. If I reverse half the integer and it's equal to the first half of the value passed, then they're the same forward and backward!      I used a basic while loop for when the value passed (x) is greater than the variable we use as our reversed number store. The code for my submission is below. The ranking isn't spectacular but I'm still proud of my efforts a...

IP #6 : Reversing an Integer

Image
 Reversing an Integer by Ashur Baroutta     The focus of this submission will be on reversing a given integer passed into our function. Our problem statement is as follows "Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31 -1], then return 0." Seems simple, though was actually a little odd. My initial reaction is to convert the integer to a string and then just reverse each character. Though in a way that feels like cheating as I don't think that solution is in the spirit of the assignment,  and it takes more resources to complete.      Pivoting my attention to a mathematical solution made the problem a lot more fun. Say our first example for x is 123. The way we attack it is to setup variables to track the reversed total and to track the remainder of a given iteration, we also create a loop iterating through while x does not equal 0.  The ...

IP#5 : LinkedList Removing Duplicates II

Image
 Removing Duplicates II by Ashur Baroutta         In our last submissions, we worked on removing duplicates, this week we look at the more complicated problem of the same type. Our problem statement is " Given the  head  of a sorted linked list,  delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list . Return  the linked list  sorted  as well ."      So not only do we have to remove duplicates, but we cannot return values that are not distinct, for example, if our list originally contained three 1's, we need to delete all the 1's in our list as even if we delete all but one of them, the number 1 itself is not a distinct value.      This took me about 3 days to think through and complete, the following is our code submission.  We start by first creating a new ListNode called newlist. We set its .next reference pointing to head (the ListNode passed ini...