-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEX12.2.py
47 lines (36 loc) · 1.44 KB
/
EX12.2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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()