Progamming in Python

 Today, I completed some assignments in the intermediate level: guess the number and parking algorithm. Both refreshed my coding skills!

Completing the Guess the Number assignment was relatively simple. I used the template, setting conditions for the 3 branches of the if statement. I don't completely understand the __name__== "__main__" part, although I know that it is the main class of the program (similar to java).

I tried the Panda assignment, but the content surpassed my level of programming, so I worked on the parking algorithm. 

  1. Following the directions on the assignment, I created a get_input function that saved price inputs directly into variables a, b, and c, which were added to a list named prices. The arrival and departure times for trucks were appended to a list and then separated by truck into tuples. To ensure for a cleaner transfer between functions, variables t1, t2, t3, and prices were stored in a list, named gi_return, which was returned by the function.


  1. The calculate function had several functions: 
    1. unpacking the data from gi_return: The function was given the list gi_return from the get_input function, and the calculate function generated a list of times at which each truck was parked: for example, a truck parked from 1-4 pm was present at 1pm, 2pm, and 3 pm. A list, named duration1, 2, or 3, was generated for each truck with integer values (1-12) representing hours of the day, as a timestamp would work. The three different rates were also organized into a list named prices.
    1. determining during which periods of time the parking lot had one, two, or three trucks: A nested for-loop was written for this purpose. The primary loop, which iterated from 1-12, was written to find the number of times a certain "timestamp" was found in the duration lists. The variable i represented the target timestamp, and three sub-loops checked lists duration1, duration2, and duration3, respectively, and if a timestamp was found in a list, the variable subcount would be incremented by 1. For instance, if truck 1, 2, and 3 were all present at 5 pm (meaning that duration1, 2, and 3 contained the integer 5), the subcount for 5pm would total 3. The subcounts were appended in chronological order to a master list named count.
    1. calculating the total price: by now, the count list would contain a list such as the following: [0,1,3,3,0,0,0,0,0,0,0,0]. This list represents a scenario in which one truck arrived at 2 pm, the other 2 trucks arrived at 3 pm, and all stayed until 4 pm. A for-loop was iterated through the count list, calculating the total price for each hour of the day (based on the number of trucks present and the correct price) and adding it to a TOTAL_price variable that was returned at the end of the function.


Writing this algorithm was challenging, and though it works for simpler cases, it still fails the test given on the assignment, outputting 33 instead of 36. I worked out the algorithm by hand as I would imagine the program doing so and still received 33 as the total price, so more debugging tomorrow!

Comments