Posts

Stock Market Max Profit

Image
      For this week I did a problem called maxProfit. I found that this problem was actually a bit more practical for real life. The problem is that you are given a list containing integers that represent the price of a stock at a certain day. For example a list of [4,3,2,6,8,1,18,3,5,7]. For example at index 0 the stock was at 4 dollars and index 1 the price was 3 dollars. We would want to buy the stock at the lowest price possible and sell the stock at the highest price possible. For example in this case we would want to buy at price 1 and sell at price 18. We take the difference of that and our profit would be 17.      At first I thought right away of going to a double for loop and taking the difference at every step and seeing if that was the max profit. I however have been reading about big o(N) notation and that would not be optimal. That would be O(N^2). I thought about a faster solution where would would just go through the list once and have  a O(N) time. The way we accomplish

Coding Challenges

Image
      For this week I did 2 more coding problems. The first one I did was more challenging and its called isIsomorphic. Lets say we have 2 strings and a holds the value of the first string and b holds the value of the second string. Two strings are isomorphic only if you can replace all the characters in a with all the characters in b. That means that all the occurrences of a character in a string a must map directly back to another character in the string b. They must back and forth together in order for it to be isomorphic. For example the string abb is isomorphic with the string cdd. That is because for every a character you can replace it with the character c. Also every b character you can replace it with the letter d. In our problem we can also assume that both strings that are given will always be of equal length.      When I first started the problem right away I thought to create a dictionary. We could loop through the strings and map every character in string a with string b.

Pointers in C

Image
      For this weeks assignment I will demonstrate a technique I actually just learned this semester in one of my csc classes. It is the idea of pointers. I am currently learning about C. The interesting part about C is the concept of pointers that I did not realize we were already using in java. A pointer is a a variable that holds the memory of an address location in your memory. Instead of actually holding a value it holds a location that you can use and pass as a parameter to a function if you want to edit that variable in another function. If you were just to pass a value as a parameter the function would only get a copy of the value and if you try to edit it in the function the function that it got called from wouldnt update. I will explain what is going on in my code for an example.     The program I wrote is dice rolling game. I only posted my main and my player turn function to show how pointers work. In C the first thing you have to do is declare your prototypes. My prototype

Ping Pong in Python

Image
      For this weeks assignment I decided to learn how to make a game using Python. I have been looking up online how to get better at coding and other than doing coding challenges its useful to create real world projects to put everything together. I learned so much just from doing this project that I didn't know from just doing coding challenges. Now this is 147 lines of code so I wont explain every single detail but I did leave comments on all of my code so you can go through it and understand what each block of codes adds to the game.I will also upload a video at the end of this page so you can see the game working in real time.     The first thing I had to do is import a built in library in Python called Turtle. It allows you to draw different objects and animations on to your screen. Once I installed Turtle I created a variable holding the Turtle.Screen function that allows me to create a screen that pops up. Once I did that I changed different attributes of my screen such as

Sunset Views Coding Challenge

Image
    For this weeks challenge I did a problem called sunset views. The challenge is that you are given a list of integers and also a string that is either west or east. The integers in the array represent the height of buildings. Your challenge is to return to the user a new list of indexes that represent the buildings in the list that were given that can see the sunset. I also included a video of me explaining my solution since this problem is easier to understand if I can show you visually my solution. In this portion I will explain how my actual code works.      To start I initialize a couple variables. First is tallest that represents the tallest building in our list, the tallest index, a new building list which will hold the indexes that can see the the sunset and the second tallest that will be initialized to zero. We will have a first loop that will be used to first find the tallest building in our list. We will use this tallest building in either west or east direction. Once we

Week 9 Coding Challenges

Image
      For this weeks challenge I did a coding challenge called generate document. The way this works is you are given a function that takes in 2 strings. The first string is called characters and the second is document. Your goal is to see if you can generate a document with the given characters in your string. The only way you can generate a document is if you have the same amount of characters in your character string that will enable you to generate the document. For example if characters = "abcabc" and document = "aabbccc" you can't make the document because you are missing one 'c' in the characters string. If you are able to make the document you will return True to the user else False. This program will also account for spaces to be able to generate the document.      The first thing I thought up when I saw this challenge was to automatically use a for loop. Once I did though I realized I needed a way to keep track how many instances of each charac

Smallest Difference Coding Challenge

Image
    For this weeks challenge I decided to do another more difficult problem called the smallest difference. The way this challenge works is you are given 2 lists each containing integers. Your job is to return the pair of integers one from each list that produces the absolute difference that is closest to 0. The items you will return will be in a new list containing the pair for example [3,2]. I found a way to create 2 different solutions for this problem and after researching online I am starting to learn optimal solutions for different problems. I have not gotten to the course in my degree that we learn optimal space and time complexity using big 0 notation so I do not know the exact verbiage yet however it is obvious that if you have a solution that requires to for loops instead of just one while loop the 2 for loops will take longer time than the while loop since you have to traverse your lists 2 times.      The first solution I did which was not the optimal solution is also called