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.
You will read your input from the file triangle.txt . 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.
You will apply three different approaches to problem solving to this single problem - greedy, divide and conquer (recursive), and dynamic programming. Here is the outline of the code:
import time # returns the greatest path sum using greedy approach def greedy (grid): return # returns the greatest path sum using divide and conquer (recursive) approach def rec_search (grid): return # returns the greatest path sum using dynamic programming def dynamic_prog (grid): return # reads the file and returns a 2-D list that represents the triangle def read_file (): return def main (): # read triangular grid from file 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 main()You can always add more helper functions as needed.
You will have these lines of output:
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 recursive search, and dynamic programming will be the same. Whereas the result from the greedy search will be less.
The file that you will be submitting will be called Triangle.py. We will be looking for good documentation, descriptive variable names, clean logical structure, and adherence to the coding conventions discussed in class.
You may work with a partner on this assignment. Both of you must read the paper on Pair Programming and abide by the ground rules as stated in that paper. The file will have a header of the following form:
# File: Triangle.py # Description: # Student's Name: # Student's UT EID: # Partner's Name: # Partner's UT EID: # Course Name: CS 313E # Unique Number: # Date Created: # Date Last Modified:There will be only one copy of the file being submitted if you are doing pair programming.
Use the Canvas system to submit your Triangle.py file. We should receive your work by 11 PM on Tuesday, 28 Feb 2017. There will be substantial penalties if you do not adhere to the guidelines. Remember Python is case sensitive. The name of your file must match exactly what we have specified.