Sunset Views Coding Challenge


    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 have our tallest we will enter our first condition which will be in case the direction that is given is "EAST".

    If the direction is east we will go through our list starting from the last element. This is because if the sun sets from the east direction our last element will always be able to see the sunset so we add the index to our list. We will initialize that to be our second tallest. Next we will check if the index we are at is the current tallest. If it is we will check to see if it is taller than the second tallest. If it is we will add it to our return list and break the loop. Our last condition is if the current building is taller than our second tallest. If it is we add it our list and update our second tallest. We will do this till we reach the tallest index which in that case our loop will break. 

    Now if the user gives us the view from the "WEST" our solution is a bit different but similar. Instead of starting from the end of the array we will start from the beginning. That is because the first element will always be able to see the sunset from the west. Next we will check if we are at the tallest index and check to see if it is the second tallest. If it is then we add it to our return list. We also check to see if the building we are at is taller than our second tallest. If it is we add that to our indexes and update our second tallest. At the end I also sorted the return list that way we can give it to our user in ascending values. I also attached a video that I will use to visually explain my solution so please watch. Thank you.

https://youtu.be/SYvYPKoyRiE
 

Comments

  1. I'm loving the videos. What a stellar way to share your progress. I'm going to send directly to Dr. Tuohy-- he will be thoroughly impressed as well.

    ReplyDelete

Post a Comment

Popular posts from this blog

Ping Pong in Python

Week 9 Coding Challenges