Posts

DSA GRIND #4

Image
 DSA GRIND #4 BY ASHUR BAROUTTA This week we kept up with the trend of solving 2 problems a week. The first problem asked us to concatenate two arrays. Stating "Given an integer array nums of length N, create an array ans of length 2N where ans[i] == nums[i] and ans[i+n] == nums[i]". This took more thought than actual coding as the solution turned out to be far simpler than imagined.  I settled for making a new array with twice the size of nums length, then I initialized a variable j = 0. After that I made a for loop and within that loop I set a condition to check if i was less than the length of the original array as if it was I knew I could just make the indexes of the new array equal to the same index as nums array. After that, we set newarray[i] equal to nums[j] as j is incrementing from 0 and at the point where this started we would be passed all the first recorded indexes.  A merger like this is probably useful when wanting to join two different specific arrays into...

Building A Simple Program with C (DiceGame)

Image
 DiceGame by Ashur Baroutta     Recently I've been learning the basic's of a language new to me, called C. It is actually quite old though the way in which you code with it looks similar to Java. I was tasked with creating a dice game that featured two players rolling until one of them had accumulated 100 or more points.  The rules for the game are simple, a player rolls 2 dice, if one of the dice is a 1 then the player records a 0 for the round. A player can choose to stand on their points accumulated that round to avoid the risk of rolling a 1 and scoring 0 for the turn. The first to reach a hundred or more wins. There are modifiers for specific scenarios, if a player should roll snake eyes (1 and 1) they score 25 points for that roll and if a player should roll two of the same number, that sum is doubled.      In theory, this game shouldn't have been so difficult to code though the way C works isn't the same as modern languages as it uses pointers t...

DSA GRIND #3

Image
 DSA GRIND #3 By Ashur Baroutta     This week we solved two problems. The first problem reads as follows :  "Given two strings , s and t, return true if "t" is an anagram of "s", and false otherwise". An anagram is a word formed by rearranging the letters of a different word, using the original words letters exactly once (typically).      To solve this I started with setting up an if condition to return false for the edge case in which the length of s did not match the length of t, as if the lengths don't match then the words are obviously not anagrams of each other. I then turned both the strings into character arrays and sorted them. After sorting, I made a for loop and checked each index for a character match, as if one failed to match I knew it was not an anagram. The code submission is shown below. I am pleased with the performance.     The second problem I worked on was a bit more complicated for me though it is built in part on the pre...

DSA GRIND #2

Image
DSA GRIND #2 by Ashur Baroutta      This week I spent most of my free time trying to come up with a solution for a problem called 3Sum. The problem statement reads "Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] equals 0".       A far from easy task for me to accomplish. Given the problem statement pointed out we cant have duplicates return in the trips, the first thing I did was sort the array so it'd be easier to iterate through and account for duplicates. I then initialized a new Linked List data structure in order to store the triplets as an array in each node. At this point I started a for loop and set the exit condition to be the length of the array-2, this is because I'm going to be using the last index as a right hand pointer that we keep sliding over (last index in arrays is always length-1).     After this I set up various conditi...

DSA Grind #1

Image
 DSA GRIND #1 by Ashur Baroutta     The first submission of the new semester will be focusing on the continuation of my data structure and algorithms training. I will be trying to push my limits each week and attacking several more problems to keep different methods/thought processes of problem solving sharp in my mind. Problem #1 : Remove Duplicates From Sorted Array Problem Statement :      "Given an integer array *nums* , sorted in non decreasing order, remove duplicates inplace such that unique elements appear only once, the order of the elements should be kept the same. After this, return k, where k is the number of unique elements remaining in the array. Do not allocate extra space for another array, this must be done by modifying the input array in place with O(1) extra memory (constant space complexity). " Solution :      I start by covering the edge cases with an if statement, if the length of the input array is 1 or 0, simple r...

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