Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 12 - Inheritance and Polymorphism - Programming Exercises - Page 429: 12.2

Answer

code

Work Step by Step

# 12.2 (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. class Location: def __init__(self): self.row = -1 self.col = -1 self.maxValue = -1 def getLocation(lst): loc = Location() for i in range(len(lst)): for j in range(len(lst[0])): if lst[i][j] > loc.maxValue: loc.maxValue = lst[i][j] loc.row = i loc.col = j return loc def main(): rows, cols = eval(input("Enter the number of rows and columns in the list: ")) lst = [] for i in range(rows): row = input("Enter row " + str(i) + ": ").split() row = [float(x) for x in row] lst.append(row) loc = getLocation(lst) print("The location of the largest element is " + str(loc.maxValue) + " at (" + str(loc.row) + "," + str( loc.col) + ")") main()
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.