2019-07-01 08:10:18 +00:00
|
|
|
"""Absolute Value."""
|
|
|
|
|
|
|
|
|
2019-07-10 20:09:24 +00:00
|
|
|
def abs_val(num):
|
2018-11-03 20:08:13 +00:00
|
|
|
"""
|
2019-07-01 08:10:18 +00:00
|
|
|
Find the absolute value of a number.
|
|
|
|
|
2019-10-21 20:36:33 +00:00
|
|
|
>>> abs_val(-5.1)
|
|
|
|
5.1
|
|
|
|
>>> abs_val(-5) == abs_val(5)
|
|
|
|
True
|
|
|
|
>>> abs_val(0)
|
2018-11-03 20:08:13 +00:00
|
|
|
0
|
|
|
|
"""
|
2019-10-08 08:24:01 +00:00
|
|
|
return -num if num < 0 else num
|
2018-11-03 20:08:13 +00:00
|
|
|
|
2019-07-01 08:10:18 +00:00
|
|
|
|
2019-10-21 20:36:33 +00:00
|
|
|
def test_abs_val():
|
|
|
|
"""
|
|
|
|
>>> test_abs_val()
|
|
|
|
"""
|
|
|
|
assert 0 == abs_val(0)
|
|
|
|
assert 34 == abs_val(34)
|
|
|
|
assert 100000000000 == abs_val(-100000000000)
|
2019-07-01 08:10:18 +00:00
|
|
|
|
2018-11-03 20:08:13 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2019-10-21 20:36:33 +00:00
|
|
|
print(abs_val(-34)) # --> 34
|