SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
And Polymorphism
Exercise:
Programming Excercises
Question:2 | ISBN:978013274719 | Edition: 6

Question

(The Location class) Design a class named Location for locating a maximal value and its location in a two-dimensional list. The class contains the public data fields row, column, and maxValue that store the maximal value and its indexes in a two-dimensional list, with row and column as int types and maxValue as a float type.
Write the following method that returns the location of the largest element in a two-dimensional list.
def Location locateLargest(a):
The return value is an instance of Location. Write a test program that prompts the user to enter a two-dimensional list and displays the location of the largest element in the list.

Here is a sample run:

Enter the number of rows and columns in the list: 3, 4
Enter row 0: 23.5  35  2  10
Enter row 1: 4.5  3  45  3.5
Enter row 2: 35  44  5.5   12.6
The location of the largest element is 45 at (1, 2)

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

The Location class Program Code:

class Location:
    def __init__(self, row=0, column=0, maxValue=0.0):
        self.row = row
        self.column = column
        self.maxValue = maxValue

    @staticmethod
    def locateLargest(a):
        max_value = float('-inf')
        max_row = 0
        max_column = 0

        # Iterate over the rows and columns of the two-dimensional list
        for row in range(len(a)):
            for column in range(len(a[row])):
                if a[row][column] > max_value:
                    max_value = a[row][column]
                    max_row = row
                    max_column = column

        # Create a new Location instance with the maximum value and its location
        location = Location(max_row, max_column, max_value)
        return location


# Test program
def test_locate_largest():
    # Prompt the user to enter the dimensions of the list
    rows, columns = map(int, input("Enter the number of rows and columns in the list: ").split(', '))

    # Prompt the user to enter the elements of the list row by row
    a = []
    for i in range(rows):
        row_values = [float(x) for x in input(f"Enter row {i}: ").split()]
        a.append(row_values)

    # Find the location of the largest element in the list
    location = Location.locateLargest(a)

    # Display the location of the largest element
    print(f"The location of the largest element is {location.maxValue} at ({location.row}, {location.column})")


# Run the test program
test_locate_largest()

 

Executed Output:

Enter the number of rows and columns in the list: 3, 4
Enter row 0: 23.5 35 2 10
Enter row 1: 4.5 3 45 3.5
Enter row 2: 35 44 5.5 12.6
The location of the largest element is 45.0 at (1, 2)

 

0 0

Discussions

Post the discussion to improve the above solution.