This assignment is a variation of a problem from Project Euler. You are required to find the greatest path sum starting at the top of the triangle and moving only to adjacent numbers on the row below.
3 7 4 2 4 6 8 5 9 3In the above triangle, the maximum path sum is 3 + 7 + 4 + 9 = 23.
Input: You will read your input from the file triangle.in . The first line indicates n the number of rows in the triangle. This will be followed by n lines of data. Each line of data will have only positive integers greater than 0. The first line of data in the triangle will have one number, the second line in the triangle will have two numbers and so on. The nth line will have n integers.
The file that you will be submitting will have the following structure. You may NOT change the names of the functions but you may add as many helper functions as needed. You will follow the standard coding conventions in Python. You will apply four different approaches to problem solving to this single problem - exhaustive search, greedy, divide and conquer (recursion) and dynamic programming. Here is the outline of the code Triangle.py.
import time # Input: grid a 2-D list of integers # Output: returns a single integer that is the greatest path sum # using exhaustive search def exhaustive_search (grid): # Input: grid a 2-D list of integers # Output: returns a single integer that is the greatest path sum # using the greedy approach def greedy (grid): # Input: grid a 2-D list of integers # Output: returns a single integer that is the greatest path sum # using divide and conquer (recursive) approach def rec_search (grid): # Input: grid a 2-D list of integers # Output: returns a single integer that is the greatest path sum # using dynamic programming def dynamic_prog (grid): # reads the file and returns a 2-D list that represents the triangle # Input: none # Output: returns a 2-D list that represents the triangle def read_file (): # Input: no input # Output: a string denoting all test cases have passed def test_cases (): # write your own test cases return "all test cases passed" def main (): # read triangular grid from file ti = time.time() # output greates path from exhaustive search tf = time.time() del_t = tf - ti # print time taken using exhaustive search ti = time.time() # output greates path from greedy approach tf = time.time() del_t = tf - ti # print time taken using greedy approach ti = time.time() # output greates path from divide-and-conquer approach tf = time.time() del_t = tf - ti # print time taken using divide-and-conquer approach ti = time.time() # output greates path from dynamic programming tf = time.time() del_t = tf - ti # print time taken using dynamic programming if __name__ == "__main__": main()You can always add more helper functions as needed.
Output: You will have these lines of output in a file called triangle.out.
The greatest path sum through exhaustive search is 23. The time taken for exhaustive search is x seconds. The greatest path sum through greedy search is 23. The time taken for greedy approach is x seconds. The greatest path sum through recursive search is 23. The time taken for recursive search is x seconds. The greatest path sum through dynamic programming is 23. The time taken for dynamic programming is x seconds.Most likely the maximum path that you will get from exhaustive search, recursive search, and dynamic programming will be the same. Whereas the result from the greedy search will be less. The time for the greedy approach will be the shortest and the time for the exhaustive search will be the longest.
For this assignment you may work with a partner. Both of you must read the paper on Pair Programming and abide by the ground rules as stated in that paper. If you are working with a partner then only one of you will submit the code. Make sure that in the header in HackerRank that you have your name and UT EID and your partner's name and UT EID. If you are working alone then you will just have your name and your UT EID.
Use the HackerRank platform to submit your code. We should receive your work by 11 PM on Friday, 17 Jul 2020. There will be substantial penalties if you do not adhere to the guidelines. HackerRank will not assign late penalties (if any), we will make the adjustments.