How many blocks you walk
- O(1) · Trivial
- Free
- Python
- JavaScript
- lists
- indexes
- operations
Problem
The city is a grid and every corner arrives as a list of two numbers: [street, avenue].
From corner [3, 5] to corner [7, 2] you walk 4 blocks along the streets and 3 along the avenues: 7 in total. There is no diagonal shortcut, you walk on the sidewalk.
Write a function that takes the two corners and returns how many blocks you walk. It doesn't matter which corner comes first: walking never adds up to less than 0. Some streets and avenues have negative numbers, the ones on the other side of downtown.
Examples
Three up and four across
[0, 0], [3, 4] → 7
The avenue goes backwards
[3, 5], [7, 2] → 7
Same avenue, street backwards
[8, 1], [2, 1] → 6
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def blocks(here, away):
passJavaScript
function blocks(here, away) {
}It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.