python Maximizes without using built in functions

  • 2021-07-13 05:33:39
  • OfStack

Using python to solve the problem, the requirement of solving is that the functions encapsulated in python, such as max, cannot be used

way1:


def findmax(data,n):
 if n==1:
  return data[0]
 else:
  maxi=data[0]
  for i in data[1:]:
   if maxi<i:
    maxi=i
 return maxi

data=[1,2,34,4]
print(findmax(data,len(data)))

code result:

34

way2:


def getMax(arr):
 for i in range(0,len(arr)):
  for j in range(i+1,len(arr)):
   first=int(arr[i])
   second=int(arr[j])
   if first<second:
    arr[i]=arr[j]
    arr[j]=first
 print(arr[0])
arr=[19,29,30,48]
getMax(arr)

code result

48


Related articles: