'Absolute value of 2 numbers
I have a function defined below in which two parameters are taken and the bigger of the two numbers is used and its absolute value is calculated .
def max_absolute_value(int1, int2):
value = 0
if (int1 >= int2):
value = int1
else:
value = int2
if value <= 0:
return value * -1
return value * 1
result = max_absolute_value(3, -5)
print(result)
The above should return 5 as the absolute value of -5 is higher than 3, however, when I run the function it returns None.
Solution 1:[1]
May be use something like this -
def max_absolute_value(int1,int2):
return max(abs(int1), abs(int2))
Now with your code the None is returned because in the if block there is no return. You may have meant -
def max_absolute_value(int1,int2):
value = 0
if (int1 >= int2):
value = int1
else:
value = int2
if value <= 0:
return value * -1
return value * 1
Still there is some logic issue in your code. In the if condition -5 will be less than 3. So your code will return absolute value of that i.e. is 3. Which is wrong. I think the first solution will be better for you.
Solution 2:[2]
You could use a one-liner including max(...), abs(...) and map(...):
def max_absolute_value(int1, int2):
return max(map(abs, [int1, int2]))
result = max_absolute_value(-3, -5)
print(result)
Solution 3:[3]
It is giving absolute value of "3" instead of absolute value of "-5" as 3 > -5 You must first take absolute value of both the numbers and then compare them
def absolute_value(a):
if a <= 0:
return a * -1
return a * 1
def max_absolute_value(int1,int2):
if absolute_value(int1) >= absolute_value(int2):
return absolute_value(int1)
return absolute_value(int2)
A simpler approach for getting absolute value is to directly use the function abs(), which returns the absolute value of the number.
def max_absolute_value(int1,int2):
if abs(int1) >= abs(int2):
return abs(int1)
return abs(int2)
Using the max() function will return a bigger value of the two.
So, an even shortened form will be:
def max_absolute_value(int1,int2):
return max(abs(int1, int2))
Solution 4:[4]
This should work
def max_absolute_value(int1,int2):
return max(abs(int1),abs(int2))
result = max_absolute_value(3,-5)
print(result)
Solution 5:[5]
I have checked your code. The problem lies with the indentation level of your code. The indentation of 1st and 2nd if...else loop must match. Then it will work as expected. It should look like this-
def max_absolute_value(int1,int2):
value = 0
if (int1 >= int2):
value = int1
else:
value = int2
if value <= 0:
return value * -1
else:
return value * 1
result = max_absolute_value(3,-5)
print(result)
Solution 6:[6]
def max_absolute_value(int1,int2):
return int1 if abs(int1) > abs(int2) else int2
result = max_absolute_value(3,-5)
print(result)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jan |
| Solution 3 | |
| Solution 4 | girishf15 |
| Solution 5 | pb111 |
| Solution 6 | Chris Penrose |
