Bonus Practice Problems(Triangular Pattern)

A triangular pattern with ‘n’ rows is formed with ‘i’ numbers in the i - th row, starting from the first row.  In a triangular pattern, The number of elements in the first row will be 1; the number of elemnts in the second row will be 2 ; and so on.
For example, if the value of ‘n’ is 3 and if the elements in each row are : First row has only one element , namely,  1 ; second  row has two elements, namely,  2, 1 ; The third rwo has three elements, namely, 1, 2, 3.
1
2 1
1 2 3
Path in a triangular pattern is described as the sequence of numbers, with one number taken from each row, starting from the first row till the last row.   For example, the paths in the pattern above are
1 – 2 – 1
1 – 2 – 2
1 – 2 – 3
1 – 1 - 1
1 – 1 – 2
1 – 1 - 3
Value of a Path is the sum of the numbers in that path. In the above illustration, Maximum value of  the paths in the  is ‘6’. Write an algorithm and the subsequent Python program to compute the maximum value among the paths in the triangular pattern.
Input Format
First line contains an integer ‘n’ which indicates the number of rows in the triangular patters
Next few lines contains the input for the triangular pattern
Output Format
Print the maximum value of the path in a triangular pattern
Input for the problem
The number of rows n
The triangular pattern
Processing involved
n = int(input())
maxi = []
pattern = []
for i in range(1,n+1):
    temp = []
    for j in range(1,i+1):
        temp.append(int(input()))
    pattern.append(temp)
for i in pattern:
    maxi.append(max(i))
Output for the problem
Print the maximum value of the path in a triangular pattern
Pseudocode
Step1.Get the number of rows n
Step2.Get the triangular pattern row by row and add all the maximum values to the maxi list
Step3. Print the sum of maxi list
Step4.End
Program:
n = int(input())
maxi = []
pattern = []
for i in range(1,n+1):
    temp = []
    for j in range(1,i+1):
        temp.append(int(input()))
    pattern.append(temp)
for i in pattern:
    maxi.append(max(i))

print(sum(maxi))

No comments:

Post a Comment

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...