← Back to challenges

Make My Way Home

PythonMediumnumbersmath

Instructions

You will be given a list, showing how far James travels away from his home for each day. He may choose to travel towards or away from his house, so negative values are to be expected.

Create a function that calculates what distance James must walk to get back home.

Examples

distance_home([2, 4, 2, 5]) ➞ 13

distance_home([-1, -4, -3, -2]) ➞ 10

distance_home([3, 4, -5, -2]) ➞ 0

Notes

  • Assume James only travels in a straight line.
  • Distance is always a positive number.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Return the First and Last Elements in a List