Question
Write a Python program, stored in a File named triangle.py, that performs the following task. -The program prompts the user to input a File name.
Write a Python program, stored in a File named triangle.py, that performs the following task.
-The program prompts the user to input a File name. If there is no File with that name in the working directory, then the program outputs an (arbitrary) error message and exits.
-The contents of the File consists of some number of lines, each line being a sequence of integers separated by at least one space, with possibly spaces before and after the First and last number, respectively, the Nth line having exactly N numbers. For instance, the contents of the file triangle_1.txt can be displayed as follows. 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
-The program outputs: 1)the largest value than can be obtained by summing up all numbers on a path that starts from the top of the triangle, and at every level down to the penultimate level, goes down to the immediate left or right neighbour; 2)the number of paths that yield this largest value; 3)the leftmost such path, in the form of a list.
Here is a possible interaction:
$ cat triangle_1.txt 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 $ python3 triangle.py Which data file do you want to use? triangle_1.txt The largest sum is: 30 The number of paths yielding this sum is: 1 The leftmost path yielding this sum is: [7, 3, 8, 7, 5]
$ cat triangle_2.txt 1 2 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 2 2 1 2 $ python3 triangle.py Which data file do you want to use? triangle_2.txt The largest sum is: 10 The number of paths yielding this sum is: 6 The leftmost path yielding this sum is: [1, 2, 1, 2, 2, 2] You can assume that the contents of any test le is as expected, you do not have to check that it is as expected.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started