Max Value of List Without Max() Method

max value of list without max() method

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.

Python: Function returning highest value in list without max()? [closed]

def highestNumber(l):
myMax = l[0]
for num in l:
if myMax < num:
myMax = num
return myMax


print highestNumber ([77,48,19,17,93,90])

Output

93

Or You can do something like this

def highestNumber(l):
return sorted(l)[-1]

Or

def highestNumber(l):
l.sort()
return l[-1]

Need help finding max value in java script (without max method)

There are a few things wrong with your code as pointed out in the comments so the question isn't 100% clear so I present you with the below approach...

function getMax() {
const nums = arguments;
var max = null;

for (let i = 0; i < nums.length; i++) {
const num = nums[i];
if (max === null || num > max) {
max = num;
}
}

return max;
}

This function accepts an infinite number of arguments to compare against and does not restrict to a fix number of inputs. For example, the below are all valid uses:

const validExample1 = getMax(33, 72, 189, 4, 1); // Output: 189
const validExample2 = getMax(2, 2); // Output: 2
const validExample3 = getMax(320, 1, 8); // Output: 320
const validExample4 = getMax(-1, -200, -34); // Output: -1

By not using the arrow function expression you can use the arguments keyword to retrieve an iterable object of all values. Alternatively, if requirements are to use arrow functions you can simply use the spread syntax (...) do...

const getMax = ( ...nums ) => {
var max = null;

for (let i = 0; i < nums.length; i++) {
const num = nums[i];
if (max === null || num > max) {
max = num;
}
}

return max;
}

A full working example:

const number1 = 3;
const number2 = 72;
const number3 = 189;

function getMax() {

const nums = arguments;
var max = null;

for (let i = 0; i < nums.length; i++) {
const num = nums[i];
if (max === null || num > max) {
max = num;
}
}

return max;
}

const result = getMax(number1, number2, number3);

console.log(`Nilai maksimum adalah ${result}`);

How do you find the maximum number in a list without using the function "max"?

A more efficient way of doing this:

numList = []
for i in range(10):
numList.append(random.randrange(0,11))

maxNum = -1
for i in numList:
if i > maxNum:
maxNum = i

print(maxNum)

Add highest and lowest values from a list into separate lists without using max and min

You can loop only once ...

lst = [1, 2, 3, 4, 78, 5, 7, 90]
# set min & max to point to the first element
_min = lst[0]
_max = lst[0]
for num in lst[1:]:
if num < _min:
_min = num
if num > _max:
_max = num
print(f'min: {_min}, max: {_max}')

output

min: 1, max: 90

Get the maximum value from an array without the max() function in python [closed]

A correct answer needs to use an iterator and work for any kind of item.

def maximum(items, default=None):
iterator = iter(items)
m = next(iterator)
for item in iterator:
if item > m:
m = item
return m


Related Topics



Leave a reply



Submit