Coding Challenges Week 2


    For this week I decided to write solve two different coding problems. As the semester goes on and I get better using Python tools I will attempt to do more every week. The first problem is called minimum waiting time. You are given a list of integers that represent the amount of time in seconds it will take for queries to run. What we are trying to do is give the minimum amount of time it will take to run all those queries. The amount of time a query takes to run is adding all the values before it. If the query is running for the first time its time will be 0 seconds since there are no queries before it. If we are given a list of     [3,2,1,2,6] the minimum waiting time in seconds would be 17 seconds. That is because the time would be calculated as (0) + (1) + (1+2) + (1+2+2) + (1+2+2+3) = 17.

    The first steps in solving this problem is to create a variable to hold the total seconds and sort the array from minimum to maximum order. We sort the array because if you are given a list with the higher values first those numbers will keep running every time you sum up the list. It will make the function run faster if we put those values at the end of the list. To do this we use the sort method on the list. After that we use a for loop using enumerate to keep track of both the index and the element in the list. Our first condition will be if the index is zero then we add 0 seconds to our time. That is because the first index will also run instantly since it's the first execution. Our else condition will be if the index is not the first then we will sum up the list from the beginning. Doing this helped me learn a new technique in java. When you use the sum method and put your list inside of it you can choose what elements you want to sum using the : symbol. This is where you see sum(queries [0:index]). This is summing all the elements from 0 index all the way but not including the current index. Once we run through the entire list we simply return the time to the user.

    Our second coding challenge is called find the three largest numbers. Our goal is we are given a list of numbers are we must return a new list with the three largest elements in the list. This one was actually difficult. The reason if you are going through the list of 100 elements you need to return only the 3 highest ones. So how can you know which 3 are the highest and also keeping track of them in a list without terminating your loop early? Instead of explaining my code ima explain this solution logically. 

    First thing we have to do is create a list of three elements max. Python is different then Java so we cant declare a list of a set size. So what I did was create a list with 3 None types. None types means that it is null. It is not an int, string, bool or anything else it is simply empty. This is how we must declare a set size list without putting values in it. Now let's say we get a list of [100, 2 , 5, 35, 2, 1, 55, 40, 42]. By looking at this list its easy to point out the three largest values is [42, 55, 100]. However that is not how a computer works and a computer cant look at the list the way we do. Remember I created my empty list represented as [None, None, None]. So we go to the first element which is 100. We add that to the end of our list since our list is empty.[None, None, 100]. We then go to 2 and check if 2 is less than 100. If it is we then check if 2 is less than the index before it. Since the index before it is None we simply insert 2 there. Our updated list is [None, 2 , 100]. We then move on to 5. We check if 5 is less than 100 and it is. We check if 5 is less than 2 which its not. So at this point we have to update that index and then check if 2 is less than the previous index. When we check the previous index is None so we place 2 there and 5 at index 1. Our list is now updated to [2,5,100]. I believe you get what our program will be doing by now. It will compare the current element to the last element in our list and check every element before that as well. If it is greater than that element our program will then shift the element inside of our list back once and check it with the previous element before updating the current one.

Comments

  1. Nice work this week, Sergio. Looking forward to watching you solve more complex problems.

    ReplyDelete
  2. Hi, Sergio! I am currently learning MATLAB which is far less complex to other programming languages and is more engineer friendly. However, it was interesting reading about the problems were trying to solve and the explanations. For the minimum waiting time problem, I can see it having many applications to create compact, fast, and tight codes. Still, I wonder if the approach to solving this problem where you save the higher values for last to avoid waiting those times is applicable for every situation. It seems a little too simple to be the case.
    For the second problem, I like your reasonable explanation. Rather than throwing your code at us, AKA jargon, you try to explain so that you make sure you and the reader truly understand. I have solved for a largest value using the max function in MATLAB, but your problem of finding the three largest values has me wondering if how I can do it in MATLAB and if the procedure would resemble anything to what you did. I am fairly new to coding, so questioning and testing these ideas would be beneficial to me. Great work.

    ReplyDelete

Post a Comment

Popular posts from this blog

Ping Pong in Python

Sunset Views Coding Challenge

Week 9 Coding Challenges