Answer the question
In order to leave comments, you need to log in
How to find max value without ?/switch/if?
So far, only the solution with cycles has come to mind.
Here is an example:
$arr = [1,2,3,4,5,6,98,65,190];
$max = $arr[0];
foreach($arr as $val){
while($val > $max){
$max = $val;
break;
}
}
Answer the question
In order to leave comments, you need to log in
You can write a function max
without using conditional statements:
def max_(a, b):
return (a + b + abs(a-b)) / 2;
m = 0
arr = [1, 2, 3, 4, 5, 6, 98, 65, 190]
for val in arr:
m = max_(m, val)
print(m)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question