max value of list without max() method
Question:
I need to find the max value of a list but I can’t use the max() method. How would I go about doing this? I’m just a beginner so I’m sure this is pretty easy to find a workaround, however I’m stumped.
Edit: Forgot to mention that it’s python I’m working in.
Answers:
Sounds like homework to me, but perhaps some logics can help you out:
when you iterate the list, the point is that you want to know whether the current element is the MAX of all others. This can be solved by keeping the MAX up to the current element (say M*) and do a simple comparison between M* and your current element. Then update M* according to the result of that comparison. At the end of the iteration I think you can figure out where your MAX is.
max = list[0]
for x in list:
if x > max:
max = x
print max
In this example, we initialize the max value to the first element. Then we iterate through the list, and if we find a larger value than the current max, we assign that value to max. At the end, we should have the largest value, which we print.
Think about how you would do this manually.
If you had a list like this:
[10, 6, 8, 2, 4, 12, 3]
To find the max manually, you would start with the first number, 10, and that would be the max. Then you move to the next number, 6:
M
[10, 6, 8, 2, 4, 12, 3]
^
Is it bigger than 10? No, then move to the next number:
M
[10, 6, 8, 2, 4, 12, 3]
^
M
[10, 6, 8, 2, 4, 12, 3]
^
M
[10, 6, 8, 2, 4, 12, 3]
^
M
[10, 6, 8, 2, 4, 12, 3]
^
Now we come to a number that IS bigger than our max of 10. So then we move the max point:
M
[10, 6, 8, 2, 4, 12, 3]
^
Then continue:
M
[10, 6, 8, 2, 4, 12, 3]
^
The list is finished now, and M points at 12.
Now you just need to code it up!
Simple one-liner in python:
max = sorted(my_list)[-1]
This puts the list in order from smallest to largest, then takes the final value which is the largest.
def max(a):
res = a[0]
for i in a:
if res < i:
res = i
return res
array = (1, 2, 3, 4, 5, 6)
print(max(array))
You could try this…..
my_list = [0, 2, 5, 6, 5, 8, 5, 8, 8]
my_max = [ ]
for list in my_list:
if list > 7:
my_max = my_max + [list]
Print(my_max)
This should output [8,8,8]
arr=[10,11,12,9,10,11]
#Find the length of you list
len=len(arr)
#Use 1st for loop to iterate over index from 0 to total length
for i in range(len):
# Use 2nd for loop to iterate from from index 1 to total length
for j in range(i+1,len):
#Compare 1st index to next index
if arr[i]>arr[j]:
#replace the positions to sort our max number at last
arr[i],arr[j]=arr[j],arr[i]
# Now, your sorted list having last element as a max for access it with index -1 (last index) of list, You will get max number from your list without using max() function
print("Your max number is:",arr[-1])
input_string =input("Enter a list numbers or elements separated by space: ")
userList = input_string.split()
l=list((map(int, userList)))
print(l)
a = (len(l))
temp=0
for i in range(0,a-1):
if l[i]>l[i+1]:
c=l[i]
if c>temp:
temp=c
else:
c= l[i+1]
if c>temp:
temp=c
print(temp)
s=[10,11,12,9,10,11]
length = len(s)-1
for i in range(length):
if s[i] > s[i + 1]:
s[i], s[i + 1] = s[i + 1], s[i]
print(s[-1]) #12
Here, we use the reduce function that is available in the built-in module called "functools", essentially "reduce" returns only one value, which in this case, is the maximum of the numbers. So, the main logic here is we return the current number if it’s larger than the next number in the list, else we just return the next number if it doesn’t satisfy the condition.
from functools import reduce
inp = input("Values separated by spaces: ")
arr = map(int, inp.split())
ans = reduce(lambda x, y: x if x > y else y, arr)
print(ans)
I need to find the max value of a list but I can’t use the max() method. How would I go about doing this? I’m just a beginner so I’m sure this is pretty easy to find a workaround, however I’m stumped.
Edit: Forgot to mention that it’s python I’m working in.
Sounds like homework to me, but perhaps some logics can help you out:
when you iterate the list, the point is that you want to know whether the current element is the MAX of all others. This can be solved by keeping the MAX up to the current element (say M*) and do a simple comparison between M* and your current element. Then update M* according to the result of that comparison. At the end of the iteration I think you can figure out where your MAX is.
max = list[0]
for x in list:
if x > max:
max = x
print max
In this example, we initialize the max value to the first element. Then we iterate through the list, and if we find a larger value than the current max, we assign that value to max. At the end, we should have the largest value, which we print.
Think about how you would do this manually.
If you had a list like this:
[10, 6, 8, 2, 4, 12, 3]
To find the max manually, you would start with the first number, 10, and that would be the max. Then you move to the next number, 6:
M
[10, 6, 8, 2, 4, 12, 3]
^
Is it bigger than 10? No, then move to the next number:
M
[10, 6, 8, 2, 4, 12, 3]
^
M
[10, 6, 8, 2, 4, 12, 3]
^
M
[10, 6, 8, 2, 4, 12, 3]
^
M
[10, 6, 8, 2, 4, 12, 3]
^
Now we come to a number that IS bigger than our max of 10. So then we move the max point:
M
[10, 6, 8, 2, 4, 12, 3]
^
Then continue:
M
[10, 6, 8, 2, 4, 12, 3]
^
The list is finished now, and M points at 12.
Now you just need to code it up!
Simple one-liner in python:
max = sorted(my_list)[-1]
This puts the list in order from smallest to largest, then takes the final value which is the largest.
def max(a):
res = a[0]
for i in a:
if res < i:
res = i
return res
array = (1, 2, 3, 4, 5, 6)
print(max(array))
You could try this…..
my_list = [0, 2, 5, 6, 5, 8, 5, 8, 8]
my_max = [ ]
for list in my_list:
if list > 7:
my_max = my_max + [list]
Print(my_max)
This should output [8,8,8]
arr=[10,11,12,9,10,11]
#Find the length of you list
len=len(arr)
#Use 1st for loop to iterate over index from 0 to total length
for i in range(len):
# Use 2nd for loop to iterate from from index 1 to total length
for j in range(i+1,len):
#Compare 1st index to next index
if arr[i]>arr[j]:
#replace the positions to sort our max number at last
arr[i],arr[j]=arr[j],arr[i]
# Now, your sorted list having last element as a max for access it with index -1 (last index) of list, You will get max number from your list without using max() function
print("Your max number is:",arr[-1])
input_string =input("Enter a list numbers or elements separated by space: ")
userList = input_string.split()
l=list((map(int, userList)))
print(l)
a = (len(l))
temp=0
for i in range(0,a-1):
if l[i]>l[i+1]:
c=l[i]
if c>temp:
temp=c
else:
c= l[i+1]
if c>temp:
temp=c
print(temp)
s=[10,11,12,9,10,11]
length = len(s)-1
for i in range(length):
if s[i] > s[i + 1]:
s[i], s[i + 1] = s[i + 1], s[i]
print(s[-1]) #12
Here, we use the reduce function that is available in the built-in module called "functools", essentially "reduce" returns only one value, which in this case, is the maximum of the numbers. So, the main logic here is we return the current number if it’s larger than the next number in the list, else we just return the next number if it doesn’t satisfy the condition.
from functools import reduce
inp = input("Values separated by spaces: ")
arr = map(int, inp.split())
ans = reduce(lambda x, y: x if x > y else y, arr)
print(ans)