-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path011_problem.py
More file actions
17 lines (13 loc) · 976 Bytes
/
011_problem.py
File metadata and controls
17 lines (13 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#Find the Largest element in array
#short method:
def largest(arr): #create a function and pass the array as an argument
return max(arr) #use the built-in max() function to find and return the largest element in the array
arr = [3, 5, 7, 2, 8] #define an array with some elements
print(largest(arr)) #call the function and print the largest element
#logic method without function:
arr = [3, 5, 7, 2, 8] #define an array with some elements
largest = arr[0] #initialize largest variable with the first element of the array
for i in range(1, len(arr)): #loop through the rest of the elements in the array
if arr[i] > largest: #if the current element is greater than the largest element found so far
largest = arr[i] #update the largest variable with the current element
print(largest) #print the largest element